chmod is a Linux command-line utility used to change the access permissions of files and directories. It is very useful in a multi-user environment where you want to restrict files and directories so that only authorized users and processes can access them. The chmod command stands for “change mode” and is used to restrict the way a file can be accessed.

In this post, we will show you how to use the chmod command in Linux.

Basic Syntax

The basic syntax of the chmod command in symbolic format is shown below:

chmod u=rwx,g=rwx,o=rwx file_name/dir_name

Where:

  • u – user
  • g – group
  • o – other
  • r – read
  • w – write
  • x – execute

The basic syntax of the chmod command in numerical format is shown below:

chmod 777 file_name/dir_name

Where:

  • The First 7 represent user permissions
  • Second 7 represents group permissions
  • Third 7 represents other permissions

Each of the digits is a combined sum of the numbers 4, 2, 1, and 0.

Where:

  • 4 – read
  • 2 – write
  • 1 – execute
  • 0 – no permissions

Also Read

How to Use chown (Change Ownership) Command in Linux

Viewing File Permissions

Before changing the file and directory permissions, it is important to view the existing permissions of the file and directory.

Run the following command to check the existing permissions of all files located inside the current directory:

ls -l

You will get the following output:

-rw-rw-r--  1 vyom vyom      1961 Feb 13 18:28  system-back.tar.gz
-rw-rw-r--  1 vyom vyom      1961 Feb 13 18:23  system-back.tar.gzip
drwxr-xr-x  2 vyom vyom      4096 Oct 25 00:27  Templates
-rwxrwxrwx  1 vyom vyom 119707966 Dec  7 00:31  Udeler-1.8.2-linux-x86_x64.AppImage
-rw-rw-r--  1 vyom vyom        93 Mar 29 21:57 'Untitled Document 1'
drwxr-xr-x  7 vyom vyom      4096 Mar 28 10:19  Videos
drwxrwxr-x  3 vyom vyom      4096 Nov  5 09:00 'VirtualBox VMs'

On each line, the first character identifies the type of entry that is being listed. If it is a dash (-), it is a file. If it is the letter d, it is a directory.

The next nine characters represent the settings for the three sets of permissions.

  • The first of the three sets of characters rw- shows the permissions for the user who owns the file.
  • The second of the three sets of characters rw- shows the permissions for members of the file’s group.
  • The third of the three sets of characters r– shows the permissions for others.

Also Read

How to Use chgrp (Change Group Ownership) Command in Linux

How to Use Chmod Command

Now that we understand different types of permissions, let’s change the permissions of the file using some examples.

To change the permissions of the file named file1.txt so that everyone can read and write it, run the following command:

chmod u=rw,g=rw,o=rw file1.txt

Or

chmod 666 file1.txt

To change the permissions of the file named file1.txt so that the file owner can read, write, and execute while a group and others can read and execute:

chmod u=rwx,g=r-x,o=r-x

Or

chmod 755 file1.txt

To grant read, write, & execute permissions to the owner and read permissions to the group and others to a directory named dir1 including all sub-directories and files, use the -R flag:

chmod -R u=rwx,g=r,o=r dir1

Or

Chmod -R 744 dir1

To grant execute permissions to everyone on a file named file1.txt, run:

chmod +x file1.txt

To remove write permissions for other users, run the following command:

chmod o-w file1.txt

To recursively remove the read permissions for other users on a given directory, run:

chmod -R o-r dirname

To remove the read, write, and execute permissions for all users except the file’s owner, run:

chmod og-rwx file1.txt

To copy the permissions of file1.txt and apply it to file2.txt, run:

chmod --reference=file1.txt file2.txt

Conclusion

In this post, you learned how to use the chmod command to change the file and directory permissions in Linux. Try it on virtual private servers from Atlantic.Net!