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 thechmodcommand, 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.”

A stylized image of a key on a keyboard

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 an Ubuntu LTS install but you could run this how-to on aRaspberry Pi. All of the how-to is performed via the Terminal. You can open a terminal window on most Linux machines by pressingctrl, altandt.

Change Permissions on the Linux Command Line

How to Check File Permissions in Linux

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 uselswith the-largument added.

1.Create a new directory calledtest_directory

Change Permissions on the Linux Command Line

2.Move into the newly created directory.

Get Tom’s Hardware’s best news and in-depth reviews, straight to your inbox.

3.Create a new test file calledtest1.txt.

4.List the contents of the directory usingls -l.

Usingls -lgives us a lot more information about the items returned on the list. We should see thattest1.txthas 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 ranls -land a directory was listed this first character would be ad. The next 9 characters are arranged in 3 sets of 3.

Change Permissions on the Linux Command Line

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 may 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 ourtest1.txtcurrently 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.

Change Permissions on the Linux Command Line

How to Change Linux File / Directory Permissions Quickly

We can use thechmodcommand 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 thetest_directory,list the current permissions fortest1.txt.

These should be unchanged from when we createdtest1.txtand should read-rw-rw-r– .2.Change the permission of the owner to read only.

3.List the directory contentsto view the new permission settings. We should now see that the permissions fortest1.txtread -r–rw-r– indicating that, for the owner, the file is now read only.

Jo Hinchliffe

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 argumentg-w.

5.List the directory contentsto view the new permission settings.

We should now see that the permissions fortest1.txtread -r–r–r– indicating that, for the groups, the file is now read only.

6.Enable write permissions for the owner.Instead of usingu-wto remove write permissions we can intuitively useu+wto grant write permissions for the owner.

7.List the directory contentsto view the new permission settings. We should now see that the permissions fortest1.txtread -rw-r–r– indicating that, for the owner, write permissions have been granted.

How to Apply Multiple File / Directory Permission Changes in Linux

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 thechmodarguments as this will cause the command to fail.

In the first section we useduandgforownerandgroupand in this section we will additionally useoto target permission changes forothers.Likewise, we usedrandwforreadandwriteand in this section we will addxto make changes to the executable permissions.

1.List the directory contentsto view the new permission settings. We should see that the permissions fortest1.txtare -rw-r–r– .

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 combiner,w,xin a single argument.

3.List the directory contents to view the new permission settings.We should see that the permissions fortest1.txtare -rwxrwxr– . This means that theownerandgroupcan read, write and execute the file whilst others can only read.

How to Change File / Directory Permissions Recursively in Linux

Thechmodcommand 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-Rargument to see how this works.

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

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 bothtest_directoryand the file,test1.txtit contains.

3.List the home directory contentsto check the permissions fortest_directory.

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 intotest_directoryto check permissions fortest1.txt.

We should see that the owner and group permissions fortest1.txthave 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 thegroupscommand.

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

2.Open a terminal and type ingroupsfollowed 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.

The output of this command looks like this.

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 777gives all three types full read, write and execute permissions whilechmod 740gives the user full permissions, the group read permissions and others no permissions at all.

0

1

2

3

Execute and Write

4

5

Read and Execute

6

Read and Write

7

All: Read, Write and Execute

With these basic usages of thechmodcommand you get a lot of control over file and directory permissions. There are lots of different arguments to add tochmodthat 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 aboutchmodit’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.