In this blog post, we will discuss two ways to change file permissions in Linux. The first way is through the use of the chmod command, and the second way is through the use of the graphical interface. We will also discuss what file permissions are and why they are important. Let’s get started!
Understanding file permissions in Linux
File permissions are important because they determine who can access and modify files on your Linux system. By default, only the owner of a file can access and modify it. However, you can change the permissions of a file to allow other users to access and modify it. This is where the chmod command comes in.
Note the multiple instances of r, w, and x. These are grouped into three sets that represent different levels of ownership:
Owner or user permissions: After the directory (d) slot, the first set of three characters indicate permission settings for the owner (also known as the user).
- In the example -rw-r–r–, the owner permissions are rw-, indicating that the owner can read and write to the file but can’t execute it as a program.
- In the example drwxr-xr-x, the owner permissions are rwx, indicating that the owner can view, modify, and enter the directory.
Group permissions: The second rwx set indicates the group permissions. In the fourth column of the example above, group1 is the group name.
- In the example -rw-r–r–, group members can only read the file.
- In the example drwxr-xr-x, group members can view as well as enter the directory.
Other permissions: The final rwx set is for “other” (sometimes referred to as “world”). This is anyone outside the group. In both examples above, these are set to the same permissions as the group.
change file permissions with chmod command in Linux
To change the permissions of a file in Linux, we can use chmod command. The syntax for the chmod command is as follows:chmod [options] mode file. The options are optional. The mode is used to specify the new permissions for the file. The file is the path to the file whose permissions will be changed.
To set a file, so it is public for reading, writing, and executing, the command is: chmod u=rwx,g=rwx,o=rwx [file_name]
To set permission as in the previously mentioned test.txt to be:
- read and write for the user
- read for the members of the group
- read for other users
Use the following command: chmod u=rw,g=r,o=r test.txt
We have another format. To change file permissions, use the command chmod (change mode). The owner of a file can change the permissions for user (u), group (g), or others (o) by adding (+) or subtracting (-) the read, write, and execute permissions.
For example, let’s say we have a file called test.txt in our home directory. The current permissions of this file are shown below: -rw-r–r–
The first column shows the file type and permissions for the owner of the file. The second column shows the file type and permissions for the group that the file belongs to. The third column shows the file type and permissions for all other users. In this case, the owner has read and write permission for the file, the group has read permission for the file, and all other users have read permission for the file.
Now, let’s say we want to change the permissions of this file so that the owner has read, write, and execute permission; the group has read and execute permission; and all other users have read and execute permission. We would use the following command: chmod a+rx test.txt
The a option specifies that the permissions should be changed for all users. The + sign indicates that the permission should be added, and the rx letters indicate that the read and execute permissions should be added.
After running this command, the permissions of our test.txt file should look like this: -rwxr-xr-x
You can also specify multiple classes and types with a single command. For example, to remove read and write permission for group and other users (leaving only yourself with read and write permission) on a file named myfile, you would enter: chmod go-rw myfile
You can also specify that different permissions be added and removed in the same command. For example, to remove write permission and add execute for all users on myfile, you would enter: chmod a-w+x myfile
Multiple permissions can be specified by separating them with a comma, as in the following example: chmod g+w,o-rw,a+x ~/example-files/
change file permissions with chmod command + numeric format in Linux
The other way to use the chmod command is the octal/numeric format, in which you specify a set of three numbers that together determine all the access classes and types. Rather than being able to change only particular attributes, you must specify the entire state of the file’s permissions.
- r(ead) has the value of 4
- w(rite) has the value of 2
- (e)x(ecute) has the value of 1
- no permission has the value of 0
The privileges are summed up and depicted by one number. Therefore, the possibilities are:
- 7 – for read, write, and execute permission
- 6 – for read and write privileges
- 5 – for read and execute privileges
- 4 – for read privileges
You can think of the three digit sequence as the sum of attributes you select from the following table:
Example:
- Read by owner 400
- Write by owner 200
- Execute by owner 100
- Read by group 040
- Write by group 020
- Execute by group 010
- Read by others 004
- Write by others 002
- Execute by others 001
Add the numbers of the permissions you want to give; for example:For file myfile, to grant read, write, and execute permissions to yourself (4+2+1=7), read and execute permissions to users in your group (4+0+1=5), and only execute permission to others (0+0+1=1), you would use: chmod 751 myfile
To grant read, write, and execute permissions on the current directory to yourself only, you would use: chmod 700
change file permissions with graphical interface in Linux
You can also change file permissions through the graphical interface. Most Linux distributions come with a file manager that allows you to change the permissions of files and directories. For example, in the GNOME desktop environment, you can open the Files application and select a file or directory. Then, click the Permissions tab to change the permissions.
Examples of Common file Permissions with chmod
chmod 600 (rw——-): 600 permissions means that only the owner of the file has full read and write access to it. Once a file permission is set to 600, no one else can access the file. Example chmod commands (in octal and symbolic notions) setting permissions to 600:
- chmod 600 example.txt
- chmod u=rw,g=,o= example.txt
- chmod a+rwx,u-x,g-rwx,o-rwx example.txt
chmod 664 (rw-rw-r–): 664 (rw-rw-r–) enables the following permissions: read and write for the owner; read and write for the group; read for others. If you trust other users within the same group and everyone needs write access to the files, this is a common setting to use. Otherwise 644 permissions can be used to restrict write access to the group. Example chmod commands (in octal and symbolic notions) setting permissions to 664:
- chmod 664 example.txt
- chmod u=rw,g=rw,o=r example.txt
- chmod a+rwx,u-x,g-x,o-wx example.txt
chmod 777 (rwxrwxrwx): chmod 777 is used to grant permissions to everyone to read, write, and execute a file. While using these permissions is a quick way to overcome a permissions-based error, it’s not a best practice for securing most files and applications. Example chmod commands (in octal and symbolic notions) setting permissions to 777:
- chmod 777 example.txt
- chmod u=rwx,g=rwx,o=rwx example.txt
- chmod a=rwx example.txt
In this blog post, we have discussed three ways to change file permissions in Linux. We hope you have found this information to be helpful! If you have any questions, please feel free to post them in the comments section below. Thanks for reading!