Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

linux
communitycreator

What are file and directory permissions in Linux?

Kedar Kodgire

Overview

In Linux, the file permissions help to access, write, and execute the files. These permissions restrict the other users from performing an unauthorized activity, and thus they maintain security.


Permissions

There are three principal permissions in Linux.

Read

The read permission provides the capability to read/view the file's contents. We execute this permission with the letter r.

Write

The write permission provides the ability to modify the file, i.e., add or remove contents from the file. We execute this permission with the letter w.

Execute

The execute authorization offers the ability to run the file. For example, you'll need this permission to run a shell script. The letter x describes this permission.


User classes

The Linux permissions model has three user categories. Every user class has its reading, writing, and executing permissions set. Here're the three categories:

  1. Owner: The owner category provides info about an owner's actions.
  2. Group: The group describes a group member's actions associated with the file/directory.
  3. Other: Similarly, the other category describes actions that the other member can perform.

Checking file permissions

We use the ls command to check the file permissions with an extension l indicating the long list format. Here's a syntax and an example.

ls -l
Syntax
root@educative:/# ls -l
total 45
drwxr-xr-x   1 root root  1024 Dec 31  2018 bin
drwxr-xr-x   2 root root  1024 Apr 12  2016 boot
drwxr-xr-x   5 root root   340 May 11 20:18 dev
drwxr-xr-x   1 root root  4096 May 11 20:18 etc
-rw-r--r--   1 root root     6 May 11 20:18 file.txt
drwxr-xr-x   2 root root  1024 Apr 12  2016 home
drwxr-xr-x   1 root root  1024 Dec 31  2018 lib
drwxr-xr-x   2 root root  1024 Nov 13  2018 lib64
drwxr-xr-x   2 root root  1024 Nov 13  2018 media
drwxr-xr-x   2 root root  1024 Nov 13  2018 mnt
-rw-r--r--   1 root root 11488 Dec 31  2018 nodesource_setup.sh
drwxr-xr-x   2 root root  1024 Nov 13  2018 opt
dr-xr-xr-x 122 root root     0 May 11 20:18 proc
drwx------   1 root root  4096 May 11 20:18 root
drwxr-xr-x   1 root root  1024 Nov 13  2018 run
drwxr-xr-x   1 root root  1024 Dec 31  2018 sbin
drwxr-xr-x   2 root root  1024 Nov 13  2018 srv
dr-xr-xr-x  13 root root     0 May 11 20:18 sys
drwxrwxrwt   1 root root  4096 May 11 20:18 tmp
drwxrwxrwx   2 root root  4096 May 11 20:18 usercode
drwxr-xr-x   1 root root  1024 Nov 13  2018 usr
drwxr-xr-x   1 root root  1024 Nov 13  2018 var
Example

Explanation

The ls -l command returned us information about 45 contents within the directory. The first column in the results indicates the permissions for the owner, group, and other categories.

  • Line 5: This represents the file (file.txt).
-rw-r–-r--   1 root root     6 May 11 20:18 file.txt
  1. The first character - indicates that this is a file.
  2. The following three characters rw- indicate that the owner can read and write the file.
  3. The following three characters r-- indicate that the group members only have read permission.
  4. Finally, the last three characters r-- indicate that the other members will only have read permission.
Terminal 1
Terminal

RELATED TAGS

linux
communitycreator
RELATED COURSES

View all Courses

Keep Exploring