
- Unix Commands Reference
- Unix Commands - Home
chmod Command in Linux
chmod is a Linux command that you can use on the terminal to change files and directory permissions. It is a versatile and powerful command that determines who can read, write, or execute directories and files.
With the chmod command, you will be able to control access levels, grant, or revoke permissions for the owner, group and others. It is extremely useful for protecting your important data from unauthorized users.
Table of Contents
Here is a comprehensive guide to the options available with the chmod command −
- Installation of chmod in Linux
- Syntax for chmod Command in Linux
- Different Available Options for chmod Command
- Examples of chmod Command in Linux
Installation of chmod in Linux
The chmod command is typically preinstalled on most Linux operating systems, because it is a part of coreutils packages of the system. However, if you fail to find it, then install the coreutils package depending on the system you are using.
For example, if you are using Debian based distributions like Ubuntu, simply use the apt package manager to install coreutils package on the system −
sudo apt install coreutils
If you are using CentOS, Fedora and similar RHEL operating systems, you can use the yum command provided below to install the coreutils package.
sudo yum install coreutils
Syntax for chmod Command in Linux
The basic syntax to use the chmod command in Linux is given below −
chmod [options] permissions filename
Here,
- [options] − Optional flags that change the chmod command behavior.
- permissions − When setting permissions in Linux, you can use either a three-digit octal number or symbolic notation.
- filename − The file or directory name for which the permissions need to be changed.
Different Available Options for chmod Command
The following table provides the available options you can use with the chmod command to change its behavior.
Option | Description |
---|---|
-c, --changes | Reports changes only when a modification is made. |
-f, --silent, --quiet | Suppresses most error messages. |
-R, --recursive | Changes permissions recursively (applies to files and directories). |
-v, --verbose | Outputs a diagnostic message for every file processed. |
--help | Shows help information and exits. |
--version | Displays version information and exits. |
Examples of chmod Command in Linux
Let’s explore different examples of chmod command in Linux −
- Assign Read Permission to a File
- Assign Execute Permissions to a File and Group Owner
- Assign Different Permissions to File, Group and Others
- Recursively Assign Permissions to Directories
- Remove All Permissions for Other Users
- Add Read/Write Permissions for All Users
- Make a Script Executable
- Restrict Permissions for Sensitive Files
Assign Read Permission to a File
One of the basic functions of the chmod command is to grant read permission to a file, so that the users can view its contents. For example, to give the file’s owner read permission, you can use the following command −
chmod u+r filename.txt
You can replace the filename.text with your desired file name. The u+r is the user read permission given to the specified file.
Assign Execute Permissions to a File and Group Owner
With the chmod command, you can also grant execute permissions to a file, and enable users to run it as a program or script. Additionally, you can specify permissions for both the owner and the group.
For example, to allow the owner and group members to execute a script called my_script.sh, you can use the following command −
chmod ug+x my_script.sh
Here, ug+x stands for user and group execute permission.
Where,
- u − Represents the user or owner of the file.
- g − Represents the group to which the file belongs
- +x − Adds the execute permission to the specified file.
Assign Different Permissions to File, Group, and Others
The chmod command also allows you to set specific permissions for different categories, such as owner (user), group, and others. You can use either octal notation (644) or symbolic notation (u=rw, g=rx, o=r) to define these permissions.
For example, to give the file’s owner read and write permissions (rw), allow group members to read and execute (rx), and grant read-only access to others (r), run the below-given command −
chmod u=rw,g=rx,o=r filename.txt
Recursively Assign Permissions to Directories
Sometimes you may need to change permissions for an entire directory and all of its contents. You can do this by using the chmod command with -R option that allows you to apply the command recursively. For example, to make all files and subdirectories within a directory (my_dir) readable, writable, and executable by the owner, and readable and executable by the group and others, use −
chmod -R 755 my_dir
Remove All Permissions for Other Users
Sometimes you may restrict access to a file, and deny all permissions to non-owners (others). For example, to remove all permissions for others (users who are not the owner or group part), you can use the following command −
chmod o= filename.txt
Add Read / Write Permissions for All Users
In some cases, you may want to allow everyone (the owner, group members, and others) to read from and write to a file. Let’s say to grant read and write permissions to all users, we can use the chmod command in the following way −
chmod a+rw filename.txt
Here, a+rw stands for add read and write permissions for all users.
Where,
- a − Refers to all users, including the owner, group members, and others.
- +rw − Adds both read (r) and write (w) permissions.
Make a Script Executable
When you have a Python or shell script, you can use the chmod command to make the script executable and run it directly from the command line. For example, to enable execution of a script called my_script.sh, you can use the following command −
chmod +x my_script.sh
Once you have done it, you will be able to run the script on the terminal.
Restrict Permissions for Sensitive Files
Sometimes you may want to limit access to sensitive files, such as private keys or configuration files. For example, to ensure that only the owner can read and write a sensitive file (e.g., private_key.pem), simply run the below-given command −
chmod 600 private_key.pem
When you set the permissions of a file to 600, it means the following −
Owner Permissions
- The owner of the file has full authority to read and write access to it.
- They can open the file, view its content, edit it, and modify it.
Group Permissions and Others Permissions
- -No permissions are granted to group members or anyone else.
- -Group members and others cannot access the file in any way.
This way you can use the chmod command to perform different activities on your system.
Conclusion
chmod is a versatile command that can be used for changing files and directory permissions. This article has provided the syntax of chmod command along with different options that you can use with the command. Further, examples are provided to help users learn the basics of chmod command and how to change permission on different files and directories.