The chmod
command is used to control who can access files and
directories on Unix and Unix-like systems. It allows you to specify three types of
permissions for each file or directory: read, write, and execute.
(r)
→ allows users to open and read the file's contents
(w)
→ allows users to modify the file's contents(x)
→ allows users to run the file as a programThese permissions can be applied to three different classes of users (or entities):
(u)
→ the owner of the file / the user who created the file
(g)
→ other users in the file's group(o)
→ other users not in the file's group(a)
→ user + group + allTo see how chmod
works, execute the ls -l
command on your linux system.
The output you get should look similar to the
example below:
$ ls -l
total 4
drwxr-xr-x 1 user group 0 Jun 19 11:29 directory1
-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
In our example, there are a total of 4 items in the directory. The first field of each line, which consists of 10 characters, indicates the file type and permissions.
Character | Meaning |
---|---|
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 group |
With reference to the table above, we can interpret that the first item is a directory named
directory1
.
The next item 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.
There are two ways to change the permissions on a file, using the symbolic mode syntax or the numeric mode syntax.
chmod [references][operator][modes] filename
Component | Description |
---|---|
references |
shorthand (u, g, o, a) for each entity |
operator |
|
modes / permissions |
zero or more letters from rwxXstugo
|
[ugoa]*([-+=]([rwxXst]*|[ugo]))+
chmod u+x file1
chmod o-w file2
chmod ugo=rx file3
chmod a=rx file3
chmod =rw,g+s file3
chmod [1-4 octal digits] filename
In numeric mode, permissions are specified in the form of one to four octal digits (0-7). Any omitted digits are assumed to be leading zeros.
Digit | Description |
---|---|
1st | selects the set user ID (4) and set group ID (2) and restricted deletion or sticky (1) attributes |
2nd | selects permissions for the owner: read (4), write (2), and execute (1) |
3rd | selects permissions for the group users: read (4), write (2), and execute (1) |
4th | selects permissions for other users NOT in the file's group: read (4), write (2), and execute (1) |
user (rwx) = 4 + 2 + 1 = 7
group (rx) = 4 + 1 = 5
other (rx) = 4 + 1 = 5
mode = 0755
chmod 444 file1
chmod 744 file2
chmod [Options]... Mode [,Mode]... file...
chmod [Options]... Numeric_Mode file...
chmod [Options]... --reference=RFile file...
-R, --recursive
change files and directories recursively-f, --silent, --quiet
suppress most error messages-v, --verbose
output a diagnostic for every file processed-c, --changes
like verbose but report only when a change is made--no-preserve-root
do not treat '/' specially (the default)--preserve-root
fail to operate recursively on '/'--reference=RFILE
use RFILE's mode instead of MODE values--help
display this help and exit--version
output version information and exit