How to Use Linux Command chmod and Its 2 Options

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.

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.

Figure 1. Checking file permissions using the Linux command ls
Figure 1. Checking file permissions using the Linux command ls

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.

Figure 2. Meaning of file permissions in Linux
Figure 2. Meaning of file permissions in Linux

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.

Figure 3. Binary, decimal, and character representation of Linux file permissions rwx
Figure 3. Binary, decimal, and character representation of Linux file permissions rwx

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
ShellScript

In the next figure, you can see the result of changing the permissions of the run.sh file from 664 to 755.

Figure 4. Linux Command chmod: Setting permissions using numeric mode
Figure 4. Linux Command chmod: Setting permissions using numeric mode

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
ShellScript

The 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.

Figure 5. Linux Command chmod: Setting permissions using symbolic mode
Figure 5. Linux Command chmod: Setting permissions using symbolic mode

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
ShellScript

The 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.

Figure 6. Linux Command chmod: Setting permissions recursively using the -R option
Figure 6. Linux Command chmod: Setting permissions recursively using the -R option

–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
ShellScript

The figure below shows the result of setting the permissions of b.sh to be the same as a.sh using the –reference option.

Figure 7. Linux Command chmod: Setting permissions using the --reference option
Figure 7. Linux Command chmod: Setting permissions 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
ShellScript

You 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 {} \;
ShellScript

The 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
ShellScript

Summary

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.

References

Leave a Comment