Linux is a powerful operating system that maintains system security through file and directory permission management. Linux Command chmod is one of the most crucial. The chmod command is used to change the permissions of files or directories. In this post, we will explore the basics of using the chmod command, its various options, and practical applications.
Table of Contents
What is the Linux Command chmod?
Linux command chmod stands for ‘change mode’, and it is a command used to change the permissions of files or directories. In the Linux file system, permissions are categorized into three types: owner, group, and others. Each user group can have read, write, and execute permissions for a file.
Understanding File Permissions
In Linux, file permissions are divided into the following three types:
- Read Permission (read): Allows reading the contents of the file.
- Write Permission (write): Allows modifying the contents of the file.
- Execute Permission (execute): Allows executing the file.
These permissions apply to three user groups:
- Owner (user): The user who owns the file.
- Group (group): The group that the file belongs to.
- Others (others): All other users not in the owner or group.
Using the -l option of the Linux command ls, you can see permissions and user group information expressed as “drwxrwxr-x” or “-rw-rw-r–” at the beginning.
In the image below, you can see three sets of rwx. The first set is for the user (owner), the next is for the group, and the last is for all other users. Each rwx indicates read, write, and execute permissions, respectively.
How to Use the chmod Command
The Linux command chmod can change file permissions using either numeric or symbolic modes. Let’s look at both methods.
Numeric Mode
In numeric mode, each permission is represented by a three-digit number. rwx is expressed as a three-digit binary number. Each digit can have values 4 (read), 2 (write), and 1 (execute), and the sum of these values sets the permission.
As shown in the figure above, the decimal value of permissions is calculated using binary addition as follows:
- Read, write, execute permission (rwx): 4 + 2 + 1 = 7
- Read, write permission (rw-): 4 + 2 = 6
- Read, execute permission (r-x): 4 + 1 = 5
- Read permission (r–): 4 = 4
To grant the owner read, write, and execute permissions, and the group and others read and execute permissions using numeric mode, use the following command:
chmod 755 filename
ShellScriptIn the next figure, you can see the result of changing the permissions of the run.sh file from 664 to 755.
Symbolic Mode
In symbolic mode, permissions are expressed with letters:
- u: User (owner)
- g: Group
- o: Others
- a: All users
Permissions are granted (+) or removed (-) using symbols:
- r: Read
- w: Write
- x: Execute
To grant the owner read, write, and execute permissions, the group read and execute permissions, and remove the read permission for others using symbolic mode, use the following command:
chmod u+rwx,g+rx,o-r filename
ShellScriptThe result of this command is shown below. The user’s permission initially had rw but lacked x, which was added. The group’s permission had rw, and adding rx resulted in the inclusion of x. Finally, the others’ read permission was removed, leaving no permissions.
The difference from numeric mode is that numeric mode sets all rwx based on the given number, whereas symbolic mode adds or removes specific permissions using the + or – symbols. Thus, symbolic mode allows modifying only the desired permissions without affecting the entire set of file permissions.
Options of the chmod Command
The Linux command chmod has several useful options. Let’s look at some frequently used options.
-R (Recursive Application)
The -R option applies permissions recursively to a directory and all its subfiles and subdirectories. For example, to set 755 permissions for the mydir directory and all its contents, use the following command:
chmod -R 755 mydir
ShellScriptThe permissions of files under mydir were 664, but using the -R option, the mydir permissions changed from 775 to 755, and all files (a.sh, b.sh, c.sh) under mydir changed to 755.
–reference
The –reference option sets the permissions of a file based on another file’s permissions. For example, to set the permissions of file2 the same as file1, use the following command:
chmod --reference=file1 file2
ShellScriptThe figure below shows the result of setting the permissions of b.sh to be the same as a.sh using the –reference option.
Useful Applications
Granting Execution Permission to Script Files
To execute a shell script in Linux, it needs execution permission. Use the following command to grant execution permission to a script file:
chmod +x script.sh
ShellScriptYou can now execute the script using ./script.sh.
Setting Web Server File Permissions
Setting file permissions on a web server is crucial. Generally, files in the web server’s root directory have the following permissions:
- Directories: 755 (owner can read, write, and execute; group and others can read and execute)
- Files: 644 (owner can read and write; group and others can read)
To set permissions for the web server’s root directory www and its files, use the following commands:
chmod -R 755 /var/www
find /var/www -type f -exec chmod 644 {} \;
ShellScriptThe second line uses the find command to locate files in /var/www. The -type f option ensures only files are found, and the -exec chmod option sets the found files’ permissions.
Precautions
Incorrect Permission Settings
Incorrect permission settings can cause security issues. For example, granting write permission to all users can lead to severe security problems, potentially damaging the system.
Using sudo
When changing the permissions of system files, you need to use the sudo command to gain administrative privileges.
sudo chmod 644 /etc/passwd
ShellScriptSummary
The Linux command chmod is an essential tool for managing file and directory permissions in Linux. You can set permissions using numeric or symbolic modes, and manage them efficiently with recursive application or reference options. Proper permission settings are crucial for maintaining system security, so always configure them carefully. Utilize the chmod command to maintain a secure and efficient Linux environment.
This concludes the explanation of how to use the chmod command and its options. We hope this post helps you in using Linux effectively.