In this article, we would discuss how to set permissions on files & directories using chmod in Ubuntu distribution. chmod is a command-line utility, which is used to change file mode bits.
But, first we need to discuss a bit about file & directory permissions itself.
We can long list the contents of a file & directory using ls command with -l option. For instance, lets say we have a file ABC.txt then – issue the following in terminal –
ls -l ABC.txt
It would return us the output –
-rw-rw-r-- 1 user group 6 Feb 14 00:00 ABC.txt
With ls -l command, we can view the permissions of an object.
where,
-rw-rw-r--
these are the permissions of the object
1 represent the link count i.e. total number of hard-links an object has
user – who owns the object
group – group owner of object
6 Feb 14 00:00 – last modification date
ABC.txt – object name itself
Now, zeroing down on permission set itself. We define permissions for three set of users namely – user, group and rest others. So, in rw-rw-r--
rw-
first three characters – read & write permissions for user
rw-
next three characters – read & write permissions for group
r--
last three characters – only read permissions for others
here, r stands for read, w for write and x for execute
Set permissions on files & directories using chmod in Ubuntu
First, we will discuss user related permissions – this will make modifications to first three characters aforementioned.
To add permissions for a user, we can use following combinations –
chmod u+r ABC.txt chmod u+w ABC.txt chmod u+x ABC.txt
where,
u+r is to allow a user to read the contents of the object
u+w is to allow a user to make modifications to the object
u+x lets the users to execute the object
To remove permissions for a user,
chmod u-r ABC.txt chmod u-w ABC.txt chmod u-x ABC.txt
where,
u-r is to disallow a user to read the contents of the object
u-w is to disallow a user to make modifications to the object
u-x disallows a user to execute the object
Similarly, to add permission for a group –
chmod g+r ABC.txt chmod g+w ABC.txt chmod g+x ABC.txt
Next, to remove permissions for a group –
chmod g-r ABC.txt chmod g-w ABC.txt chmod g-x ABC.txt
Lastly, for all others – add permissions –
chmod o+r ABC.txt chmod o+w ABC.txt chmod o+x ABC.txt
Remove permissions for all others –
chmod o-r ABC.txt chmod o-w ABC.txt chmod o-x ABC.txt
It is worth mentioning here that, we can use combinations to add permissions to a user and group. For instance,
chmod ug+r ABC.txt chmod u+rw ABC.txt chmod ug-r ABC.txt chmod u-rw ABC.txt
Similarly, for others also –
chmod o+rw ABC.txt chmod o-rx ABC.txt
In the same way, we can perform aforementioned operations on directory also. Just replace file_name with directory_name.
In conclusion, we have discussed how to set permissions on files & directories using chmod in Ubuntu distribution.