DEV Community

Mahima Bhardwaj
Mahima Bhardwaj

Posted on

4 1

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.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (1)

Collapse
 
codycodes profile image
Cody Antonio Gagnon

Badiya! Bahut helpful tutorial.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more