Day 39 - File Permissions in Linux

This is the 8th post in the Linux Series and as I mentioned in the last post this post will be about the file permissions in the Linux FileSystem. The permissions are rights to access files or read, write or modify them and in Linux these permissions are allowed or not allowed by the chmod command and to to do this one must have the root access to modify the file permissions. Now to the command.

Permissions to Files

chmod command short for change mode is used to change permissions of files and directories. First I will navigate to my /tmp directory and hit ls command with list -l flag and it will show the following output.

root@User:/tmp$ ls -l
total 4
-rw-r--r-- 1 root root    0 Feb  7 14:04 file.txt
drwxr-xr-x 5 root root 4096 Jan 19 00:10 tutorial

Here the first ten character tell us about the file permission and who can have access to and what kind of access to the file. The first character fot the file type of if it is a directory and the next nine characters are a set of three characters.

  • d - the first character tells us if it is a directory or a file - if it is a file.
  • r - r for the access to read the file
  • w - w for the access to write to the file
  • x - x for the access to execute the file.

The first three character in the nine permissions are the current logged in user and the second three are for the group the the third are for all the users.

-rw-r--r-- 1 root root    0 Feb  7 14:04 file.txt

The first character - tells us that this is a file, next three characters rw- tells us that this user can read and write to this file but not execute the file, the next three characters r-- tells us this group can only read the file but cannot write to it or execute. and the next three characters r-- are also same for all the users.

Changing permissions with chmod

To change the permissions to file for the users and groups we use chmod command. Now how to use the chmod command.

root@User:/$ chmod [who]symbol[permissions] file
  • who - user or the group being given the permissions
    • u for the user
    • g for the group
    • o for the all the other users
    • a for all the users and groups
  • symbol - add or remove permissions
    • + for appending to current permissions
    • - for removing from current permissions
    • = for setting permissions for all
  • permissions - the permissions to give
  • file - file name

Now modifying permissions for a file but first I remove all the permissions of the file by typing the commad chmod a-rwx file.txt

Removing all the permissions

root@User:/tmp$ ls -l
-rw-r--r-- 1 root root    0 Feb  7 14:04 file.txt
root@User:/tmp$ chmod a-rwx file.txt
root@User:/tmp$ ls -l
---------- 1 root root    0 Feb  7 14:04 file.txt

Adding Permissions

For all the users using a with + for adding and rx for read and execute permissions.

root@User:/tmp$ ls -l
---------- 1 root root    0 Feb  7 14:04 file.txt
root@User:/tmp$ chmod a+rx file.txt
root@User:/tmp$ ls -l
-r-xr-xr-x 1 root root    0 Feb  7 14:04 file.txt

Removing Permissions

For the group and other users only using g and o with - for removing and

root@User:/tmp$ ls -l
-r-xr-xr-x 1 root root    0 Feb  7 14:04 file.txt
root@User:/tmp$ chmod g-x file.txt
root@User:/tmp$ ls -l
-r-xr--r-x 1 root root    0 Feb  7 14:04 file.txt
root@User:/tmp$ ls -l
-r-xr--r-x 1 root root    0 Feb  7 14:04 file.txt
root@User:/tmp$ chmod o-x file.txt
root@User:/tmp$ ls -l
-r-xr--r-- 1 root root    0 Feb  7 14:04 file.txt

Setting Permissions For All

root@User:/tmp$ ls -l
-r-xr--r-- 1 root root    0 Feb  7 14:04 file.txt
root@User:/tmp$ chmod a=r file.txt
root@User:/tmp$ ls -l
-r--r--r-- 1 root root    0 Feb  7 14:04 file.txt
root@User:/tmp$ chmod a=x file.txt
root@User:/tmp$ ls -l
---x--x--x 1 root root    0 Feb  7 14:04 file.txt
root@User:/tmp$ chmod a=rwx file.txt
root@User:/tmp$ ls -l
-rwxrwxrwx 1 root root    0 Feb  7 14:04 file.txt

zainscizainsci