Chmod

From Antalya
Jump to: navigation, search
Main use

chmod stands for CHange MODe and will allow you to change the permissions for a set of files and directories.

There are three main permissions:

  • r Read
  • w Write. Note that, in UNIX, if you write, you can also delete something.
  • x Execute. Confusingly for directories, the execute permission will allow you to change to that directory.

It is possible to use a numerical shortcut for these permissions. In the numerical format, read is 4, write is 2 and execute is 1. So when you say 7 for a permission, you mean you can read, write, execute and 5 is read, execute but not write.

There are also three main levels of permissions, that can be set independently.

  • user: this is your user, the owner of the file/directory
  • group: this is the group you belong to, the group of the file/directory
  • others: anyone else that has access to the computer but is neither you nor in your group.

So the file permissions are set by specifying the permissions for all three levels one after another.

chmod 644 README.txt

Will allow your user to read and write the file (6) and people in your group, as well as anyone else will only be allowed to read. (one of the most common settings). Note that for directories, you need to set the execute permission, otherwise you will not be able to enter the directory.

chmod 750 temp

Will allow your user to read/write and change to the directory called temp and users that belong to the same group can read and change to the directory, but not create new files inside the directory, or delete the directory contents.

It is also possible to specify the groups and permissions symbolically.

chmod -R u+w,go-w * 

It may look cryptic, but -R tells to go recursively, u+w tells it to add write permissions for the user and og-w tells to remove the write permission from group and others.

permissions
  • 755: user read/write/execute, others and group just read/execute
  • 644: user read/write, others and group just read
  • 700: user read/write/execute, others and group no rights

Note that there can be many silly permissions that are possible, but quite senseless

  • 002: only people that do not own the file, or do not belong to the group may write/delete the file. They can not read it.
  • 123: user can execute, group can write and the rest of the world can both read and write.

Tidbits

  • Your default permissions are given by: umask. The bits that are set in the umask are by default not set in the files/directories you create.
umask 022

will create by default directories 755 and files 644 whereas

umask 027

will create by default files that are not accessible by others, and will only allow your users in your group read permissions.