Skip to content

Modifying Permissions

Changing permissions can easily be done with the chmod command.

First, pick which permission set you want to change: user, group, or other. You can add or remove permissions with a + or -. Let's look at some examples.

Adding permission bit on a file:

$ chmod u+x myfile

The above command reads like this: change permission on myfile by adding executable permission bit on the user set. So now the user has executable permission on this file!

Removing permission bit on a file:

$ chmod u-x myfile

Adding multiple permission bits on a file:

$ chmod ug+w

There is another way to change permissions using numerical format. This method allows you to change permissions all at once. Instead of using r, w, or x to represent permissions, you'll use a numerical representation for a single permission set. No need to specify the group with g or the user with u.

The numerical representations are seen below:

  • 4: read permission
  • 2: write permission
  • 1: execute permission

Let's look at an example:

$ chmod 755 myfile

Can you guess what permissions we are giving this file? Let's break this down. The number 755 covers the permissions for all sets. The first number (7) represents user permissions, the second number (5) represents group permissions, and the last (5) represents other permissions.

Wait a minute, 7 and 5 weren't listed above. Where are we getting these numbers? Remember we are combining all the permissions into one number now, so you'll need to do some simple math.

  • 7 = 4 + 2 + 1: user permissions (read, write, execute)
  • 5 = 4 + 1: group permissions (read, execute)
  • 5 = 4 + 1: other users have read and execute permissions

One thing to note: it's not a great idea to change permissions without thought. You could potentially expose a sensitive file for everyone to modify. However, many times you legitimately need to change permissions—just take precautions when using the chmod command.


Exercise

Change some basic text file permissions and observe the permission bits changing as you run ls -l.


Interactive Quiz

1. What number represents the read permission when using numerical format?




2. What does the +x option do in chmod u+x myfile?




3. Which command will add write permission for both the user and the group?





Summary

  • Changing permissions is an important aspect of Linux file management.
  • Use the chmod command to modify permissions in symbolic or numerical formats.
  • Always double-check file permissions to avoid exposing sensitive files.