chmod command walkthrough

Detailed instructions to learn how to correctly manage file permissions on Linux / Unix systems, with the chmod command.

We are going to present a guide article on the use of the chmod command on Linux operating systems, or rather Unix-like systems, since this same command is available on Unix itself, Linux and other systems such as MacOS.

We will offer various general explanations of the command and a series of practical examples so that it can be understood well.

What are file permissions

As we can imagine, the file permissions are the restrictions that the file has to perform operations with it. These operations can be:

  • Reading
  • Writing (modification)
  • Execution

Each file or directory in Linux systems has a series of permissions that are expressed in turn in three different types of users.

  • Owner: The user who owns the file
  • Group: The Group to which the owner user belongs
  • Others: All other users

When we do a directory listing, using the ls -l command on a Linux system, or MacOS for example, the permissions for each file or directory are displayed, as shown in the following image.

How file permissions are expressed

In the previous image, the part where the permissions are expressed is in a yellow box. As you can see, it is made up of several characters that at first could be difficult to interpret, so we are going to explain them.

Each of the user types and each of the permissions that each user type has are expressed in groups. The groups are structured as can be seen in the following image.

See also  Games in HTML5

As you can see, there are three groups of user types and for each of them there is a character that indicates if it can be read, written, or executed.

An “r” appears in a directory listing if it can be read. For modifications a “w” is indicated and for executions an “x”. If you do not have permissions for any of these operations, a hyphen “-” simply appears.

chmod Command

To change the permissions of a file or directory on a Linux system server, you have to use the chmod command.

In general, any linux or unix command has a help, which you can see by invoking the command, followed by the -help parameter. Something like this:

chmod –help

This particular command has several allowed syntaxes. Among them, for example, you can use:

chmod mode-on-octal file

We can indicate the options or not, as we wish. Typical options are:

  • -R to also look in the subdirectories of the path.
  • – v to show each file processed
  • – c is like -v, but only warns of files that it modifies their permissions

The octal mode is a base 8 (octal) number that specifies the permission. Octal numbers are specified starting the number with a 0. For example, 0777 is indicates all possible permissions for all user types. 0666 indicates that read and write permissions are given, but not execute. 0766 indicates that read and write permissions are given, but they only have execute permission for the users who own the file. 0755 indicates read and execute permissions, but write only for the user who owns the file.

See also  for in in javascript

For example:

chmod 0777 file.txt

Assign all permissions to the file file.txt

chmod 0666 *

Assigns read and write, not execute, permissions to all files and directories in the directory where we run the command.

chmod -R 0644 *

This gives permissions to all files and directories in the directory where the command is invoked and all directories hanging from it. The permissions assigned are read to all users, write only to the owner of the file, and execute to no one.

Another way of working with chmod

Then permissions can also be assigned in another way, using another possible chmod syntax, which is perhaps more useful if we don’t want to deal with octal values.

chmod mode… file

For this we have to be clear about the different groups of users:

  • u: user owner of the file
  • g: file owner user group
  • or: all other users
  • a: all user types (owner, group and others)

You also have to know the letter that abbreviates each type of permit:

  • r: refers to read permissions
  • w: refers to write permissions
  • x: refers to execution permissions

examples

chmod o=rwx *

Assign read, write, and execute permissions for “other” users to all files in the folder

chmod a=rwx file.txt

Assign all permissions to all users for the file file.txt

chmod go= *

Removes all permissions for group users and other users.

chmod u=rwx,g=rw,o= *

It gives all permissions to the owner of the file, it assigns read and write permissions to those in the owner’s group, and it removes all permissions to other users.

See also  CSS3 linear-gradient

Note: A space after the comma “,” in the various permission modes indicated causes the command to fail.

chmod a=r *

Gives read-only permissions to all user types.

In a similar way to what we have just seen, you can also add or remove permissions with the + and – operators. For this, the type of user and the permission that is subtracted or added are indicated. Something like this:

chmod a-wrx *

This removes all permissions for all user types.

chmod a+r,gu+w *

This command assigns read permissions to all users and write permissions to the owner of the file and the owner’s group.

chmod u=w,a+r *

This command assigns write permission to the owner user and adds read permission to all users.

Loading Facebook Comments ...
Loading Disqus Comments ...