Skip to content

File Permissions

chmod is used to change file permissions.

Permission Groups

Each file in Linux has 3 permission groups.

  • Owner - Apply to the owner of the file
  • Group - Apply to the user group assigned to the file
  • Others - Apply to all other users

Viewing the Permissions

A simple way to view the file permissions is to use ls -l command whereas -l flag stands for use a long listing format.

$ ls -l
total 16
drwxrwxr-x 5 wtongze wtongze 4096 Sep  3 08:42 docs
-rw-rw-r-- 1 wtongze wtongze 1554 Sep  3 09:54 mkdocs.yml
-rw-rw-r-- 1 wtongze wtongze  524 Aug 28 08:21 requirements.txt
drwxrwxr-x 7 wtongze wtongze 4096 Sep  3 11:44 site
-rwxrw-r-x
│││││││└┴┴ Others (x1)
││││└┴┴ Group (x10)
│└┴┴ User (x100)
└ File type
Permission Meaning Value
r Read 4
w Write 2
x Execute 1

The above permission can also be writen as 0765 because: $$ (4 + 2 + 1) * 100 + (4 + 2) * 10 + (4 + 1) * 1 = 0765 $$

Info

Permission for directories often permit execute for all users in order to allow search for access to directory meta information and change working directory.

Leftmost digit in the 4-digit permission can also have the following attributes1:

Meaning Value
Set User ID 4
Set Group ID 2
Sticky 1

Changing the Permissions

chmod command is used to change the permissions for files.

Permission Group Shorthand

  • u - User
  • g - Group
  • o - Others
  • a - All users

Examples

chmod a+x example.sh   # Allow all uses to execute
chmod o-x example.sh   # Disallow others to execute
chmod 700 private.txt  # Only file owner can read, write and execute
chmod -R 600 folder/   # Change permission recursively