Linux - 11 - access rights

Unix systems use a simple access rights model. However if you are a newcomer to Unix it isn't immediately intuitive.

Principles

There are three different types of access in Unix, read, write, and execute, denoted by the letters r, w, and x respectively.

Each file has exactly one owner, and belongs to a exactly one group.

Each file stores rights for the owner, group, and others, denoted by the letters u, g, and o respectively

To find out what the rights of a file is use ls -l file.txt. The rights are listed as a 9 character string. The first three characters are for the owner, the next three for the group, and the final three for "others". If the particular right is allowed, (r, w or x) then that character is displayed, if not a - is displayed. Note the string is prefixed by the directory attribute, 'd' if the file is a directory, - if not.

For example the following shows that the file is readable by the owner (adam), the group (users), and by others. It is only writable by the owner, and is not executable by anyone.

ls -l foo.txt
-rw-r--r--    1 adam   users   5 Jul 19 13:10 foo.txt

Changing access rights

chmod changes access rights for a file. The syntax is chmod <changes> <file> Changes are a comma separated list. Each entry in the list consists of the concatanation of the following:



Examples

# remove group access to one file
chmod g-rwx foo.txt

# remove group access to all files in a directory
chmod -R g-rwx foo/

# grant executable access to everyone
chmod a+x foo.exe

# remove write and executable access from group and others
chmod go-rx foo.exe

Changing the owner

chown <user> <file> allows you to change the owner of a file.

Changing the group

chgrp <group> <file> allows you to change the group the file belongs to.