03. Changing Permissions chmod

To change the mode of a file or directory, we use the chmod command.

Note that only the file's owner or the superuser is able to change the mode of a file.

There are two ways of using chmod - by octal number or symbolic representation.

Chmodding by Octal

Octal values range from 0-7, rather than 0-9 in the more familiar decimal number system.

To see how chmodding works in octal, we must convert first to binary.

The 1 represents a boolean true value, while a 0 represents false, which is then applied to our rwx pattern for permissions.

OctalBinaryFile Mode
0000---
1001--x
2010-w-
3011-wx
4100r--
5101r-x
6110rw-
7111rwx

Now you can see how an octal value can be translated to all file permissions for either owner, group or all.

To use chmod, we pass in file mode in octal values for owner, group, in order, followed by the file we'd want to modify.

$ chmod 644 helloWorld.txt

Here, we would set the file accession of the owner to 6, or rw-, and 4, or r-- for both the group and others (all).

This would give us the new file attributes -rw-r--r--.

As another example, the command below would change the helloWorld.txt accession to -r---wxr--. Can you see why?

$ chmod 434 helloWorld.txt

Most common permission modes

It'd be a hassle to memorize all octal values. Here are a list of absolute permission modes that are used most often.

OctalBinary
644rw- --r --r
600rw- --- ---
755rwx -wx -wx
700rwx --- ---
711rwx --x --x

Chmodding by Symbolic Representation

The second way to chmod is by symbolic representation. If you have trouble remembering the octal forms, this may be a better alternative for you.

We have four characters to describe the file user, group, others and all.

u
User (file owner).
g
Group.
o
Others (world).
a
All (u+g+o).
[none]
If no characters are present, all is assumed.

Authorizing or revoking permissions

We may use the plus (+) and minus (-) symbols to add or remove the permissions rwx.

To give or revoke permissions from multiple groups, simply separate the permissions with a comma (,).

Here are some common examples of chmodding by symbol. We'll start with the default 644 permissions (-rw-r--r--).

$ touch helloWorld.txt
$ ls -l helloWorld.txt
-rw-r--r-- 1 JohnDoe staff 0 Apr 1 23:20 helloWorld.txt
# Give group permission to execute $ chmod g+x helloWorld.txt $ ls -l helloWorld.txt
-rw-r-xr-- 1 JohnDoe staff 0 Apr 1 23:20 helloWorld.txt
# Revoke read permissions from others $ chmod o-r helloWorld.txt $ ls -l helloWorld.txt
-rw-r-x--- 1 JohnDoe staff 0 Apr 1 23:20 helloWorld.txt
# Give all execution permission $ chmod +x helloWorld.txt $ ls -l helloWorld.txt
-rwxr-x--x 1 JohnDoe staff 0 Apr 1 23:20 helloWorld.txt
# Revoke execution permission from all and give read permission to others $ chmod a-x,o+r helloWorld.txt $ ls -l helloWorld.txt
-rw-r--r-- 1 JohnDoe staff 0 Apr 1 23:20 helloWorld.txt

Specifying exact permissions

In addition to + and -, we can assign precise permissions with the equals symbol (=).

# All can only read
$ chmod =r helloWorld.txt
$ ls -l helloWorld.txt
-r--r--r-- 1 JohnDoe staff 0 Apr 1 23:20 helloWorld.txt

This will change our mode of helloWorld.txt to only read.

$ chmod g=rwx helloWorld.txt
$ ls -l helloWorld.txt
-rw-rwxr-- 1 JohnDoe staff 0 Apr 1 23:20 helloWorld.txt

Note that the settings for umask may alter what you'd expect for the chmod command above. Let's learn about umask in our next lesson to better understand this.

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Ad