Linux command: chmod

Quick startchmodCommand to change the file mode

Each file in the Linux/macOS operating system (usually a UNIX system) has 3 permissions: read, write, and execute.

Go to the folder and runls -alcommand.

The weird string you see on each file line, for exampledrwxr-xr-xTo define the permissions of a file or folder.

Let us analyze it.

The first letter indicates the type of file:

  • -Indicates that it is a normal file
  • dIndicates that it is a directory
  • lMeans this is a link

Then, you have 3 sets of values:

  • Representatives of the first groupownerFile
  • The authority of the second group of representativesgroupThis file is the same as
  • Representatives of the third groupEveryone else

These sets consist of 3 values.rwxExpress specificCharacterHave read, write and execute access rights. Everything deleted will be-, Which allows you to form various combinations of values and relative permissions:rw-,r--,r-x, and many more.

you can use itchmodcommand.

chmodIt can be used in 2 ways. The first one uses symbolic parameters and the second one uses numeric parameters. Let's start with the symbol, which is more intuitive.

You enterchmodFollowed by a space and a letter:

  • arepresentativeAll
  • urepresentativeuser
  • grepresentativegroup
  • orepresentativeother

Then you enter+or-Add or remove permissions. Then, you enter one or more permission symbols (r,w,x).

All followed by the file or folder name.

Here are some examples:

chmod a+r filename #everyone can now read
chmod a+rw filename #everyone can now read and write
chmod o-rwx filename #others (not the owner, not in the same group of the file) cannot read, write or execute the file

You can apply the same permissions to multiple roles by adding multiple letters before the role+/-:

chmod og-r filename #other and group can't read any more

If you are editing a folder, you can use-r(Recursive) flag.

Digital parameters are faster, but when you don’t use them every day, I have a hard time remembering them. You use a number to represent the permissions of the role. The value of this number can be up to 7, and it is calculated in the following way:

  • 1Does it have execute permission
  • 2If you have write permission
  • 4If you have read permission

This gives us 4 combinations:

  • 0Permission denied
  • 1Can be executed
  • 2Can write
  • 3Can be written and executed
  • 4Can read
  • 5Can read and execute
  • 6Can read and write
  • 7Can read, write and execute

We use them in groups of 3 to set permissions for all 3 groups:

chmod 777 filename
chmod 755 filename
chmod 644 filename

ThischmodThis command can be used in Linux, macOS, WSL and wherever you have a UNIX environment

Download mine for freeLinux Command Manual


More cli tutorials: