How to Use the chmod Command on Linux | Vevioz Developers

How to Use the chmod Command on Linux

DevOps

Linux Systems Engineer
Joined
Oct 18, 2019
Messages
198
Reaction score
18
Points
18
Location
Novosibirsk, Russia
Website
addons.vevioz.com
Offline
maxresdefault.jpg

Code:
chmod -R 777 /path

Control who can access files, search directories, and run scripts using the Linux’s chmod command. This command modifies Linux file permissions, which look complicated at first glance but are actually pretty simple once you know how they work.

chmod Modifies File Permissions​

In Linux, who can do what to a file or directory is controlled through sets of permissions. There are three sets of permissions. One set for the owner of the file, another set for the members of the file’s group, and a final set for everyone else.

The permissions control the actions that can be performed on the file or directory. They either permit, or prevent, a file from being read, modified or, if it is a script or program, executed. For a directory, the permissions govern who can cd into the directory and who can create, or modify files within the directory.

You use the chmod command to set each of these permissions. To see what permissions have been set on a file or directory, we can use ls.

Viewing and Understanding File Permissions​

We can use the -l (long format) option to have ls list the file permissions for files and directories.

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 three characters show the permissions for the user who owns the file (user permissions).
  • The middle three characters show the permissions for members of the file’s group (group permissions).
  • The last three characters show the permissions for anyone not in the first two categories (other permissions).
There are three characters in each set of permissions. The characters are indicators for the presence or absence of one of the permissions. They are either a dash (-) or a letter. If the character is a dash, it means that permission is not granted. If the character is an r, w, or an x, that permission has been granted.

The letters represent:

  • r: Read permissions. The file can be opened, and its content viewed.
  • w: Write permissions. The file can be edited, modified, and deleted.
  • x: Execute permissions. If the file is a script or a program, it can be run (executed).
For example:

  • --- means no permissions have been granted at all.
  • rwx means full permissions have been granted. The read, write, and execute indicators are all present.
In our screenshot, the first line starts with a d. This line refers to a directory called “archive.” The owner of the directory is “dave,” and the name of the group that the directory belongs to is also called “dave.”

The next three characters are the user permissions for this directory. These show that the owner has full permissions. The r, w, and x characters are all present. This means the user dave has read, write and execute permissions for that directory.

The second set of three characters are the group permissions, these are r-x. These show that the members of the dave group have read and execute permissions for this directory. That means they can list the files and their contents in the directory, and they can cd (execute) into that directory. They do not have write permissions, so they cannot create, edit, or delete files.

ADVERTISEMENT


The final set of three characters are also r-x. These permissions apply to people who are not governed by the first two sets of permissions. These people (called”others”) have read and execute permissions on this directory.

So, to summarise, group members and others have read and execute permissions. The owner, a user called dave, also has write permissions.

For all of the other files (apart from the mh.sh script file) dave and members of the dave group have read and write properties on the files, and the others have read permissions only.

For the special case of the mh.sh script file, the owner dave and the group members have read, write, and execute permissions, and the others have read and execute permissions only.

Understanding The Permission Syntax​

To use chmod to set permissions, we need to tell it:

  • Who: Who we are setting permissions for.
  • What: What change are we making? Are we adding or removing the permission?
  • Which: Which of the permissions are we setting?
We use indicators to represent these values, and form short “permissions statements” such as u+x, where “u” means ” user” (who), “+” means add (what), and “x” means the execute permission (which).

The “who” values we can use are:

  • u: User, meaning the owner of the file.
  • g: Group, meaning members of the group the file belongs to.
  • o: Others, meaning people not governed by the u and g permissions.
  • a: All, meaning all of the above.
 
Top Bottom