chmod command in Linux

chmod-command.jpg

All files and directories in Linux have permissions associated with them, which provide them security from the outside world. But are all the permissions associated permanently, or can they be changed? The answer is yes, the permissions can be modified with the help of the chmod command in Linux. The chmod command in Linux is very helpful for administrators and users who work with different users and need some files and directories to be accessed by specific people. In this guide, we will learn about permissions in Linux in detail, with the available options.

Table of Contents:

What is the chmod command in Linux?

The chmod command (change mode) is a Linux command used to modify the permissions of a file or directory by setting or removing access rights for users, groups, and others. It modifies the permission bits of a file or directory as stored by the Linux file system.

Syntax of the chmod command in Linux

Below is the syntax of the chmod command in Linux.

chmod [options] mode file_or_directory
  • mode refers to the permission setting (symbolic or numeric)
  • file_or_directory is the name of the file or directory to modify
Master DevOps Today - Accelerate Your Future
Enroll Now and Transform Your Future
quiz-icon

File and Directory Permissions in Linux

Linux is a multi-user OS; every file and directory has permission settings that determine who can read, write, or execute it. Linux file permissions control how users can interact with files and directories, as per the rules enforced on them by the Linux kernel.

1. Types of Permissions in Linux

There are the following types of permissions in Linux:

1. r (Read): The file or directory having the read permission can only view file contents or list directory contents.

2. w (Write): The file or directory having the write permission can modify files or create or delete items in a directory.

3. x (Execute): The file or directory having this permission can run the file as a program or enter a directory.

2. Permissions Access Categories

There are the following permission categories in Linux.

1. u: It applies to the user or the owner.

2. g: It applies to the group.

3. o: It applies to others, i.e., everyone else.

4. a: It applies to all, i.e., user, group, and others.

3. Numeric Representation of Permissions

In Linux, the permissions are represented by binary or octal format. The permissions are stored as binary, but commonly represented using a three-digit number that represents the octal format. While the permissions represented using single-digit numbers reflect the binary format.

Below are numeric representations of permissions.

Permission Binary Octal Meaning
000 0 No permissions
–x 001 1 Execute only
-w- 010 2 Write only
-wx 011 3 Write and execute
r– 100 4 Read only
r-x 101 5 Read and execute
rw- 110 6 Read and write
rwx 111 7 All (read and write and execute)

Now, let us understand the permission with the help of an example as below.

example

In the above image, Intellipaat is the name of the directory where the file sample.txt resides. The first part -rw-r–r– indicates the file type and permissions: it’s a regular file (-), with the owner (akshat) having read and write permissions (rw-), the group (akshat) having read-only access (r–), and others also having read-only access (r–). The number 1 denotes a single hard link to the file. The next two fields show that the file is owned by user akshat and the group is also akshat. The file size is 0 bytes, indicating it is empty, and the last modified date and time are July 30 at 04:11. The final part is the file name, sample.txt.

Modes in the chmod Command in Linux

1) Symbolic Mode

The symbolic mode in the chmod command in Linux uses symbols or letters to modify permissions for user categories and allows adding, removing, or setting permissions using the operators.

The following operators are used to modify the permissions in the chmod command in Linux.

Operator Meaning Description
+ Add permission Adds one or more permissions to the specified user class.
Remove permission Removes one or more permissions from the specified user class.
= Set exact permission Sets the permissions exactly and removes any other permissions not listed.

For example, the command chmod u=rw file.txt sets the read and write permissions for the user (u) and removes the execute permission if it was previously.

2) Octal Mode

The octal mode uses the numbers instead of symbols to represent the permission sets and is a compact way to set permissions at once. Each of the three digits corresponds to a specific class of users:

  • The first digit represents the owner’s permissions.
  • The second digit represents the group permissions.
  • The third digit represents the other permissions.

Below is a representation of octal values.

Permission Symbol Value
Read r 4
Write w 2
Execute x 1

For example, according to the command chmod 754 filename,

  • The owner (7) will get read, write, and execute permission.
  • The group (5) will get read and execute permission.
  • Others (4) will get read permission.

How to Remove or Add the Permissions from a File in Linux?

To remove the permissions from a file in Linux, use a chmod command with the minus (-) operator to remove specific permissions from a file for a user, group, or owner.

Below is the syntax of it:

chmod [who]-[permissions] filename

In the same way, to add permissions to a file in Linux, use a chmod command with the addition (+) operator to grant specific permissions to a file for a user, group, or owner.

Below is the syntax of it:

chmod [who]+[permissions] filename

In the above syntax,

Who refers to:

  • u for user (owner)
  • g for group
  • o for others
  • a for all (user + group + others)

Permissions refer to:

  • r for read
  • w for write
  • x for execute

Example:

 Remove or Add the Permissions from a File

In the above example, a file named example.txt is created, having the above permissions as default. Then write permission was removed for the owner by the command chmod u-w example.txt. In the second command, chmod u+x example.txt, the owner was granted execute permissions.

Options in the chmod Command in Linux

Below are the options present in the chmod command in Linux:

1. -c or –changes

The -c option in the chmod in Linux displays a message in your terminal when actual changes are made to the file’s permissions.

Below is the syntax of it:

chmod -c [permissions] [file(s)]

Example:

-c or --changes

In the above example, first the permission for the sample.txt file is displayed, and then changed to 600 and 675, displaying the message as above.

2. -R or –recursive

The -R option in the chmod command in Linux changes the permissions of the specified directory and all files and subdirectories within it.

Below is the syntax of it:

chmod -R mode file_or_directory

Example:

-R or --recursive

In the above example, the command chmod -R 777 . modifies the permissions of the file sample.txt from rw-r–r– to rwxrwxrwx.

Note: The . in the above command represents the current directory.

Get 100% Hike!

Master Most in Demand Skills Now!

3. -v or –verbose

The -v option in the chmod command in Linux is used to print a message for every file whose permissions are processed by the chmod command, regardless of whether the permission was changed or not.

Below is the syntax of it:

chmod -v permission file(s)

Example:

-v or --verbose

In the above example, the command chmod -v 644 sample.txt modifies the permissions of the sample.txt from 0777 (rwxrwxrwx) to 0644 (rw-r–r–) as shown above.

4. -f or –silent or –quiet

The -f option in the chmod command in Linux allows the command to quietly skip over files or directories where permission changes fail, without displaying an error in the terminal.

Below is the syntax of it:

chmod -f permission file_path

Example:

-f or --silent or --quiet

In the above example, a file named protected.txt is created in the root folder by entering the sudo password. Then, the permissions are changed to 644, which displays the error without the -f option.

5. –help

The –help option in the chmod command in Linux is used to display the help information for the chmod command and prints a summary of the syntax, available options, and usage instructions directly to the terminal.

Below is the syntax of it:

chmod --help

Example:

--help

In the above example, there are some basic options of the chmod command.

6. –version

The –version option of the chmod command in Linux displays the version information of the chmod command installed on your system.

Below is the syntax of it:

chmod --version

Example:

 --version

The above example displays the current version of the chmod command installed in your system.

What are special file permissions?

In Linux, some special permissions bits add extra control over how files and directories behave. These are:

  • SUID (Set User ID)
  • SGID (Set Group ID)
  • Sticky Bit

These permissions are used to manage how users interact with files and directories, especially when a privilege is needed in a secure way.

1. SUID

SUID (Set User ID) is a special permission in Linux systems that allows a user to execute a file with the permissions of the file’s owner, not the user who is running it, and is only applicable to executable files.

2. SGID

SGID (Set Group ID) behaves differently when it is applied to a file or directory.

  • When SGID is set on an executable file, it means, any user executing the file will temporarily gain the permissions of the group that owns the file.
  • When SGID is set on a directory, it means, all files or subdirectories created inside it will automatically inherit the group ownership of the parent directory, not the primary group of the user creating the file.

3. Sticky Bit

The Sticky Bit is a special permission that is used only on directories, not files. It prevents the user from deleting or renaming the files that are owned by others in that directory, even if they have write permissions.

How to Make a Script Executable in Linux Using the chmod Command?

In Linux, scripts are plain text files by default, and to run them, you have to make them executable. By default, when you make a file, it might not have execute permissions and will throw a “Permission denied” error if you try to run it without this permission.

Steps to Make a Script Executable in Linux Using the chmod Command

Below are the steps to make a script executable in Linux using the chmod command:

1. Create a Script File

Make the file intellipaat.sh using the following command

nano intellipaat.sh

Now, write the content in it as follows.

Make a Script Executable in Linux Using the chmod Command

Once the content is written, save the file by Ctrl+O, and Enter, then to exit, press Ctrl+X.

Note: The #!/bin/bash in the scripts above is known as the shebang, and it tells the system to use the bash shell to run this script.

Step 2: Make It Executable

Use the chmod command as follows to give execute permission to the file:

chmod +x intellipaat.sh

Step 3: Run the Script

Run the file intellipaat.sh as follows:

./intellipaat.sh

Output:

Script Executable in Linux Using the chmod Command

Best Practices for using chmod command in Linux

The chmod command in Linux is useful due to its many real-world applications, as mentioned above, but it should be used wisely because of the following reasons:

  • Avoid Using chmod 777: The command chmod 777 in Linux gives full permissions to everyone, which can lead to unauthorized changes or deletions
  • Be careful with chmod -R: The option -R in the chmod command in Linux applies permissions to all the files and subdirectories under a directory, which can break file and system functionality.
  • Always verify the permissions: Always use ls -l to verify which file or directory has been updated with the permissions.
  • Use symbolic notations: Symbolic notations like u, g, o, a, and so on are more readable than numeric mode, which will be helpful for beginners.
Unlock Your Future in Linux
Start Your Linux Journey Today
quiz-icon

Conclusion

From the above article, we learned that the chmod command in Linux is a powerful tool used for security and management purposes by administrators and common users. It allows control over who can read, write, or execute permissions. It supports many options, like -R, -v, -f, -c, etc. However, the chmod command should be used with caution, as one wrong command can lead to potential security risks, hence, always follow the best practices.

If you want to learn more about the grep command in Linux, you can refer to our Linux Course.

chmod command in Linux – FAQs

Q1. What is the chmod in Linux command?

chmod is a command used in Linux to change the permissions of files and directories, controlling who can read, write, or execute them.

Q2. Why is chmod used?

It’s used to set or modify file access permissions for the owner, group, and others to enhance security and manage access control.

Q3. What does 755 mean in chmod?

755 means the owner has full permissions (read, write, execute), while the group and others have read and execute permissions.

Q4. Should I use chmod 777?

You should avoid chmod 777 unless necessary, as it gives everyone full access and poses a major security risk.

Q5. What is the difference between chmod 777 and 7777?

While 777 sets file permissions, 7777 includes a special SetUID/SetGID/Sticky Bit bit in addition to full access, which can affect how the file behaves and who can delete or execute it.

About the Author

Senior Consultant Analytics & Data Science, Eli Lilly and Company

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

fullstack