Friday, November 22, 2013

File Permissions (chmod) on Linux

chmod is a Linux command that will let you "set permissions" (aka, assign who can read/write/execute) on a file.
Usage:
chmod permissions file

Or
Usage:
chmod permission1_permission2_permission3 file

When using chmod, you need to be aware that there are three types of Linux users that you are setting permissions for. Therefore, when setting permissions, you are assigning them for "yourself", "your group" and "everyone else" in the world. These users are technically know as:
  • Owner
  • Group
  • World

Therefore, when setting permissions on a file, you will want to assign all three levels of permissions, and not just one user.

Think of the chmod command actually having the following syntax:
chmod owner group world FileName

Now that you understand that you are setting permissions for THREE user levels, you just have to wrap your head around what permissions you are able to set!

There are three types of permissions that Linux allows for each file.
  • Read
  • Write
  • Exeute

Putting it all together:

So, in laymen terms, if you wanted a file to be readable by everyone, and writable by only you, you would write the chmod command with the following structure.
COMMAND : OWNER : GROUP : WORLD : PATH
chmod read & write read read FileName 
chmod 6 4 4 myDoc.txt

What are those numbers?

Computers like numbers, not words. Sorry. You will have to deal with it. Take a look at the following output of `ls -l`
[root@demo]$ ls -l
-rw-r--r-- 1 gcawood iqnection 382 Dec 19 6:49 myDoc.txt

You will need to convert the word read or write or execute into the numeric equivalent (octal) based on the table below
  • 4 – read (r)
  • 2 – write (w)
  • 1 – execute (x)

Examples.

chmod 400 mydoc.txt – read by owner
chmod 040 mydoc.txt – read by group
chmod 004 mydoc.txt – read by anybody (other)
chmod 200 mydoc.txt – write by owner
chmod 020 mydoc.txt – write by group
chmod 002 mydoc.txt – write by anybody
chmod 100 mydoc.txt – execute by owner
chmod 010 mydoc.txt – execute by group
chmod 001 mydoc.txt – execute by anybody

It is also work noting that you can use shortcuts like the following:
chmod ug+rw file
chmod o-r file
chmod ugo=rwx file

Note: never set things to 777 unless you know what you do.