How to Change File or Directory Permissions in Linux
If you have ever tried to run a script from the command line in Linux and received an error message that it is not executable or you tried to enter a directory only to be blocked by the system, you probably don’t have the permissions to do so. things. Fortunately, if you have the appropriate rights or the ability to act as a super user (accessible using sudo), you can change file and directory permissions.
In this guide, we will look at the chmod command, a powerful command that can change file and directory permissions for owner, usergroup members, and others. In a section below, we’ll also explain how to find out which group your user is in and exactly what Linux means by “others”.
While you get used to these commands, it’s good to work with sample empty files and directories, and you should take extra care to make sure you follow the instructions carefully.
All commands in this tutorial will work on most Linux machines. We used an Ubuntu 20.04 install, but you can run this guide on a Raspberry pie. All procedures are performed through 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
To begin, let’s create a test file in a test directory and examine its default permissions. To see the permissions we will use ls with the -I 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 directory contents using ls -l.
$ ls -l
Using ls -l gives us much more information about the returned items on the listing. We should see this test1.txt has been created. We are interested in the first 10 characters in the list which, for our test file, read – rw- rw- r–.
The first – indicates that the object in the list is a file. If we ran ls -l and a directory was listed that 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 refers to user groups, and the last set of three refers to others. Each set of 3 characters can contain either – or r, w and x. If you can see an r in the set, it means read permissions have been granted to this set. If you can see aw that this set can write to a file and if you can see an x in the set then this set can run the file as a script or program.
We can see that our test1.txt currently has 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 Quickly Change Linux File/Directory Permissions
We can use the chmod command to enable and disable read, write, and execute permissions for owner, group, and others. Let’s start by modifying the individual permissions for the owner and the group.
1. In the test_directory, list the current permissions for test1.txt.
$ ls -l
These should be unchanged since creation test1.txt and should read -rw-rw-r– .
2. Change owner permission to read-only.
$ chmod u-w test1.txt
3. List 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 group permission to read-only. Similar to changing permissions for owner, we can change permission settings for groups. To revoke write permissions, we can use the argument gw.
$ chmod g-w test1.txt
5. List 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 groups the file is now read-only.
6. Enable write permissions for the owner. Instead of using uh to remove write permissions we can intuitively use u+w to grant write permissions to the owner.
$ chmod u+w test1.txt
7. List 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
We can also combine the arguments we used in the previous section to make multiple Linux file permission changes in a single command. In this section, it is important not to add extra spaces in the chmod arguments as this will cause the command to fail.
In the first section, we used you and g for owner and group and in this section we will additionally use oh 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 executable permissions.
1. List 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 the owner can also execute and the group can also write and execute. Note that there is no space after the comma and also note that you can combine r, w, x in a single argument.
$ chmod u+x,g+wx test1.txt
3. List the contents of the directory 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 while others can only read.
$ ls -l
How to Change File/Directory Permissions Recursively in Linux
the chmod The command can be used to create changes recursively in a directory, which means that the changes are also applied to files inside the directory. Let’s use what we’ve learned so far and additionally use the recursive -R argument to see how it works.
1. Move to your home directory and list the contents.
$ cd $ ls -l
We should see test_directory listed in earlier parts of this guide. Permissions for test_directory should be drwxrwxr-x.
2. Modify the owner and group permissions of 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 contents of the home directory to check the permissions of test_directory.
$ ls -l
We should see that the owner and group permissions allow reading and executing, 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 made to the host directory, removing write permissions.
How to view your Linux group
When we talk about users, groups and others, we mean that our user usually belongs to a user group. A user and a group can have the same or very different permissions. For example, a team member might need more permissions to perform a certain task. The permissions we grant to a user and group will be different from those we grant to other users, users who are not part of the group.
We can see the groups our user is part of through the groups order.
1. Open a terminal and type groups. This will list all groups available on our installation.
2. Open a terminal and type groups followed by the username. For example here we check which groups “Tom” belongs to and find that he belongs to tom and sudo groups.
The output of this command looks like this.
tom : tom sudo
The others are not a group. Instead, “others” refers to anyone who is not the owner, or in a group who has access to a file or directory. Generally others will only have read access to all directory files, but this can be changed and we will explore this later.
How to Change Linux File Permissions with Numeric Codes
Although using 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, mod 777 gives all three types full read, write, and execute permissions while mod 740 gives the user full permissions, the group read permissions, and the others no permissions at all.
The table below shows the meaning of each number.
|3||Run and Write|
|5||read and run|
|6||Read and write|
|7||Everything: read, write and execute|
With these basic uses of chmod command you get a lot of control over file and directory permissions. There are many different arguments to add to chmod that allow you to work with different approaches.
For example, it is worth researching the use of = instead of + and – because, rather than enabling and disabling permissions, you can set permissions directly for some or all users. As you research and learn chmod Don’t forget to practice on test files and directories, as it can be frustrating if you accidentally remove all permissions on a file you depend on.