The Linux chmod command is used to change the file and directory permissions in a Linux-based operating system. It allows users to define who can read, write, or execute a file by modifying the permission settings. Permissions are typically assigned to three categories: the owner of the file, the group to which the file belongs, and others (everyone else). The chmod command can be used with symbolic notation (e.g., chmod u+x filename to add execute permission for the user) or numeric notation (e.g., chmod 755 filename to set specific read, write, and execute permissions for each category). This command is essential for managing access control on a system, ensuring that files and directories have the appropriate security settings.
Please note that all examples and instructions mentioned in this tutorial have been tested on Ubuntu 24.04, and the chmod version we’ve used is 8.25.
Chmod basics
Consider the following ls command example:
The first column in the output is of our interest. Leave aside the initial ‘-‘ (which signifies the type of file). The remaining fields in the column can be broken down further as: rw- , rw- , and r–. These are the permissions that the file owner, the group the file belongs to, and others have when it comes to this file.
This means that the owner has both read (r) and write (w) permissions, as does the group. Others, however, only have read permissions for the file. Please note that we’re assuming the current user owns the file.
Now, suppose the requirement is to give everyone the right to execute this file. Here’s how this can be done:
chmod +x script.sh
Here are the permissions now:
The extra ‘x’ in permissions for owner, group, and others signifies that everyone now has execute access for this file.
However, more likely than not, you might not want everyone to have execute access for a file. What if the requirement is to grant only the owner/current user execute access to script.sh.
Well, for this, the first step now would be to take back execute access from everyone, something which you can do using the following command:
chmod -x script.sh
And then grant it explicitly to the owner:
chmod u+x script.sh
As you’d have guessed, ‘u+x’ says grant (+) the owner/current user (u) execute (x) access to the file. Similarly, for group, you can use ‘g’ and for others you can use ‘o’.
Please note that whenever you want to grant/revoke a common set of permissions to/from all, you can use ‘a’ instead of ‘ugo’. What I mean is, this:
chmod ugo-x script.sh
can be replaced by this:
chmod a-x script.sh
Also, remember that if none of these (‘u’, ‘g’, ‘o’, and ‘a’) is explicitly specified, then also the default is assumed to be ‘a’.
Moving on, if you want, you can also simply copy permissions granted to, say, the owner/current user and have them for the group or others. For this use the sign ‘=’.
For example, to copy owner/user permissions to group, use the following command:
chmod g=u script.sh
Another scenario could be to copy permissions for a particular file and have them for your file. For this, use the –reference command line option. Here’s the general template for using this command line option:
chmod --reference=[source-file] [destination file]
In the above command, source-file is the file whose permission bits you want to copy, and destination-file is the file whose permission bits you want to change.
Moving on further, there’s also a numerical notation (also known as octal representation) using which you can tell chmod to change permissions. There are three numbers you play with in this mode: 4, 2, and 1. While 4 is for read, the other two are for write and execute, respectively.
For example, consider the following example:
Now, suppose the task is to add execute permission for owner/user, remove write permission but add execute permission to group, and remove all permissions from others. This can be done as follows:
chmod 750 script.sh
In the above command, ‘7’ is for user, which is the result of 4+2+1 as the requirement for user is to have all permissions. Similarly, ‘5’ is for group, which the result of 4+0+1 as the requirement is to give only read and execute permissions to group. Finally, ‘0’ is for others, which is the result of 0+0+0, as the requirement is to revoke all permissions from others.
For those dealing with symbolic links, here’s something worth knowing:
chmod never changes the permissions of symbolic links; the chmod system call cannot change their permissions. This is not a problem since the
permissions of symbolic links are never used. However, for each symbolic link listed on the command line, chmod changes the permissions of
the pointed-to file. In contrast, chmod ignores symbolic links encountered during recursive directory traversals.
Command-line Reference
The chmod command in Linux has several command-line options that modify its behavior. Here is a list of the most commonly used options:
- -R, –recursive: This option applies the permission changes recursively to all files and directories within the specified directory. It is useful when you want to change the permissions of a directory and all its contents in one go.
Example: chmod -R 755 /path/to/directory
- -c, –changes: This option reports only when a change is made. If permissions are modified successfully, it outputs a message indicating the change.
Example: chmod -c 644 filename
- -v, –verbose: This option provides detailed information about every file processed, whether the permissions were changed or not.
Example: chmod -v 755 filename
- -reference=RFILE: Instead of specifying permissions directly, this option allows you to set the permissions of a file or directory to match another file or directory (RFILE).
Example: chmod –reference=file1 file2
- -f, –silent, –quiet: This option suppresses most error messages, making the command run without displaying output for errors.
Example: chmod -f 755 filename
These options allow chmod to be used flexibly in various scenarios, from single-file adjustments to large-scale directory permission changes.
Conclusion
Here, in this tutorial, we have discussed most of the basics related to this tool, and the examples we’ve discussed are aimed at making those basics clear.
Please note that our discussion mainly focused on files. You should keep some details in mind while using chmod with directories. For all that info (as well as more details on chmod), head to the tool’s man page.