chmod Command: Tutorial & Examples

Change the permissions of a file or directory

The chmod command is a Linux command that allows you to change the permissions of a file or directory. It stands for "change mode."

Permissions in Linux are represented by a series of three octal (base-8) digits, known as the "mode." Each digit represents the permissions for a different category of users: the owner of the file, the group owner of the file, and all other users.

To use the chmod command, you need to specify the new mode and the name of the file or directory that you want to change. For example:

chmod 754 /path/to/file

This command will change the permissions of the file /path/to/file to mode 754.

The first digit of the mode (7 in this example) is made up of the sum of the permissions for the owner of the file. The possible values are:

  • 7: read, write, and execute permissions (rwx)
  • 6: read and write permissions (rw-)
  • 5: read and execute permissions (r-x)
  • 4: read permission only (-r-)
  • 0: no permissions (---)

The second digit of the mode (5 in this example) is made up of the sum of the permissions for the group owner of the file. The possible values are the same as for the owner.

The third digit of the mode (4 in this example) is made up of the sum of the permissions for all other users.

You can also use the -R option to recursively change the permissions of all files and directories within a directory. For example:

chmod -R 754 /path/to/directory

This command will change the permissions of the directory /path/to/directory and all of its contents (including subdirectories and files) to mode 754.

As an alternative to using the mode numbers, you can also specify the desired access rights directly for user (u), group (g) or others (o). You need to specify if you need to add (+) or remove (-) access rights. For example:

chmod g+rx /path/to/file

This will give read and execute rights to the group of the file, while the status of the write flag will remain the same.

Note that you must be the owner of the file or directory, or have superuser privileges (using the sudo command), to use the chmod command.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA