Cantech Knowledge Base

Your Go-To Hosting Resource

How to Use the Chmod Command in Linux?

Linux `chmod` command controls read, write, or execute permissions of file and directory. The command modifies the permission bits assigned to the owner, group, and others. Thus, these permissions ensure that only authorized users can access or modify files.

Its numeric mode uses octal values like 755 or 644. Whereas, the symbolic mode uses `+`, `-`, or `=` to grant or remove permissions. A wrong permission setting can break a script or expose sensitive data.

All in all, your system could be vulnerable to unauthorized access if there’s no proper permission management. So, you must understand that knowing how to use chmod is important if you are a Linux user.

Understanding File Permissions in Linux

As discussed, every file and directory in Linux has permissions assigned to different users. Let’s understand this further –

Users Associated with Linux Files

Linux has users categorised into three types based on file ownership and access control.

The owner (user who created the file and has specific rights), the group (multiple users who share access to the file), and the others (all other users on the system who are neither the owner nor part of the group).

Linux File Permission Notations

There are two ways in which permissions in Linux are represented – specific letters or numeric values.
The letter r stands for read (users can view the file contents), the letter w represents write (to modify or delete a file), and the letter x indicates execute (run the file as a script or program).
Thus, with r, w, and x in a specific order, a file’s permissions will appear as a string. For example, rwxr-xr– means:

  • The owner has read, write, and execute permissions.
  • The group members can read and execute but cannot modify the file.
  • Other users can only read the file.

Viewing Current Permissions

You can use the ls -l command to check the permissions of a file or directory. Below is the example output that you get by running the command: ls -l sample.txt –

-rwxr-xr-- 1 user group 4096 Sep 1 12:34 sample.txt

This output says that –

  • The owner can read, write, and execute the file.
  • The group members can only read and execute the file.
  • Other users can only read the file.

Changing Permissions with chmod

As mentioned, the chmod command can modify permissions for files and directories. Below is the basic syntax of this command –

chmod [options] mode file/directory

Here, [options] are optional flags used to change the command’s behavior. The mode specifies the new permissions, whereas file/directory is the target file or directory.

Common chmod Options

  • -R – Recursively changes permissions for all files and subdirectories.
  • -v – Displays detailed output of the changes (verbose).
  • -c – Shows output only when a change occurs (changes).

Creating Sample Files and Directories

You can create some sample files and directories before you commence to test permission changes.

So, run the below command to create three text files –

touch file1.txt file2.txt file3.txt  

Also, create directories using –

mkdir directory directory1 dir shared shared/directory  

With the below command, create an executable script file –

touch backup.sh

Modifying Permissions Using Symbolic Mode

In this mode, you can change permissions using specific letters. The symbols used are:

  • u is the owner (user).
  • G means the group.
  • O refers to others.
  • A – All includes owner, group, and others.

Further, operators such as below help modify permissions –

  • + adds a permission.
  • – removes a permission.
  • = sets a permission and removes all others that are not specified.

Examples of Symbolic Mode chmod Commands

The file owner can execute a file using –

chmod u+x file.txt

The below command will remove write permission from a group –

chmod g-w file.txt  

Running the following command will enable other users to read and write a file –

chmod o=rw file.txt  

Modifying Permissions Using Numeric (Octal) Mode

The numeric mode assigns a three-digit number. Each of this digit represent permissions for the owner, group, and others. The numeric values are –

  • 4 – Read permission.
  • 2 – Write permission.
  • 1 – Execute permission.
  • 0 – No permission.

These values are combined to set permissions. Some common examples include:

  • 7 (rwx) – Full permissions (4+2+1).
  • 6 (rw-) – Read and write (4+2).
  • 5 (r-x) – Read and execute (4+1).
  • 4 (r–) – Read-only.

Examples of Numeric Mode chmod Commands

If you want to set 755 permissions, below is the command –

chmod 755 file.txt

This gives the owner full access and only read and execute permissions for the group and others.

Similarly, set 644 permissions with –

chmod 644 file.txt  

This means the owner can read and write, but others can only read.

Changing Permissions Recursively

-R option modifies permissions for a directory and all its contents. Below is the command –

chmod -R 755 directory1  

This gives the owner full access, whereas the group and others can read and execute.

The below displays a detailed progress while changing permissions.

chmod -v 755 file.txt  

Use the below for recursive and verbose output –

chmod -Rv 755 directory1  

Applying chmod to Multiple Files

Change permissions for multiple files using –

chmod 644 file1.txt file2.txt file3.txt  

Using Special Permissions

Linux provides additional permission types for advanced control. These include SUID, SGID, and Sticky Bit.

SUID (Set User ID)

You can run a file with the permissions of its owner with SUID on an executable file. Below is the command –

chmod u+s file1.txt  

SGID (Set Group ID)

SGID works for both files and directories.

For executable files, it makes the file run with the permissions of a group. This is useful when multiple users need shared access.

For directories, it ensures that any new file or folder inside it gets the same group as the main directory. This helps you avoid any issues where files get assigned to a user’s default group.

You can set the SGID bit on a directory like directory1.

chmod g+s directory1

With this, any file created inside will automatically take the directory’s group. It will not follow the user’s default group.

Sticky Bit

Users cannot modify or delete files in a shared directory unless they own them. To apply this permission, use the command –

chmod +t directory  

Advanced chmod Usage

Change permissions for all .txt files in a directory (apply new permission at once after finding the .txt file in the working directory) –

find . -name "*.txt" -exec chmod 644 {} \;

Set permissions only for files and leave directories unchanged –

find /dir -type f -exec chmod 644 {} \;

Give all permissions to the owner of the subdirectory. Also, restrict deletion in a shared directory –

chmod 1770 /shared/directory

Only the script owner can execute a file. Other users are restricted –

chmod 700 backup.sh

Conclusion

You need to manage file permissions carefully for system security and access control in Linux. The chmod command provides various basic to advanced options to modify and manage file and directory permissions flexibly.

May 9, 2025