DEV Community

Cover image for Linux Insight Blogs: chmod
Ankur Singh
Ankur Singh

Posted on • Edited on

1 1 1 1 1

Linux Insight Blogs: chmod

Introduction

Welcome 👋 to this blog. In this blog we will gonna be learning about the chmod a command-line utility on Unix/Linux systems. A command-line utility is a program or tool that you run using CLI(Command Line Interface) instead of GUI(graphical user interface)

chmod

chmod stands for change mode. It's a command-line utility which used to change the permissions of files and directories. In Unix/Linux or in general computer Science we can define permission following:

  • read(r), view file content

  • write(w), modify file content

  • execute(x), run as a program

With the help of this command, we can change the permissions of the files assigned to three user groups.

In Unix/Linux systems we have three user classes:

  • Owner(u), user who owns a file

  • group(g), users in the file group

  • other(o), other users

Let's get our hands dirty with code

Open your terminal by pressing CTRL + Alt + T.

We have 2 ways to use this commands

1. With symbols

In this we will be using these symbols +, - and = symbols to modify the permissions. The meaning of the symbols + (Add permission), - (Remove permission) and = (set exact permissioin).

Let's start typing the command in the terminal:

I have created a three files in the blog folder.

ankur@ankur:~/Desktop/blog$ ls
file1.txt  file2.txt  file3.txt
Enter fullscreen mode Exit fullscreen mode

Let's check the permission of all the files by executing the ls commands with -l flag.

ankur@ankur:~/Desktop/blog$ ls -l
total 0
-rw-rw-r-- 1 ankur ankur 0 Jun 28 23:15 file1.txt
-rw-rw-r-- 1 ankur ankur 0 Jun 28 23:15 file2.txt
-rw-rw-r-- 1 ankur ankur 0 Jun 28 23:15 file3.txt
Enter fullscreen mode Exit fullscreen mode

The -rw-rw-r-- part, which represents file permissions in Linux. This string has 10 characters, structured like this:

[Type][Owner][Group][Others]
 -     rw-     rw-    r-- 
Enter fullscreen mode Exit fullscreen mode
  • Give execute permission to the owner
ankur@ankur:~/Desktop/blog$ chmod u+x file1.txt 
ankur@ankur:~/Desktop/blog$ ls -l
total 0
-rwxrw-r-- 1 ankur ankur 0 Jun 28 23:15 file1.txt
Enter fullscreen mode Exit fullscreen mode

Now you can see the x in the owner string, which means it has execute privilege.

  • Removing execute permission from the owner
ankur@ankur:~/Desktop/blog$ chmod u-x file1.txt 
ankur@ankur:~/Desktop/blog$ ls -l
total 0
-rw-rw-r-- 1 ankur ankur 0 Jun 28 23:15 file1.txt
Enter fullscreen mode Exit fullscreen mode

Now you can see the x vanishes in the owner string.

  • Set exact permission explicitily
ankur@ankur:~/Desktop/blog$ chmod u=rwx,g=r,o=wr file1.txt 
ankur@ankur:~/Desktop/blog$ ls -l
total 0
-rwxr--rw- 1 ankur ankur 0 Jun 28 23:15 file1.txt
Enter fullscreen mode Exit fullscreen mode

You can see the string is changed.

2. With Numbers

Each permission is mapped to a numeric value.

r (read)    4
w (write)   2
x (execute) 1
Enter fullscreen mode Exit fullscreen mode

So, everything worked the same expect in this method we passed the 3 string 1st one for owner, 2nd one for group and the 3rd one for others

for example

ankur@ankur:~/Desktop/blog$ chmod 755 file1.txt 
ankur@ankur:~/Desktop/blog$ ls -l
total 0
-rwxr-xr-x 1 ankur ankur 0 Jun 28 23:15 file1.txt
Enter fullscreen mode Exit fullscreen mode
755 will be breaked into(7, 5, 5)
7 -> 4+2+1 (rwx)
5 -> 4+1 (r-x)
5 -> 4+1 (r-x) 
Enter fullscreen mode Exit fullscreen mode

You can visit link to see the mapping of permission with the numeric.

🎉 You nailed it

You’ve now learned how to use the chmod command to change the permission of the files with two methods symbolic and numerical. It’s time to open your terminal and try these commands on your own system. Play around, explore, and see what's happening under the hood of your machine. Got something cool or unexpected? Share your best use case of chmod commands in the comment section.

Let's connect
📧 Email: ankursingh91002@gmail.com
🔗 LinkedIn: Ankur Singh
🔗 Twitter: @ankur_136

Building Security into Each Phase of the Mobile App Lifecycle

Building Security into Each Phase of the Mobile App Lifecycle

A mobile app security breach can not only result in loss of data but also loss of consumer trust. To prevent breaches, DevOps teams are starting to adopt a security-focused mindset, building it into the SDLC earlier than before.

Read more

Top comments (0)

Pulumi Azure GitHub Image

"Give Pulumi a shot and you will never look back" - Engin

Build and ship infrastructure faster using languages you know and love. Use Pulumi’s open source SDK to provision infrastructure on any cloud, and securely and collaboratively build and manage infrastructure using Pulumi Cloud.

Get Started

👋 Kindness is contagious

Take a moment to explore this thoughtful article, beloved by the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A heartfelt "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay