Become chmod pro in 5 minutes

2 minute read

1. Directory or file can have 3 types of users

    u = User (owner)
    g = Group (members to which the file belongs)
    o = Others (rest of the world)

    a = All of the above

2. Directory or file can have 3 types of permissions

    r = Read
    w = Write
    x = Execute

      = (blank) None, no permissions

3. Users can be assigned various permissions using 3 types of operations

    + Add permission (permission is added to existing permissions)
    - Remove permission
    = Re-assign (set permission and remove other permissions)

Using = means we remove/wipe-out existing permission and re-assign new permissions

The above 3 points are enough for us to understand what chmod is doing. Let’s dive into some examples to decrypt chmod’s actions -

EXAMPLES

Set Permissions:

chmod u=r file1.txt// User has read only permission

chmod g=rw file1.txt// Group has read and write

chmod o= file1.txt   // Others have no permission

Add/Remove Permissions:

chmod u+x file1.txt// Add execute to user

chmod g-w file1.txt// Remove write from group

chmod o+r file1.txt// Add read to others

Multiple Assignments: Use ,

chmod u=rwx,g+rx,o= file1.txt

// User has read/write/execute; Add read/execute to Group; Others None

Note: No space beteween commas

Multiple Files:

chmod o-r *.txt// Remove read from others to all the text files

chmod -R o-r *.txt// Include files in sub-directories (-R means RECURSIVE); Remove read from others to all the text files

QUIZ

Question         Answer
chmod u=rwx file // User has all permissions for this file
   
chmod g-x file // Remove execute permission from group
   
chmod u=x,g-w,o=r file // User has execute; remove write from Group; Others have read permissions
   
chmod a+x file // Add execute permissions to All users
   
chmod +x file // If no user is mentioned then by default it takes a(all) i.e Add execute permissions to All users

Hey, don’t you think it was easy!!!

BONUS

OCTAL/NUMERICAL shorthand is more common among core users, so let’s learn that as well.

In octal notation, the first number represents user permission, the second number represents group permission and the third number represents the others permission.

Syntax:

chmod ugo file # one octal mapped to each user

Example:

chmod 750 file1.txt

Here 7 is for user; 5 is for group; 0 is for others. The permission for this file are rwx r-x ---.

7 means read, write, execute 5 means read and execute 0 means no permission

Below table clarifies how 7 means rwx and 5 mean r-x

Octal Binary Permission
0 000 ---
1 001 --x
2 010 -w-
3 011 -wx
4 100 r--
5 101 r-x
6 110 rw-
7 111 rwx

As you might have already noticed, 0 means no permission, while 1 means r, w or x depending on its position.

What does this mean -

chmod 744 file1.txt

// 7 - User can read, write, execute

// 4 - Group can read

// 4 - Owner can read

Lastly, the first notation when we check the permission of a file or directory using the command ls -l is for either a file or directory.

- means file

d means directory

Example:

alt text

Updated:

Leave a comment