Understanding files and directory permissions in Linux with examples

Share this article

Linux is a multiuser system, which means that more than one user can log in to the system at the same time. Users have their own personal home director (there can be users who have no home directory).

Permission on files and directory decides which files/folders are allowed to be accessed by any given user. Therefore understanding this permission is important and we will also check out what are the commands which are used to change the permissions.

Types of permission

First, let’s understand what types of permission are there:

  1. Read (r): It decide that file or directory can be read by user or not.
  2. Write (w): It decide that file or directory can be written by user or not .
  3. Execute (x): if this permission is granted, allow user to run file as executable or a program.

The absence of permission is signified by a dash.

In the user management article, we have seen

How to find Linux File and Directory Permissions

To see permissions on the files and subdirectories in a directory we use ls -l command. This command lists all the content of a directory in a long listing format. Every column in the output shows different useful information.

  • 1st column: permissions on files/directory.
  • 2nd column: total number of links
  • 3rd Column: file owner name
  • 4th column: name of the group
  • 5th column: total size of file in MB
  • 6th 7th 8th column: time of the file modification
  • 9th column: file/directory name

Also, For more commands have a look at files and directory important commands article.

Following is the example of ls -l command. The output will look like this.

test@test:~$ ls -l /home/test/Desktop/cloud_exp/scripts/
-rw-rw-r-- 1 test test 1465 Mai 18 10:35 backup.py
test@test:~$

Permission part of the ls -l command.

The first part or the permission part of the ls -l command has a total of 10 characters. In this section, we will look into the significance of each character.

Apart from the first character, the remaining 9 characters indicate the read, write and execute permissions which can be divided into 3 classes. Have a look at the example picture.

First character

The first character defines the following things:

  • – : means regular file
  • d : means directory
  • p : named pipe aka fifo
  • l : is a symbolic link
  • b : is a block file
  • c : is a character file
  • s : is a socket

Owner’s Right

Positions 2, 3, and 4 are the owner’s read, write and execute permissions.

Group’s Rights

Positions 5, 6, and 7 are the group’s read, write and execute permissions.

Other’s Rights:

Positions 8,9 and 10 are the rest of the user’s read write and execute permissions.

Examples.

Let’s first look at the file example here and try to figure out what type of permission it has on the file.

test@test:~$ ls -l /home/test/Desktop/cloud_exp/scripts/
-rw-rw-r-- 1 test test 1465 Mai 18 10:35 backup.py
test@test:~$

The above example is showing permission for the test user and test group on backup.py file. In the below snapshot you can see what permissions are assigned for the backup.py file.

In the next example, we will look into directory examples. The following example is showing permissions on the scripts directory.

The important point to note here is, permissions on the directory show the highest permissions assigned to any of the files in the directory.

test@test:~$ ls -l /home/test/Desktop/cloud_exp/
drwxrw-r-- 3 test test 4096 Mai 18 10:35 scripts

Leave a Comment

Your email address will not be published. Required fields are marked *