Wednesday, 12 May 2010

Setting file permissions in linux

File permissions for a file or a directory are grouped after "user" (u), "group" (g) and "other" (o), and permission to read (r), write (w) and execute (x). In your home directory, you are registered as the user of all the files, and by default you have read, write and execute premission to all files.

The file permissions can be seen by typing "ls -l" in the terminal. The permissions are then listed as a string, for example "drwxr-xr-x". This string consists of a "d" plus three groups of "rwx", corresponding to the user, group and others, respectively. The "-" sign indicates "no permission". For the example above, the user has all permissions, while the group and others only have read and execute permissions.

File permissions are set by the "chmod" command. One way to use this command is to add (+) or remove (-) permissions. For example, the following command adds permission for user and group to write and execute "somefile.txt"

chmod ug+wx somefile.txt

The same syntax is used for files and folders. To set permissions for all files and subfolders of a folder, add a "-R" ("recursive") switch. For example,

chmod ugo-w -R SomeFolder

will remove all write permissions for "SomeFolder" and all its contents.

This approach is based on changing the file permissions relative to the way that they were before. The file permissions can also be set "absolutely". It is common to do this by coding r,w and x as numbers 4, 2, and 1. The file permission is then identified by the sum of the numbers, for example "r-x" = 4+1 = 5. For example, to let the user have all permissions while restricting write access for group and others, "rwxr-xr-x" can be translated to 755, and the chmod syntax is as follows:

chmod 755 file.txt

For further information on file permissions and how to change them, consult the Ubuntu Community Documentation.


Post a Comment