Examples to Use chmod Command in Linux

chmod Command in Linux to Change File Permissions

File permissions are one of the subjects that both new and professional Linux users would deal with during their daily tasks. It is essential to change the file permission to read, write, and execute commands since Linux does not have just one user. The chmod command in Linux enables you to manage file permissions by letting choose who has access to files, search directories, and run scripts. This useful command is used in most Linux distributions. So, Ubuntu, Debian, and CentOS users will have the option of individually setting their file permissions or setting them for the entire group using the chmod command in Linux.

Changing Linux file permissions may seem complicated, but learning the syntax of the chmod command helps you to understand how it uses ”who”, ”what”, and ”which” to set permissions. If you are going to buy Linux VPS and you are concerned about who can do what to a file or directory, this guide is what you need to get familiar with permissions and get skilled in changing them since permissions control what can be done with a file or directory.

Let’s get started and learn the definition of file permission in Linux and practice some examples to see how to use the chmod command in Linux to change file permissions.

An Intro to File Permissions in Linux

The actions that can be taken about the file or directory in Linux are controlled by permissions. The type of access connected to a file is called file permission. There are three main types of users in Linux:

  1. The file owner
  2. The group members
  3. Others

Each file in Linux has its owner, a group, and permission access for the above main users. There are three different file permissions available to each of these user classes:

  1. Read
  2. Write
  3. Execute

Also, to understand permission syntax, you need to know what are the who, what, and which we mentioned earlier:

  1. Who: For whom are the permissions being set?
  2. What: Change are we implementing? Do we grant or deny the permission?
  3. Which: permission are we granting right now?

You can specify which users can run, read, or write the file by understanding file permissions. The ability to open a file does not always entail the ability to edit it. Files and folders have associated privileges, and the system prevents unauthorized users from accessing the files and directories. To not face Permission Denied Error in Linux, the files and directories you are interacting with must belong to your user account.

While the chmod 777 command in Linux permits all users to read, write, and execute in the system’s primary files, such as configuration files, it can create severe security problems for the Linux operating system because all users can quickly access the information in these files and make changes to them. So, it is important to know about working with the chmod command in Linux to not change it into Dangerous and destructive Linux Commands.

Check File Permission in Linux

As you now know, the chmod is used to set permissions. You can run a command to see about permissions that have been set to your considered file or directory on your system. To change the ownership of a file and configure its permissions, the Chown command is used in Linux. ls enables you to check the permissions of the files.

To view a list of the file permissions for the files and directories in Ubuntu, Debian, and CentOS, run:

ls -l

Adding the name of a single file to the ”ls” command displays its permissions:

$ ls -l operafile

To obtain the contents of the “operafile,” along with its file permissions, the “-l” option is added in this case. Replace your considered file name with ”opera”.

Each set of permissions contains three characters. The characters serve as indicators of whether permission is granted or denied. They are either a letter or a dash (). If there is a dash in the character, permission is not given. That authorization has been given if the character is a r, w, or x.

What is Linux chmod Command?

The term “Change Mode” is referred to by the abbreviation “chmod”. It alters the way that files, scripts, and system folders are accessed. For determining modification permission, The chmod has several distinct modes. Numeral (letter) or symbolic (octal) forms of these modes are used to depict them. To control file permissions, you can use the chmod command in Linux. It’s an important command that practically every user will occasionally find themselves using.

chmod Command Syntax

$ chmod [Options] [Filename]

The following options are available for numerical representation:

  • 0” represents “no permission”.
  • 1” represents “execute permission”.
  • 2” represents “write permission”.
  • 4” represents “read permission”.

Following is the syntax for the chmod command when using the symbolic representation:

$ chmod [Option1] [Operator] [Option2] [Filename]

The following options are available in the symbolic form of chmod command in Linux:

  • u” indicates file owner.
  • g” indicates groups.
  • o” indicates others.
  • a” indicates all users as owner, group, and others (ugo).

The below operators are accepted by the chmod command in Linux:

  • +”: This operator is used to add specified permissions.
  • ”: This operator is used to remove specified permissions.
  • =”: This operator is used to define the exact file permission for any user.

Change Permissions in Linux

chmod permissions in Linux can be changed using numbers as flags:

NumberPermissionDescription
0---No permissions
1--xExecute only
2-w-Write only
3 (i.e. 2+1)-wxWrite and execute only
4r--Read-only
5 (i.e. 4+1)r-xRead and execute only
6 (i.e. 4+2)rw-Read and write only
7 (i.e. 4+2+1)rwxRead, write and execute

How to Use chmod Command in Linux [Practical Examples]

If you study Linux commands, chmod command is certainly one of them. This tutorial covers 8 helpful examples to help understand the chmod command and the way you can use it to change file permissions in linux ubuCentosd centos.

Example 1the . Using chmod command to Set read by owner only

No other group or user has access to read, write, or execute this file except for this permission. Even the owner won’t be able to access the file to run programs or make changes. To set ”read by owner only” file permission, run:

chmod 400 operafile

The symbolic mode of the above command is:

chmod u=r filename

Example 2. Using chmod Command to Write only by the Owner

The chmod command in Linux enables users to write permissions restricted to the owner. To set ”write by owner only” file permission, type:

chmod 200 operafile

You can simply use the command below to do the same in symbolic mode:

chmod u=w filename

Example 3. Using chmod Command to Change Permissions Recursively

When using the -R flag to apply the same permissions, it will have a recursive effect so that they are the same for the entire directory:

chmod -R [permission] Target_Directory

For example, you can use the following command to allow all users to read, write, and execute the contents of a specific directory:

chmod -R 777 Your Directory

This can also be done using symbolic mode:

chmod -R a=rwx Target_Directory

Example 4. Using chmod Command to Allow Execution Permissions to Everyone

While working on Linux, you face some occasions when you need to allow every user to execute a particular file. To do this, the chmod command in Linux can be used as shown below:

chmod 111 operafile

Also, the symbolic form would be like:

chmod a=x filename

Example 5. Using chmod Command to Limit Owners & Others Rights to read-only

Using the chmod command in Linux, you can give the file owner all permissions while limiting others to read-only access:

chmod 744 filename

You must add permissions for each user owner, group owner, and other user separately if you wish to have distinct permissions for each of them. Therefore, you can use the chmod command in Linux as you see below if you want the owner to have full rights and everyone else to have read-only access only.

chmod u=rwx,g=r,o=r filename

Example 6. Using chmod Command to Set Execute by Owner

The below mode denotes that the file “owner” will be given “execute” permission:

$ chmod u+x filename

To verify the file permission changes, run:

$ ls -l filename

Example 7. Using chmod Command to Allow Read Permissions to Everyone

To change the file permissions to ”read by everyone”, you can use the following symbolic form:

$ chmod a+r filename

Then, to verify the file permission changes, run:

$ ls -l filename

Example 8. Using chmod Command to Read by group only

The chmod command in Linux is also used to associate “ready by group only” permission to the file. To do this, run the command below:

$ chmod 040 operafile

Then, you can confirm the changed file permission by running the following command:

$ ls -l operafile

The “020” mode and the “002” mode can both be used to define the permissions “write by group only” and “write by other only,” respectively.

Conclusion

In this article, you got familiarized with the concept of File Permissions and you learned Howthe  to use chmod command in Linux. With the help of the described examples, you know how to the chmod command in Linux to set file permissions and allow users to access a specific file. As an Ubuntu, Debian, or CentOS user, you can change file permissions using the numeric and symbolic modes of the chmod command in Linux.

Leave a Reply

Your email address will not be published. Required fields are marked.