Skip to main content

How To Change File or Directory Permissions in Linux

Change Permissions on the Linux Command Line
(Image credit: Tom's Hardware)

If you’ve ever tried to run a script from the command line in Linux and gotten an error message saying that it’s not executable or attempted to enter a directory only to be blocked by the system, you probably don’t have permissions to do those things. Fortunately, if you have the proper rights or the ability to act as a super user (accessible by using sudo), you can change the permissions on files and directories.

In this how-to we’ll look at the chmod command, a powerful command that can change file and directory permissions for the owner, user group members and others. In a section below, we’ll also explain how to tell what group your user is in and exactly what Linux means by “others.”

Whilst you become accustomed to these commands it’s good to work with example empty files and directories and you should take extra care to ensure you are carefully following the instructions.

All the commands in this how-to will work on most Linux machines. We’ve used a Ubuntu 20.04 install but you could run this how-to on a Raspberry Pi. All of the how-to is performed via the Terminal. You can open a terminal window on most Linux machines by pressing ctrl, alt and t.

How to Check File Permissions in Linux

(Image credit: Tom's Hardware)

To begin, let's create a test file in a test directory and take a look at its default permissions. To see the permissions we will use ls with the -l argument added.

1. Create a new directory called test_directory

$ mkdir test_directory

2. Move into the newly created directory.

$ cd test_directory

3. Create a new test file called test1.txt.

$ touch test1.txt

4. List the contents of the directory using ls -l.

$ ls -l

Using ls -l gives us a lot more information about the items returned on the list. We should see that test1.txt has been created. We are interested in the first 10 characters on the listing which for our test file read - rw- rw- r--

The first – indicates that the object on the list is a file. If we ran ls -l and a directory was listed this first character would be a d. The next 9 characters are arranged in 3 sets of 3. 

The first set of 3 refers to the owner, the second set of 3 refer to user groups and the final set of three refer to others. Each set of 3 characters can contain either – or r, w, and x. If you can see an r in the set then that set has read permissions granted. If you can see a w that set can write to a file and if you can see an x in the set then that set can execute the file as a script or program. 

We can see that our test1.txt currently has the owner and group member permissions set to read and write with others only allowed to read. No one has permission to execute the file. 

How to Change Linux File / Directory Permissions Quickly

(Image credit: Tom's Hardware)

We can use the chmod command to toggle the read, write and execute permissions on and off for the owner, group and others. Let’s begin with changing single permissions for the owner and group.

1. In the test_directory, list the current permissions for test1.txt.

$ ls -l


These should be unchanged from when we created test1.txt and should read -rw-rw-r-- .

2. Change the permission of the owner to read only.

$ chmod u-w test1.txt

3. List the directory contents to view the new permission settings. We should now see that the permissions for test1.txt read -r--rw-r-- indicating that, for the owner, the file is now read only.

$ ls -l

4. Change  permission of groups to read only. Similar to changing permissions for the owner we can change permission settings for the groups. To revoke write permissions we can use the argument g-w.

$ chmod g-w test1.txt

5. List the directory contents to view the new permission settings.

$ ls -l

We should now see that the permissions for test1.txt read -r--r--r-- indicating that, for the groups, the file is now read only.

6. Enable write permissions for the owner. Instead of using u-w to remove write permissions we can intuitively use u+w to grant write permissions for the owner.

$ chmod u+w test1.txt

7. List the directory contents to view the new permission settings. We should now see that the permissions for test1.txt read -rw-r--r-- indicating that, for the owner, write permissions have been granted.

$ ls -l

How to Apply Multiple File / Directory Permission Changes in Linux

(Image credit: Tom's Hardware)

We can also combine the arguments we used in the previous section to make multiple changes to Linux file permissions in a single command. In this section it’s important not to add any extra spaces in the chmod arguments as this will cause the command to fail. 

In the first section we used u and g for owner and group and in this section we will additionally use o to target permission changes for others. Likewise, we used r and w for read and write and in this section we will add x to make changes to the executable permissions.

1. List the directory contents to view the new permission settings. We should see that the permissions for test1.txt are -rw-r--r-- .

$ ls -l

2. Change the permissions so that additionally the owner can execute and the group can additionally write and execute. Notice that there are no spaces after the comma and also notice that you can combine r,w,x in a single argument.

$ chmod u+x,g+wx test1.txt

3. List the directory contents to view the new permission settings. We should see that the permissions for test1.txt are -rwxrwxr-- . This means that the owner and group can read, write and execute the file whilst others can only read.

$ ls -l

How to Change File / Directory Permissions Recursively in Linux

(Image credit: Tom's Hardware)

The chmod command can be used to create changes recursively to a directory meaning that changes are also applied to the files contained within the directory. Let’s use what we have learnt so far and additionally use the recursive -R argument to see how this works.

1. Move to your home directory and list the contents.

$ cd
$ ls -l

We should see test_directory listed from the previous parts of this how-to. The permissions for test_directory should read drwxrwxr-x.

2. Change the owner and group permissions of both the directory and its contents. Running this command will revoke owner and group write permissions for both test_directory and the file, test1.txt it contains.

$ chmod -R u-w,g-w test_directory

3. List the home directory contents to check the permissions for test_directory.

$ ls -l

We should see that the owner and group permissions allow for reading and execution but now do not allow writing to the directory.

4. Move into test_directory to check permissions for test1.txt.

$ cd test_directory
$ ls -l

We should see that the owner and group permissions fortest1.txt have been changed to match the recursive changes to the host directory, removing write permissions. 

How to View Your Linux Group

When we talk of users, groups and others what we mean is that our user typically belongs to a group of users. A user and group can have the same, or very different permissions. For example a team member may need more permissions to perform a certain task. The permissions that we give a user and a group will be different to what we give other users, users who are not in the group.

We can see the groups that our user is part of via the groups command.

1. Open a terminal and type in groups. This will list all of the groups available on our installation.

groups

2. Open a terminal and type in groups followed by the user’s name. For example here we check which groups “Tom” belongs to and find that he belongs to the groups tom and sudo.

groups tom

The output of this command looks like this.

tom : tom sudo

Others are not a group. Rather “others” refers to anyone who is not the owner, or in a group which has access to a file or directory. Typically others will only have read access to any files of directories, but this can be changed and we shall explore this later.
 

How to Change Linux File Permissions With Numeric Codes

Though the use of r,w or x is easier to remember for Linux file permissions, many people use a series of numeric codes with chmod instead. You feed the chmod command a three-digit number and each digit applies to a different group in this order: user, group, others. So, for example, chmod 777 gives all three types full read, write and execute permissions while chmod 740 gives the user full permissions, the group read permissions and others no permissions at all.

The table below shows what each number means.

NumberPermissions
0None
1Execute
2Write 
3Execute and Write 
4Read
5Read and Execute
6Read and Write
7All: Read, Write and Execute

With these basic usages of the chmod command you get a lot of control over file and directory permissions. There are lots of different arguments to add to chmod that allow you to work with different approaches. 

For example it’s worth researching the use of = instead of + and – as, rather than toggling permissions on and off, you can define permissions directly for some or all users. As you research and learn about chmod it’s worth remembering to practice on test files and directories as it can be frustrating if you accidentally remove all permissions on a file you depend on. 

Jo Hinchliffe is a UK-based freelance writer for Tom's Hardware US. His writing is focused on tutorials for the Linux command line.