< Previous | Contents | Next >
chmod – Change File Mode
To change the mode (permissions) of a file or directory, the chmod command is used. Be aware that only the file’s owner or the superuser can change the mode of a file or direc- tory. chmod supports two distinct ways of specifying mode changes: octal number repre- sentation, or symbolic representation. We will cover octal number representation first.
What The Heck Is Octal?
Octal (base 8), and its cousin, hexadecimal (base 16) are number systems often used to express numbers on computers. We humans, owing to the fact that we (or at least most of us) were born with ten fingers, count using a base 10 number sys- tem. Computers, on the other hand, were born with only one finger and thus do all all their counting in binary (base 2). Their number system only has two numerals, 0 and 1. So in binary, counting looks like this:
0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011...
In octal, counting is done with the numerals zero through seven, like so: 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21...
Hexadecimal counting uses the numerals zero through nine plus the letters “A” through “F”:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, 12, 13...
While we can see the sense in binary (since computers only have one finger), what are octal and hexadecimal good for? The answer has to do with human con- venience. Many times, small portions of data are represented on computers as bit patterns. Take for example an RGB color. On most computer displays, each pixel is composed of three color components: eight bits of red, eight bits of green, and eight bits of blue. A lovely medium blue would be a 24 digit number:
010000110110111111001101
How would you like to read and write those kinds of numbers all day? I didn't think so. Here's where another number system would help. Each digit in a hexa- decimal number represents four digits in binary. In octal, each digit represents three binary digits. So our 24 digit medium blue could be condensed down to a six digit hexadecimal number:
436FCD
Since the digits in the hexadecimal number “line up” with the bits in the binary number we can see that the red component of our color is 43, the green 6F, and the blue CD.
These days, hexadecimal notation (often spoken as “hex”) is more common than octal, but as we shall soon see, octal's ability to express three bits of binary will be very useful...
With octal notation we use octal numbers to set the pattern of desired permissions. Since each digit in an octal number represents three binary digits, this maps nicely to the
scheme used to store the file mode. This table shows what we mean:
Table 9-4: File Modes In Binary And Octal
Octal | Binary | File Mode |
0 | 000 | --- |
1 | 001 | --x |
2 | 010 | -w- |
3 | 011 | -wx |
4 | 100 | r-- |
5 | 101 | r-x |
6 | 110 | rw- |
7 | 111 | rwx |
By using three octal digits, we can set the file mode for the owner, group owner, and
world:
[me@linuxbox ~]$ > foo.txt
[me@linuxbox ~]$ ls -l foo.txt
-rw-rw-r-- 1 me me 0 2016-03-06 14:52 foo.txt [me@linuxbox ~]$ chmod 600 foo.txt
[me@linuxbox ~]$ ls -l foo.txt
-rw------- 1 me me 0 2016-03-06 14:52 foo.txt
[me@linuxbox ~]$ > foo.txt
[me@linuxbox ~]$ ls -l foo.txt
-rw-rw-r-- 1 me me 0 2016-03-06 14:52 foo.txt [me@linuxbox ~]$ chmod 600 foo.txt
[me@linuxbox ~]$ ls -l foo.txt
-rw------- 1 me me 0 2016-03-06 14:52 foo.txt
By passing the argument “600”, we were able to set the permissions of the owner to read and write while removing all permissions from the group owner and world. Though re- membering the octal to binary mapping may seem inconvenient, you will usually only have to use a few common ones: 7 (rwx), 6 (rw-), 5 (r-x), 4 (r--), and 0 (---).
chmod also supports a symbolic notation for specifying file modes. Symbolic notation is divided into three parts: who the change will affect, which operation will be performed, and what permission will be set. To specify who is affected, a combination of the charac- ters “u”, “g”, “o”, and “a” is used as follows:
Table 9-5: chmod Symbolic Notation
Symbol Meaning
Symbol Meaning
u Short for “user” but means the file or directory owner.
g Group owner.
o Short for “others,” but means world.
a Short for “all.” The combination of “u”, “g”, and “o”.
If no character is specified, “all” will be assumed. The operation may be a “+” indicating that a permission is to be added, a “-” indicating that a permission is to be taken away, or a “=” indicating that only the specified permissions are to be applied and that all others are to be removed.
Permissions are specified with the “r”, “w”, and “x” characters. Here are some examples of symbolic notation:
Table 9-6: chmod Symbolic Notation Examples
Notation Meaning
Notation Meaning
u+x Add execute permission for the owner.
u-x Remove execute permission from the owner.
+x Add execute permission for the owner, group, and world.
Equivalent to a+x.
o-rw Remove the read and write permission from anyone besides the owner and group owner.
go=rw Set the group owner and anyone besides the owner to have read and write permission. If either the group owner or world previously had execute permissions, they are removed.
u+x,go=rx Add execute permission for the owner and set the permissions for the group and others to read and execute. Multiple specifications may be separated by commas.
Some people prefer to use octal notation, some folks really like the symbolic. Symbolic notation does offer the advantage of allowing you to set a single attribute without disturb- ing any of the others.
Take a look at the chmod man page for more details and a list of options. A word of cau- tion regarding the “--recursive” option: it acts on both files and directories, so it's not as useful as one would hope since, we rarely want files and directories to have the same per- missions.