Whether you want to change file permissions in Linux (using chmod 777) or check user permissions in Linux using command; here is the tutorial for you.
File Permissions
There are three types of Linux file permissions: read, write, and execute.
- Read (r): Read permission is used to access the file’s contents. Users with read permission can view the contents of the file and folders.
- Write (w): Write permission is used to modify or change the contents of a file or a folder.
- Execute (x): Execute permission allows you to execute the contents of a file or to execute a file, such as a script or a program.
File permission octal values in Linux
In Linux, a three-digit value represents specific file permissions and these digital values are known as octal values. Octal values are base 8 numbers, and they use the numbers 0 to 7 to represent file permissions.
Each number corresponds to the read, write, and execute file permissions for the owner, group, and others, respectively.
Each permission has a numeric value assigned to it with the following meaning:
- r (read): 4
- w (write): 2
- x (execute): 1
See below given table:
| Octal Value | File Permissions Set | File Permissions Description |
|---|---|---|
| 0 | — | No permissions |
| 1 | –x | Execute permission only |
| 2 | -w- | Write permission only |
| 3 | -wx | Write and execute permissions |
| 4 | r– | Read permission only |
| 5 | r-x | Read and execute permissions |
| 6 | rw- | Read and write permissions |
| 7 | rwx | Read, write, and execute permissions |
Let us understand this by an example, in the permission value 744, the first digit (7) corresponds to the user, the second digit (4) to the group, and the third digit (4) to others.
By adding up the value of each user classification (7, 4 and 4), you can find the file permissions.
For example, the first digit (7) corresponds to the user and have read (4), write (2), and execute (1) permissions for its owner (4+2+1=7), and only read permission for all other users (4 for read).
See the following explanation for a better understanding:
- Owner: rwx = 4+2+1 = 7
- Group: r– = 4+0+0 = 4
- Others: r– = 4+0+0 = 4
How To Check User Permissions In Linux
The easiest way to view file permissions in Linux is using the ls -l command. For example:
ls -l EXAMPLE.txt
This produces output like:
-rw-r--r-- 1 SCP users 1024 Nov 15 11:20 EXAMPLE.txt
Let’s break down what each part means:
- -rw-r–r– – The permission string
- 1 – Number of hard links
- SCP – Owner of the file
- users – Group that owns the file
- 1024 – File size in bytes
- Nov 15 11:20 – Last modification date and time
- EXAMPLE.txt – File name
Understanding -rw-r–r– – The permission string
The permission string -rw-r–r– contains 10 characters. Let’s decode them position by position:
Position 1 – The first position is the File type:
- Regular file
- d Directory
- l Symbolic link
- c Character device
- b Block device
Positions 2-4 – Position nos. 2 to 4 are about the owner and user permissions:
- r Read permission
- w Write permission
- x Execute permission
Positions 5-7 – The Position nos. 5 to 7 are about the group permissions:
Positions 8-10 – Lastly the position nos. 8 and 9 and 10 are correspondingly about the other permissions.
So in the above example (see above) the permission string -rw-r–r– means:
- Regular file (-)
- Owner – read and write permission (rw-)
- Group – only read permission (r–)
- Others – only read permission (r–)
File Permissions In Linux – Chmod 777
In simple terms, the chmod command changes file permissions. The command chmod 777 directory_name or chmod 777 file_name changes the permissions of a directory or the file so that everyone (owner, group, and others) has read, write, and execute permissions. As we can see above that 777 means:
- 7 Owner rwx – Read, write, execute
- 7 Group rwx – Read, write, execute
- 7 Others rwx – Read, write, execute
Where r (4) = Read, w (2) = Write and x (1) = Execute. Which means, 4 + 2 + 1 = 7 = rwx and hence 777 = rwxrwxrwx for any given directory.
Recursive Permissions
To change permissions recursively for a directory and all of its contents, use the chmod command with the uppercase -R flag.
chmod -R 755 /path/to/directory
Caution Note – When you execute chmod 777 recursively grants read, write, and execute permissions to everyone (Owner, Group, and Others) for every single file and folder within that path (provided by the user).
Similarly, other most used common permission combinations are:
- 755 (
rwxr-xr-x) – Executable files and directories - 644 (
rw-r--r--) – Regular files - 600 (
rw-------) – Private files - 777 (
rwxrwxrwx) – Full access for each one
Everyone can do anything
Generally considered insecure
