← All lessons
Files and permissions · Lesson 4 of 7

Changing permissions

chmod (change mode) sets permissions. Only the file's owner (or root) can use it on a file.

The letter way says who, then + to add, - to take away or = to set exactly, then which permissions. Who is u (user, the owner), g (group), o (others) or a (all three).

$ chmod u+x backup.sh      # the owner may run it
$ chmod go-w notes.txt     # group and others may no longer write
$ chmod a=r readme.txt     # everyone may read, nobody may write

The number way gives each set a digit, adding up read = 4, write = 2 and execute = 1. So 7 is rwx (4+2+1), 6 is rw- (4+2), 5 is r-x (4+1), 4 is r-- and 0 is nothing. Three digits cover owner, group and others.

$ chmod 755 backup.sh     # rwxr-xr-x
$ chmod 644 notes.txt     # rw-r--r--
$ chmod 600 secret.key    # rw-------

Those three are the ones you'll use most: 755 for programs and folders, 644 for ordinary files, 600 for private things like keys. chmod -R changes a folder and everything in it.

Avoid chmod 777. It lets anyone on the machine change or replace the file, and it's almost never the real fix for a Permission denied error. Work out who needs access and give them just that.

Practice

Let the owner run deploy.sh, using letters.

More info

Quick reference

chmod u+x fileAdd execute for the owner.
chmod go-w fileTake write from group and others.
u g o aOwner, group, others, all.
r = 4, w = 2, x = 1Add them up for each set.
755, 644, 600Programs and folders; files; private files.
chmod -RA folder and everything in it.

The other Linux courses