How do I use chmod to manage file permissions?



An Intro to Understanding Octal Notation for Linux and Mac File Permissions - and Managing Them Using chmod


This article is part 2 of 3 in the series on file permissions:
Understanding File Permissions
How do I use chmod to manage file permissions?
Sticky Bits, SUID's and SGID's

Article Navigation:
Overview
Break Down of Octal Notation
How Do I change File Permissions Using chmod?
Where and how to get help
Where to go from here

Overview

Chmod is a command line utility that is used for manually managing the access and permissions to files and directories on Linux, Mac, and other Unix like operating systems. According to the man page document for chmod: "The chmod utility modifies the file mode bits of the listed files as specified by the mode operand. It may also be used to modify the Access Control Lists (ACLs) associated with the listed files". While there are multiple ways of handling the editing of file permissions via chmod, we have chosen to focus primarily on octal notation.

Understanding octal file permissions

Octal notation is a numerical system for modifying the permissions on Linux, Mac and other Unix like file systems. Each octal permission can be represented by 3 or 4 numbers; where each of these numbers is an "octal", meaning they range from 0-7. Each one of the numbers represents permissions that can be set to either a file or directory.

Break Down of Octal Notation

* This article focuses on chmod using 3 numbers. If you're looking to have to use 4 numbers, to set a sticky bit, SUID or SGID, you will need to see the third article in this series. - link here -

When made up of 3 numbers, each of on the "octals" represents each of the groups that have access to a file. For example the octal 724 presents a situation where 7 is the octal for setting Owner permissions, 2 represents Group permissions, and 4 represents other permissions. Heres a breakdown

Break Down of Octal Notation - Example
example octal: "724"

Owner - 7
Group - 2
Other - 4
Each one of these numbers allows the read, write, and execute permissions per group. All possible combinations for RWX access are, and can be express by these octal numbers and their relative values, where read is equal to 4, write is equal to 2, and execute is 1. Here is a break down

Permission Values - Example
x (execute) - 1
w (write) - 2
r (read) - 4
Combining these together help up communicate the permissions we wish to set. For example if I wanted to have read and write access, I would have a value of 6, as read is 4, write is 2, so combined they equal 6.

Combining Octal Values - Example
example octal: "724"

Owner Permissions - 7 = rwx
Group Permissions - 2 = -w-
Other Permissions - 4 = r--
Pulling it all together:
Once decoded you're able to get a makeup of the permissions that are being set, in the case of 724, the break down becomes -rwx-w-r--, where file type is a file denoted by the leading "-", owner is able to read, write, and execute. Group is only able to write, and other is only able to read.

How Do I change File Permissions Using chmod?

* Please note there are multiple ways of using chmod, within this article we have chosen to only show examples using octal notation.

Chmod is quite simple to use while using octal notation. The structure of the command is simply:
chmod < octal permission you wish to set > < file or directory >

chmod usage example
Using chmod to change myfile.txt's permissions
$ chmod 777 myfile.txt

You can then confirm the change using ls -l
$ ls -al

output being:
-rwxrwxrwx 1 root root 4096 Sep 24 2016 myfile.txt

"-rwxrwxrwx" being "myfile.txt's" new permissions, which permit the following:
Owner: rwx - 7
Group: rwx - 7
Other: rwx - 7
chmod usage example
Using chmod to change myfile.txt's permissions
$ chmod 142 myfile.txt

Confirming the change using ls -l
$ ls -al

output being:
---xr---w- 1 root root 4096 Sep 24 2016 myfile.txt

"---xr---w-" being "myfile.txt's" new permissions, which permit the following:
Owner: --x - 1
Group: r-- - 4
Other: -w- - 2

Where and how to get help

Chmod is a very complex command line utility, with a large number of options. If you need further help you can check the following resources:

From the command line:
Using chmod's help flag from the command line
$ chmod -h

Checking the man pages for chmod from the command line
$ man chmod

Websites:
If all else fails Stack Overflow and Super User are always a good source of info along with well respected q and a.

Where to go from here

This article is part 2 of 3 in the series on file permissions.