Linux - View and Edit File Permissions.
Tags: linux, bash, beginners
Published: 2022-11-30 07:34:28

When working with servers most of the time you will work with **linux** servers (Ubuntu, Debian, etc.). In order to change, view or create **files** you need the right **permissions**. To get a basic understanding of what that means, I will tell you how to view, read and interpret these permissions.

## Viewing Permissions

An easy way to view permissions for files inside a folder you can use the `ls` command with the `o` or `l` flag like so:

```bash
ls -o
#or
ls -l
```

Move to a directory that has files in it and type the above command into your terminal. It will generate multiple lines of output that look similar to this:

```bash
# permissions   hard links  user    group of user   file size    change date     file/folder name
  drwxrwxr-x    9           toscani toscani         288         11 Nov 09:07    public
```

The important part are the first ten letters `drwxrwxr-x`. They describe the permissions. These characters are broken down into four groups.

1. The first letter, in this case `d`
1. Letters two to four `rwx`
1. Letters five to seven `rwx`
1. and finally eight to ten `r-x`

Group one states if the entry describes a file `-`, a directory `d` or a link `l`. The other three groubs describe the permissons each type of user has. On linux we distinguish between the **user**, the **group** and **others** - in this order. Hence `drwxrwxr-x` means:

1. It is a **directory** which can be
1. _read_, _written_ and _executed_ by the **user**
1. _read_, _written_ and _executed_ by the **group** the user is in
1. _read_ and _executed_ by the **others**

> You are maybe wondering what "executing a directory" means. It basically lets the you open that directory. "Reading a directory" on the other hand means "to be able to see" the directory, e.g. being able to list it with the `ls` command.

## Changing permissions

Now that you know how to view file permissions, I will tell you how to change them. For this we use the `chmod` (change mode) command. There are two different methods for this. The _symbolic method_ and the _absolute method_.

> If you have ever seen the `chmod` command in a tutorial before the _symbolic method_ is the one with the letters, the _absolute method_ is the one with the numbers ;).

### Symbolic method

The _symbolic method_ combines an _access class_ with an _operator_ and an _access type_ to set permissions. A common example is to make a file executable for everybody:

```bash
chmod a+x my_file.sh
# or, as `a` is the default
chmod +x my_file.sh
```

| Access class | Operator              | Access Type  |
| ------------ | --------------------- | ------------ |
| `u`, user    | `+`, to add access    | `r`, read    |
| `g`, group   | `-`, remove access    | `w`, write   |
| `o`, other   | `=`, set exact access | `x`, execute |
| `a`, all     |                       |              |

These parameters can be combined in many different ways. Although I can´t tell you what the right configuration for a specific usecase is, the following examples might help you to find that out yourself.

> `u` always means the current user and `g` always refers to the group the current user is in.

```bash
# remove read and write permissions for all except you
chmod go-rw my_file.sh

# set access to only read for everybody
chmod a=r myfile.sh

# remove write access from and add execution rights for the group
chmod g-w+x myfile.sh

# grant read permission for a directory and all files in it to all
# -R is used as a flag, not as a parameter to describe the permissions
chmod -R +r mydir
```

### Absolute method

In contrast to the _symbolic method_, the _absolute method_ sets all permissions at once by using a three digit number. Each of the digits is the sum of it´s individual permissions. Let´s use the following example:

```bash
chmod 774 myfile.sh
```

#### Base permissions

| Permission | Number         |
| ---------- | -------------- |
| 4          | read           |
| 2          | write          |
| 1          | execute        |
| 0          | no permissions |

#### Combinations

| Permission | Number                              |
| ---------- | ----------------------------------- |
| 7          | read, write and execute (4 + 2 + 1) |
| 6          | read and write (4 + 2)              |
| 5          | read and execute (4 + 1)            |
| 3          | execute and write (1 + 2)           |

The first number represents the **user**, the second the **group** and the last represents **others**. Hence, the example above sets the following permissions:

- read, write and execute for the **user**
- read, write and execute for the **group**
- read for **others**

Now you know how to read and change file and directory permissions on linux. If you need further information just consult the `man` page by typing `man chmod` into the command line.

If you have questions or feedback, please leave a comment :).