Linux Tutorial: How to Use chmod to update file and directory permissions

in utopian-io •  7 years ago  (edited)

The Linux chmod command is used to change the read, write and execute permissions of files and directories and can be absolute or symbolic.

Every Linux file and every Linux directory has its own set of permissions which controls who can do what with them. Each of them has three types of permission:

  1. read
  2. write
  3. execute

They can be accessed (according to the permissions) by three types of user:

  • the file owner
  • the user group
  • all other users

But all are set by one Linux command - chmod.

Viewing File Permissions

The easiest way to examine a file or directory's permission is by using ls in its long format:

$ cd ~/bin
$ ls -l
lrwxrwxrwx 1 bainm users 16 2017-12-01 18:43 cdu -> check_disk_usage
drwxr-xr-x 2 bainm users 4096 2017-12-01 19:34 cdu_data
-rwxrwxrwx 1 bainm users 84 2017-12-01 15:19 check_disk_usage

The first column shows the file permissions and is actually divided into 10 fields (with any empty fields set to -):

  • Field 1: the file type, which can be:
    - (a file)
    d (a directory)
    l (a symbolic link)
  • fields 2-4: the file owner permissions
  • fields 5-7: the file group permissions
  • fields 8-10: the permissions for all other users

Each group of three fields (for owner, group and others) have the same structure:

  • 1st field: read permission (can be r or -)
  • 2nd field: write permission (can be w or -)
  • 3rd field: execute permission (can be x or -)

Therefore, the file check_disk_usage has a very dangerous set of file permissions - it can be written to by anybody logged on to the system (not the best idea for an executable file in a bin directory); however, the file permissions can be changed by using chmod either of two ways:

  • setting the permission symbols
  • setting the absolute permission


Setting the Permission Symbols

When setting the permission symbols a combination of different symbols are used; to start with there are symbols for the owner, group and others:

  • u - owner
  • g - group
  • o - others

Then a symbol to add or remove a permission:

    • add
    • remove

And finally the symbols for the permissions themselves:

  • r - read
  • w - write
  • x - execute

For example:
$ chmod u+rwx check_disk_usage
$ chmod g+r-w+x check_disk_usage
$ chmod o-r-w-x check_disk_usage
$ ls -l check_disk_usage
-rwxr-x--- 1 bainm users 84 2008-12-05 15:19 check_disk_usage

Now:

  • the owner can read, write and execute the file
  • members of the group can read and execute the file
  • anyone else has no access at all


Setting the Absolute Permission

The absolute permission is an octal translation of the binary representation of the permissions a file - that sounds complicated, but is made more obvious by viewing a complete set of permissions:

O Bin rwx

0 000 ---
1 001 --x
2 010 -w-
3 011 -wx
4 100 r--
5 101 r-x
6 110 rw-
7 111 rwx

Unlike the symbolic permissions all three permissions have to be set at the same time, for example:

$ chmod 777 check_disk_usage
$ ls -l check_disk_usage
-rwxrwxrwx 1 bainm bainm 84 2008-12-05 15:19 check_disk_usage
$ chmod 750 check_disk_usage
$ ls -l check_disk_usage
-rwxr-x--- 1 bainm bainm 84 2008-12-05 15:19 check_disk_usage

Summary

A Linux user uses the chmod command to change the permissions of any of their files or directories; chmod has two modes:

symbolic - this uses:

  • u, g or o to represent owner, group and other
    • or - to add or remove a permission
  • r, w, and x to represent read, write and execute

absolute - uses an octal translation of the binary representation of the permissions a file or directory so that:

  • 0 represents ---
  • 1 represents --x
  • 2 represents -w-
  • 3 represents -wx
  • 4 represents r--
  • 5 represents r-x
  • 6 represents rw-
  • 7 represents rwx

In both cases the form chmod [symbolic or absolute permission] [file or directory] is used.



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @folke! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @folke I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x