avatarYang Zhou

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2740

Abstract

-</b>” stands for a regular file which is our case, and another two possible characters are “<b>d</b>” for a directory and “<b>l</b>” for a <a href="https://readmedium.com/hard-links-vs-soft-links-in-linux-whats-the-difference-bbd3dfbe24f7">symbolic link</a>.</p><p id="eab3">And the following 9 characters can be separated into 3 parts:<code>rw-</code>, <code>r--</code>, and <code>r--</code>.</p><p id="6d8b">The rules are:</p><ul><li>Every part represents the permissions for one type of user</li><li>Every character in a part represents one type of permission</li><li><code>r</code> is for “read” permission, <code>w</code> is for “write”, <code>e</code> is for “execute” and <code>-</code> means no permission.</li></ul><p id="7da7">Therefore, the permissions of the <code>testDir</code> file are super clear:</p><ul><li>The owner of this file (“yang”) has the “read” and “write” permissions.</li><li>The members of the owning group only have the “read” permission.</li><li>Other users only have the “read” permission.</li></ul><h1 id="9361">2. Change File Permissions Through Numerical Format</h1><p id="c5b2">To change the permissions of a file, Linux provides us with a convenient numerical format.</p><p id="ad75">It uses 4 numbers to represent the 3 types of permissions and the no permission situation:</p><ul><li>0 for no permission</li><li>1 for “execute”</li><li>2 for “write”</li><li>4 for “read”</li></ul><p id="f782">So, as the following table shows, there are 8 possible situations in total:</p><figure id="7363"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*j_6H_Q4hr7dbAAMrcuBmYA.png"><figcaption>Table from <a href="https://en.wikipedia.org/wiki/Chmod">Wikipedia</a></figcaption></figure><p id="3c07">Based on the above table, eight numbers are enough to represent all possible permissions.</p><p id="6c50">Since there are 3 types of users, all the <code>chmod</code> command needed are 3 numbers to define 3 permissions.</p><p id="4704">Now, let’s go back to the command mentioned at the beginning and figure out what it means:</p><div id="0364"><pre><span class="hljs-attribute">chmod</span> <span class="hljs-number">664</span> file_name</pre></div><p id="3af3">All clear now:</p><ul><li>The first <code>6</code> means the owner of the file has the “read and write” permission.</li><li>The second <code>6</code> means the owning group has the “read and write” permission.</li><li>The last <code>4</code> means the other users only have the “read” permission.</li></ul><h1 id="63b6">3. Change File Permissions Through Symbolic Format</h1><p id="6f28">Besides the numerical style, there is another way called symbolic format.</p><p id="2e28">As its name implies, the <code>chmod</code> command can

Options

use some symbols to represent different types of users and permissions.</p><p id="c869">For users,</p><ul><li><code>u</code>: file owner</li><li><code>g</code>: owning group</li><li><code>o</code>: other users</li><li><code>a</code>: all of the above (the same as <code>ugo</code>)</li></ul><p id="a243">For permissions,</p><ul><li><code>r</code>: read</li><li><code>w</code>: write</li><li><code>x</code>: execute</li></ul><p id="f6c7">In addition, there are 3 types of operators:</p><ul><li><code>=</code>: To assign the exact permissions</li><li><code>-</code>: To remove permissions</li><li><code>+</code>: To add permissions</li></ul><p id="5c6f">Now, it’s time to demystify another command mentioned at the beginning:</p><div id="da62"><pre>chmod u+w,<span class="hljs-built_in">go</span>-w <span class="hljs-built_in">file_name</span></pre></div><p id="817a">It means giving the owner “write” permission and removing the “write” permission from the group and other users.</p><h1 id="daaf">4. Add Options to a chmod Command</h1><p id="0cf3">Last but not least, there are some options you can add when using the <code>chmod</code> command:</p><ul><li><code>-R</code> or <code>--recursive</code>: Apply the same permissions to all the sub-files recursively of a directory.</li><li><code>-v</code> or <code>--verbose</code>: Print the information about the permissions changes (from which permission to which permission)</li><li><code>-f</code>: Ignore all error messages.</li></ul><p id="fe9f">And like lots of other commands, the <code>chmod</code> command also supports <code>--help</code> and <code>--version</code> options to check its manual and version.</p><h1 id="dbd4">Conclusion</h1><p id="b918">The <code>chmod</code> command is about how to assign 3 types of permissions to 3 types of users. It provides two convenient ways: numerical style or symbolic style. Mastering it can help us precisely control the access permissions of your files.</p><p id="03b9"><b><i>Thanks for reading. If you like it, don’t forget to follow <a href="https://yangzhou1993.medium.com/follow">me</a> to enjoy more great articles. </i></b>🙂</p><p id="6777"><i>More articles about Linux:</i></p><div id="2cbf" class="link-block"> <a href="https://yangzhou1993.medium.com/list/8f3bde5c1182"> <div> <div> <h2>Linux Master</h2> <div><h3>undefined</h3></div> <div><p>undefined</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*ef53b3fcaf2b908a0410e3c9a51b8e5025d7ee16.jpeg)"></div> </div> </div> </a> </div></article></body>

5 Levels of Using Chmod Commands in Linux

Precisely control the access permissions of your files

Image from Wallhaven

If you need to pick out one command in Linux that is essential but confusing for beginners, which one you will choose? 🤔

For me, it is the chmod command.

I started to use Linux many years ago. But something like the following made me confused many times:

  • chmod 664 file_name
  • chmod u+w,go-w file_name

What’s the number 664 mean? What’s the messy characters u+w or go-w mean? What’s the …?

Basically, they are just some rules and syntax of the chmod command. They are not hard to understand, but may scare newbies.

This article will explain the design and uses of the chmod command in a beginner-friendly and example-led way. After reading, you never need to google something about it again. 🙂

0. Understand the Basic Concepts of File Permissions in Linux

The name “chmod” is an abbreviation of “change mode”. It’s used to control the access permissions of files or directories from different users in Linux.

On the one hand, there are 3 types of permissions for a file:

  • Read
  • Write
  • Execute

On the other hand, there are 3 types of users:

  • The owner of the file
  • The group (including all its members) which owns the file
  • Other users (who are not the file’s owner and not members of the group)

Therefore, file permissions management in Linux is just about how to assign the 3 types of permissions to the 3 types of users.

1. Know How To Check File Permissions

Of course, before changing a file’s permissions, we need to check its current status firstly. The ls -l command can help us.

For example, I just created a file named testDir and use ls -l testDir to display its information. The results are as follows:

The result of `ls -l testDir` command

All permissions information is in the first part.

The first character represents the file’s type: “-” stands for a regular file which is our case, and another two possible characters are “d” for a directory and “l” for a symbolic link.

And the following 9 characters can be separated into 3 parts:rw-, r--, and r--.

The rules are:

  • Every part represents the permissions for one type of user
  • Every character in a part represents one type of permission
  • r is for “read” permission, w is for “write”, e is for “execute” and - means no permission.

Therefore, the permissions of the testDir file are super clear:

  • The owner of this file (“yang”) has the “read” and “write” permissions.
  • The members of the owning group only have the “read” permission.
  • Other users only have the “read” permission.

2. Change File Permissions Through Numerical Format

To change the permissions of a file, Linux provides us with a convenient numerical format.

It uses 4 numbers to represent the 3 types of permissions and the no permission situation:

  • 0 for no permission
  • 1 for “execute”
  • 2 for “write”
  • 4 for “read”

So, as the following table shows, there are 8 possible situations in total:

Table from Wikipedia

Based on the above table, eight numbers are enough to represent all possible permissions.

Since there are 3 types of users, all the chmod command needed are 3 numbers to define 3 permissions.

Now, let’s go back to the command mentioned at the beginning and figure out what it means:

chmod 664 file_name

All clear now:

  • The first 6 means the owner of the file has the “read and write” permission.
  • The second 6 means the owning group has the “read and write” permission.
  • The last 4 means the other users only have the “read” permission.

3. Change File Permissions Through Symbolic Format

Besides the numerical style, there is another way called symbolic format.

As its name implies, the chmod command can use some symbols to represent different types of users and permissions.

For users,

  • u: file owner
  • g: owning group
  • o: other users
  • a: all of the above (the same as ugo)

For permissions,

  • r: read
  • w: write
  • x: execute

In addition, there are 3 types of operators:

  • =: To assign the exact permissions
  • -: To remove permissions
  • +: To add permissions

Now, it’s time to demystify another command mentioned at the beginning:

chmod u+w,go-w file_name

It means giving the owner “write” permission and removing the “write” permission from the group and other users.

4. Add Options to a chmod Command

Last but not least, there are some options you can add when using the chmod command:

  • -R or --recursive: Apply the same permissions to all the sub-files recursively of a directory.
  • -v or --verbose: Print the information about the permissions changes (from which permission to which permission)
  • -f: Ignore all error messages.

And like lots of other commands, the chmod command also supports --help and --version options to check its manual and version.

Conclusion

The chmod command is about how to assign 3 types of permissions to 3 types of users. It provides two convenient ways: numerical style or symbolic style. Mastering it can help us precisely control the access permissions of your files.

Thanks for reading. If you like it, don’t forget to follow me to enjoy more great articles. 🙂

More articles about Linux:

Linux
DevOps
Bash
Programming
Operating Systems
Recommended from ReadMedium