After getting my question answered here and doing some research about the outcome I found an article which explains it all very well. I would like to share some parts of this article here for future references.
In order to use
chmod to change permissions of a file or directory, you will first need to know what the current mode of access is. You can view the contents of a directory in the terminal by
cd to that directory and then use:
$ ls -l
-l switch is important because using
ls without it will only display the names of files or folders in the directory.
Below is an example of using
ls -l on my home directory:
drwxr-xr-x 2 peter users 4096 Jul 5 21:03 Desktop
drwxr-xr-x 6 peter users 4096 Jul 5 17:37 Documents
drwxr-xr-x 2 peter users 4096 Jul 5 13:45 Downloads
drwxr-xr-x 2 peter users 4096 Jun 24 03:36 Movies
drwxr-xr-x 2 peter users 4096 Jun 24 03:38 Music
drwxr-xr-x 2 peter users 4096 Jun 26 00:09 Pictures
-rw-r--r-- 1 peter users 354 Jul 6 17:15 chmodtest
What the columns mean
The first column is the type of each file:
- denotes a normal file.
d denotes a directory, i.e. a folder containing other files or folders.
p denotes a named pipe (aka FIFO).
l denotes a symbolic link.
The letters after that are the permissions, this first column is what we will be most interested in. The second one is how many links there are in a file, we can safely ignore it. The third column has two values/names: The first one (in my example 'peter') is the name of the user that owns the file. The second value ('users' in the example) is the group that the owner belongs to (Read more about groups).
The next column is the size of the file or directory in bytes and information after that are the dates and times the file or directory was last modified, and of course the name of the file or directory.
What the permissions mean
The first three letters, after the first
d, are the permissions the owner has. The next three letters are permissions that apply to the group. The final three letters are the permissions that apply to everyone else.
Each set of three letters is made up of
r is always in the first position,
w is always in the second position, and
x is always in the third position.
r is the read permission,
w is the write permission, and
x is the execute permission. If there is a hyphen (
-) in the place of one of these letters it means the permission is not granted, and if the letter is present then it is granted.
In case of folders the mode bits can be interpreted as follows:
r (read) stands for the ability to read the table of contents of the given directory,
w (write) stands for the ability to write the table of contents of the given directory (create new files, folders; rename, delete existing files, folders) if and only if execute bit is set. Otherwise, this permission is meaningless.
x (execute) stands for the ability to enter the given directory with command cd and access files, folders in that directory.
Changing permissions using the chmod command
chmod is a command in Linux and other Unix-like operating systems. It allows you to change the permissions (or access mode) of a file or directory.
You can alter permissions in two different ways:
To change the permissions-or access mode of a file, we use the chmod command in a terminal. Below is the command's general structure:
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 owns 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.
The permissions are the same as already discussed (
The chmod command lets us add and subtract permissions from an existing set using + or - instead of =. This is different to the above commands, which essentially re-write the permissions (i.e. to change a permission from
rw-, you still need to include
r as well as
w after the
= in the
chmod command. If you missed out
r, it would take away the
r permission as they are being re-written with the =. Using + and - avoid this by adding or taking away from the current set of permissions).
chmod can also set permissions using numbers.
Using numbers is another method which allows you to edit the permissions for all three owner, group, and others at the same time. This basic structure of the code is this:
chmod xxx file/directory
Where xxx is a 3 digit number where each digit can be anything from 1 to 7. The first digit applies to permissions for owner, the second digit applies to permissions for the group, and the third digit applies to permissions for all others.
In this number notation, the values r, w, and x have their own number value:
To come up with a three digit number you need to consider what permissions you want an owner, group, and user to have, and then total their values up. For example, say I wanted to grant the owner of a directory read-write and execution permissions, and I wanted to group and everyone else to have just read and execute permissions. I would come up with the numerical values like so:
Owner: rwx = 4+2+1=7
Group: r-x = 4+0+1=5 (or just 4+1=5)
Other: r-x = 4+0+1=5 (or just 4+1=5)
Final number = 755
$ chmod 755 filename
This is the equivalent of using the following:
chmod u=rwx filename
chmod go=rx filename
Most folders/directories are set to 755 to allow reading and writing and execution to the owner, but deny writing to everyone else, and files are normally 644 to allow reading and writing for the owner but just reading for everyone else, refer to the last note on the lack of x permissions with non executable files - its the same deal here.