How do I use chmod to change permissions?

The chmod (short for change mode) command is used to manage file system access permissions on Unix and Unix-like systems. There are three basic file system permissions, or modes, to files and directories:

Each mode can be applied to these classes:

The user is the account that owns the file. The group that owns the file may have other accounts on the system as members. The remaining class, other (sometimes referred to as world), means all other accounts on the system.

You can view the modes on files and directories by executing this command:

ls -l

The first field will contain 10 characters referring to the following characteristics:

Character    What it means
        1               "d" if a directory, "-" if a file
        2               "r" if file is readable to user, "-" if not
        3               "w" if file is writable to user, "-" if not
        4               "x" if file is executable to user, "-" if not
        5-7             same as 2-4, with reference to group
        8-10            same as 2-4, with reference to everyone on the system

In the following example, file1 is readable and writable to the user and readable to everyone on the system. file2 is readable, writable and executable by everyone. file3 is readable, writable and executable only to the user:

$ ls -l
total 28
-rw-r--r--  1 user       group     273 Mar 24 11:28 file1
-rwxrwxrwx  1 user       group    1449 Jan 29 14:01 file2
-rwx------  1 user       group    4119 Jan 26 13:22 file3

To change the permissions of a file, one uses the chmod command, with the following syntax:

chmod [references][operator][modes] filename

The references are shorthand (u, g, or o) for each class. The operator determines whether to add (+), remove (-) or explicitly set (=) the particular permissions. The modes are read (r), write (w), or execute (x).

For example, to add the execute permission for the user to file1:

chmod u+x file1

To remove the write permission for others for file2:

chmod o-w file2

You can combine multiple references and modes to set the desired access all at once. For example, to explicitly make file3 readable and executable to everyone:

chmod ugo=rx file3

The all (a) mode is the same as ugo, allowing the previous command to be expressed as:

chmod a=rx file3

For more information on changing permissions, run this command:

man chmod

Related Articles

© Computing and Educational Technology Services