File Permissions in Ubuntu

Ability to set file permissions for individual users or user-groups is one of the most sought after features of Linux. If you are system admin for a school, college or a company you work for then proper file permission setting is among the most vital tasks.

The command used to modify file permissions is chmod, short for change mode of a file. You can also use Nautilus file browser to change the file permissions. I will cover the details after a little background on the file permissions. To find the permission settings for a file, issue the following command int he terminal from the directory where your file is.

ls -l
cmd output

As you can see, the first column in the output has some strange looking character sets. This set is the file permissions for that file (directory). The third column is the owner of the file (directory) and the fourth column is the default group of the file (directory). We can ignore all the other columns at this point.

In the first column, each set would be 10 character wide. The very first character is a d for a directory or just a (hyphen) for files. After that, the next three characters are the permissions for the owners account. The order is read-write-execute. If the superuser can read the file it would display r otherwise just a . Similarly for write and execute w and x would be displayed. The next three characters are permissions for all the other users belonging to the file group (from the fourth column in the output above). Final three characters are the permissions for everyone not part of the group. Superusers (root accounts) can always override all the settings mentioned here and none of the permissions apply to them.

Changing Permissions – The Easy Way

The easiest way, as I said, is to just change the file permissions using Nautilus but it’s time consuming if you want to change the permissions of a lot of files. Command line way may seem tedious to begin with but it’s the faster way once you know your way around. To change the permissions in Nautilus, right click on any file (directory). Go to properties and then to permissions. Change the permissions and click ok. That’s it.

File Permissions

Changing Permissions – The Faster Way

To change file permissions via the terminal, you can use the chmod command. To change permissions of a file, enter the following command in the terminal.

chmod ABC path/to/file

Here, ABC is a 3 digit number which is the decimal representation of the file permissions. For example, r-x means 101 in binary which translates to 5 in decimal. So, if you want everyone to have just read and execute access to files and only the owner has the write access to files then the permissions are rwx,r-x,r-x which is 111,101,101. That translates to 7,5,5 (comma is only given for clarity here). So, the command would now be,

chmod 755 path/to/file

Don’t worry if you don’t know how binary works, you will get used to it. Easiest way is add 4 for read, 2 for write and 1 for execute. So, considering the examples above, rwx = 4 + 2 + 1 = 7 and r-x = 4 + 0 + 1 = 5. You’ll get used to this. There is another way to to this, the text method (which I don’t prefer).

chmod who=permissions filename

Where Who is any from a range of letters, and each signifies who you are going to give the permission to. They are as follows:

u – The user that own the file.
g – The group the file belongs to.
o – The other users i.e. everyone else.
a – all of the above – use this instead of having to type ugo.

And then you can directly write rwx in front of the equal sign. For example,

chmod g=rx

Next post will cover setting default permissions to files you create using umasks.


Tags: , , , , , , ,
Copyright 2017. All rights reserved.

Posted October 20, 2011 by Aditya in category "Linux