DEV Community

Mahima Bhardwaj
Mahima Bhardwaj

Posted on

How to use chmod | Understanding File & Directory Permissions

Youtube

File permissions are key to managing a Linux system. They control who can access files and folders, helping keep the data safe and secure.

Whether you’re a system administrator, developer, or just a regular user, it’s important to know how file permissions work. This article will explain file permissions in simple terms.

Image description

In first step we will list all files and directories in long format using ls -l.

  1. ls- to list all files and directories
  2. -l- this flag is used to list data in long format
-rw-rw-r-- 1   rps rps 03 Aug 24  03:21 file2.txt
Enter fullscreen mode Exit fullscreen mode

If first chacater is hyphen then current object is file. we will divide current permission into 4 group.

  • → hyphen is denoting one group rw- -> rw- is denoting second group rw- -> rw- is denoting third group r- — -> r — is denoting fourth group.

second group is denoting user permission. user has access to read and write only.

Third Group is denoting group access. group has access to read and write only.

fourth group is denoting others. others has acces to read only.

Note: here user and group has same name i.e rps.

Image description

file2.txt
In this image we can clearly see user has not access to execute the file2.txt. As we can see in image permission denied.

To give permission to use user to execute file2.txt we will use chmod command.

Before using chmod command first we understand chmod command .

The chmod command in Linux is used to change the permissions of a file or directory. Permissions control who can read, write, or execute the file.

r (read) — Permission to view the contents of a file or list the contents of a directory.
w (write) — Permission to modify the contents of a file or make changes within a directory (like creating or deleting files).
x (Execute): Permission to run a file as a program or script. For directories, it allows entering the directory.

Execute Permission to user
In the above Image we have given access to user to execute file2.txt.

chmod u+x file2.txt

Now user has permission to execute file2.txt
./file2.txt
when you will execute file2.txt it will successfully execute.

To give the group permission to execute file2.txt

chmod g+x file2.txt

Group get access to execute file2.txt
Now we will grant permission to others to write to and execute file2.txt.

chmod o+wx file2.txt

Now others has access to write and execute file2.txt.

Now we want to revoke the write and execute permissions from the group for file2.txt.

chmod g-wx file2.txt

Enter fullscreen mode Exit fullscreen mode

group had only access to read permission
Now we want to revoke the write and execute permissions from the user for file2.txt.

User has no access to write into file2.

chmod u-wx file2.txt
Enter fullscreen mode Exit fullscreen mode

txt
Now I will give write permission to user for file2.txt

chmod u+w file2.txt

Now user has permission to write into file2.txt.

Summary for file permission
Wrapping up
Understanding Linux file permissions (how to find them, read them, and change them) is an important part of maintaining and securing your systems.

Top comments (1)

Collapse
 
codycodes profile image
Cody Antonio Gagnon

Badiya! Bahut helpful tutorial.