Linux OS Basics – Part 4

Permissions

File Permissions

As we learned previously, files have different permissions or file modes. Let’s look at an example:

pineturtle@DESKTOP-ESO0AIU:~$ ls -l ~/Desktop
drwxr-xr-x 2 pine turtle 4096 Apr 28 11:45 .

There are four parts to a file’s permissions. The first part is the filetype, which is denoted by the first character in the permissions, in our case since we are looking at a directory it shows d for the filetype. Most commonly you will see a  for a regular file.

The next three parts of the file mode are the actual permissions. The permissions are grouped into 3 bits each. The first 3 bits are user permissions, then group permissions and then other permissions. I’ve added the pipe to make it easier to differentiate.

d | rwx | r-x | r-x 

Each character represents different permission:

  • r: readable
  • w: writable
  • x: executable (basically an executable program)
  • -: empty

So in the above example, we see that the user pine has read, write and execute permissions on the file. The group turtle has read and executed permissions. And finally, the other users (everyone else) has read and executed permissions.

Modifying Permission

We can use chmod command to change the permissions. Decide what permission you want to change like, users, groups or others. We then use operators + and – to add or remove the permissions.

Adding permission bit on a file

pineturtle@DESKTOP-ESO0AIU:/tmp$ ls -l
-rw-r--r-- 1 pineturtle pineturtle   60 Apr 28 21:32 text.txt
pineturtle@DESKTOP-ESO0AIU:/tmp$ chmod u+x text.txt
pineturtle@DESKTOP-ESO0AIU:/tmp$ ls -l
-rwxr--r-- 1 pineturtle pineturtle   60 Apr 28 21:32 text.txt

Notice the difference in the first part of the permissions column when we did the ls for the first time we have “-rw” and after changing the permission for the file we see “-rwx”

Removing permission bit on a file

pineturtle@DESKTOP-ESO0AIU:/tmp$ ls -l
-rwxr--r-- 1 pineturtle pineturtle   60 Apr 28 21:34 text.txt
pineturtle@DESKTOP-ESO0AIU:/tmp$ chmod u-x text.txt
pineturtle@DESKTOP-ESO0AIU:/tmp$ ls -l
-rw-r--r-- 1 pineturtle pineturtle   60 Apr 28 21:34 text.txt

Adding multiple permission bits on a file

pineturtle@DESKTOP-ESO0AIU:/tmp$ ls -l
-rwxr--r-- 1 pineturtle pineturtle   60 Apr 28 21:34 text.txt
pineturtle@DESKTOP-ESO0AIU:/tmp$ chmod ug+x text.txt
pineturtle@DESKTOP-ESO0AIU:/tmp$ ls -l
-rwxr-xr-- 1 pineturtle pineturtle   60 Apr 28 21:32 text.txt

We can also numerical expressions

The numerical representations are seen below:

  • 4: read permission
  • 2: write permission
  • 1: execute permission
pineturtle@DESKTOP-ESO0AIU:/tmp$ chmod 755 text.txt
pineturtle@DESKTOP-ESO0AIU:/tmp$ ls -l
-rwxr-xr-x 1 pineturtle pineturtle   60 Apr 28 21:32 text.txt

What the?! Where did 7 5 5 come from?

Let’s do the math here:

7 = 4 + 2 + 1, so 7 is the user permissions and it has read, write and execute permissions
5 = 4 + 1, the group has read and execute permissions
5 = 4 +1, and all other users have read and executed permissions

Hope you get the idea. One thing to note or keep in mind is to please ve very cautious in using chmod it can be dangerous sometimes as we sometimes end up exposing sensitive files to everyone.

Ownership Permission

We can modify the owner of the file or folder using chown.

Try the below commands and see how these commands work to change the permissions.

pineturtle@DESKTOP-ESO0AIU:/tmp$ sudo groupadd group1
pineturtle@DESKTOP-ESO0AIU:/tmp$ useradd user1

changing user permission

pineturtle@DESKTOP-ESO0AIU:/tmp$ sudo chown user1 text.txt
pineturtle@DESKTOP-ESO0AIU:/tmp$ ls -
-rwxr-xr-x 1 user1      pineturtle   60 Apr 28 21:32 text.txt

changing group permission


pineturtle@DESKTOP-ESO0AIU:/tmp$ sudo chgrp group1 text.txt
-rwxr-xr-x 1 user1      group1       60 Apr 28 21:32 text.txt

changing both user and group permission

pineturtle@DESKTOP-ESO0AIU:/tmp$ sudo chown user1:group1 text.txt
pineturtle@DESKTOP-ESO0AIU:/tmp$ ls -l
-rwxr-xr-x 1 user1      group1       60 Apr 28 21:32 text.txt

Guess this is clear to you.

See you in the next part 🙂

Leave a comment