Chmod Cheat Sheet

This quick reference cheat sheet provides a brief overview of file permissions, and the operation of the chmod

Chmod Practices

Batch Change

$ chmod -R 644 /your\_path
$ find /path -type d -exec chmod 755 {} \;
$ find /path -type f -exec chmod 644 {} \;

See: Command Substitution

Web Permissions

$ chmod -R 644 /var/www/html/
$ chmod 644 .htaccess
$ chmod 644 robots.txt
$ chmod 755 /var/www/uploads/
$ find /var/www/html -type d -exec chmod 755 {} \;

SSH Permissions

$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/authorized\_keys
$ chmod 600 ~/.ssh/id\_rsa
$ chmod 600 ~/.ssh/id\_rsa.pub
$ chmod 400 /path/to/access\_key.pem

Chmod Examples

chmod 754

$ chmod 754 foo.sh
$ chmod u=rwx,g=rx,o=r foo.sh

Executable

$ chmod +x ~/example.py
$ chmod u+x ~/example.py
$ chmod a+x ~/example.py

Removing Permissions

In order to remove read write permissions given to a file, use the following syntax:

$ chmod o-rw example.txt

For our file example.txt, we can remove read write permissions using chmod for group by running the following command:

$ chmod g-rx example.txt

To remove chmod read write permissions from the group while adding read write permission to public/others, we can use the following command:

$ chmod g-rx, o+rx example.txt

But, if you wish to remove all permissions for group and others, you can do so using the go= instead:

$ chmod go= example.txt

Symbolic mode

Deny execute permission to everyone.

$ chmod a-x chmodExampleFile.txt

Allow read permission to everyone.

$ chmod a+r chmodExampleFile.txt

Make a file readable and writable by the group and others.

$ chmod go+rw chmodExampleFile.txt

Make a shell script executable by the user/owner.

$ chmod u+x chmodExampleScript.sh

Allow everyone to read, write, and execute the file and turn on the set group-ID.

$ chmod =rwx,g+s chmodExampleScript.sh

chmod 777

$ chmod 777 example.txt
$ chmod u=rwx,g=rwx,o=rwx example.txt
$ chmod a=rwx example.txt

chmod 664

$ chmod 664 example.txt
$ chmod u=rw,g=rw,o=r example.txt
$ chmod a+rwx,u-x,g-x,o-wx example.txt

chmod 600

$ chmod 600 example.txt
$ chmod u=rw,g=,o= example.txt
$ chmod a+rwx,u-x,g-rwx,o-rwx example.txt

Operators

Symbol Description
+ Add
- Remove
= Set

Getting Started

File Types

Abbreviation File Type
d Directory
- Regular file
l Symbolic Link

Permissions

Abbreviation Permission Value
r Read 4
w Write 2
x Execute 1
- No permission 0

Objects

Who (abbr.) Meaning
u User
g Group
o Others
a All, same as ugo

Permission Modes

Permission Description Octal Decimal
--- No Permission 000 0 (0+0+0)
--x Execute 001 1 (0+0+1)
-w- Write 010 2 (0+2+0)
-wx Execute and Write 011 3 (0+2+1)
r-- Read 100 4 (4+0+0)
r-x Read and Execute 101 5 (4+0+1)
rw- Read and Write 110 6 (4+2+0)
rwx Read, Write and Execute 111 7 (4+2+1)

Explains

$ ls -l
-rw-r--r--  1 root root 3 Jun 29 15:35 a.log
drwxr-xr-x  2 root root 2 Jun 30 18:06 dir

#Permission analysis of "dir"

d  rwx  r-x  r-x
┬  ─┬─  ─┬─  ─┬─  
│   │    │    │  
│   │    │    └─ 4. Other|5 (4+0+1)
│   │    └────── 3. Group|5 (4+0+1)
│   └─────────── 2. User |7 (4+2+1)
└─────────────── 1. File Type | directory

Common Permissions

Command s Meaning
400 r-------- Readable by owner only
500 r-x------ Avoid Changing
600 rw------- Changeable by user
644 rw-r--r-- Read and change by user
660 rw-rw---- Changeable by user and group
700 rwx------ Only user has full access
755 rwxr-xr-x Only changeable by user
775 rwxrwxr-x Sharing mode for a group
777 rwxrwxrwx Everybody can do everything

Chmod Generator

Permissions:

User Group Other
Read
Write
Execute
const reg_num = /^[0-7]{3}$/; // some regex to check the num input
const reg_let = /^([r-]{1}[w-]{1}[x-]{1}){3}$/; // some regex to check the text input
window.addEventListener("DOMContentLoaded", function () {
// loop over all the check boxes
for (let i = 1; i < 10; i++) {
let checkBox = document.getElementById(${i});
checkBox.addEventListener('change', function () {
change_occured(true, false, false);
// get rid of bad input classes
document.getElementById('num').classList.remove('bad-input');
document.getElementById('let').classList.remove('bad-input');
});
}
// the octal input
let num_input = document.getElementById('num');
let num_fn = function () {
// check for bad input
if (!reg_num.test(this.value)) {
this.classList.add('bad-input');
} else {
this.classList.remove('bad-input');
change_occured(false, true, false);
}
};
num_input.addEventListener('change', num_fn);
num_input.addEventListener('keyup', num_fn);
// the let input
let let_input = document.getElementById('let');
let let_fn = function () {
// check for bad input
if (!reg_let.test(this.value)) {
this.classList.add('bad-input');
} else {
this.classList.remove('bad-input');
change_occured(false, false, true);
}
};
let_input.addEventListener('change',let_fn);
let_input.addEventListener('keyup',let_fn);
});
/* SETUP
r-4-1 r-4-4 r-4-7
w-2-2 w-2-5 w-2-8
x-1-3 x-1-6 x-1-9
*/
// define a function that runs when a change occures
function change_occured(caller_was_check, caller_was_num, caller_was_let) {
let num1 = 0, num2 = 0, num3 = 0; // these are the three numbers for the octal
let perm_string = ''; // holds the permision string ex. rw-x--r--
if (caller_was_check) {
// loop over all the check boxes and get the permisions
for (let i = 1; i < 10; i++) {
let checkBox = document.getElementById(${i});
if (checkBox.checked) { // if checked
let current_perm = check_to_octal_and_text(i);
perm_string += ${current\_perm.perm\_let};
if (i <= 3) {
num1 += current_perm.perm_num;
} else if (i <= 6) {
num2 += current_perm.perm_num;
} else {
num3 += current_perm.perm_num;
}
} else { // if not checked
perm_string += '-';
}
}
// set the permision input text
document.getElementById('let').value = perm_string;
document.getElementById('num').value = ${num1}${num2}${num3};
} else if (caller_was_num) {
// get the individual numbers
let num_input_val = document.getElementById('num').value;
num1 = num_input_val.substring(0, 1);
num2 = num_input_val.substring(1, 2);
num3 = num_input_val.substring(2, 3);
// set the checkboxes and get the perm string
perm_string += octal_to_check_and_txt(num1, 0); //Owner
perm_string += octal_to_check_and_txt(num2, 1); //Owner
perm_string += octal_to_check_and_txt(num3, 2); //Owner
// set the permision input text
document.getElementById('let').value = perm_string;
} else if (caller_was_let) {
// get the text input
let perm_text = document.getElementById('let').value;
num1 = text_to_check_and_octal(perm_text.substring(0, 3), 0)
num2 = text_to_check_and_octal(perm_text.substring(3, 6), 3)
num3 = text_to_check_and_octal(perm_text.substring(6, 9), 6)
// set the octal value
document.getElementById('num').value = ${num1}${num2}${num3};
}
}
// define a function to converts the checkbox # to the respective permissions
// returns perm_num, perm_let
function check_to_octal_and_text(check_num) {
let perm_num = 0;
let perm_let = '-';
switch (check_num) {
case 1:
case 4:
case 7:
perm_num = 4;
perm_let = 'r';
break;
case 2:
case 5:
case 8:
perm_num = 2;
perm_let = 'w';
break;
case 3:
case 6:
case 9:
perm_num = 1;
perm_let = 'x';
break;
default:
perm_num = 0;
perm_let = '-';
}
// return values
return {
perm_num,
perm_let
};
}
/**
Takes a number 1-7 and which class it is in:
0 = owner
1 = Group
2 = Public
Returns: perm text (ex. "rwx") and sets the appropriate checkboxes
*/
function octal_to_check_and_txt(octal_num, class_num) {
let perm_text = '';
let offset = class_num * 3;
switch (octal_num) {
case '1':
document.getElementById(${1 + offset}).checked = false;
document.getElementById(${2 + offset}).checked = false;
document.getElementById(${3 + offset}).checked = true;
perm_text = '--x';
break;
case '2':
document.getElementById(${1 + offset}).checked = false;
document.getElementById(${2 + offset}).checked = true;
document.getElementById(${3 + offset}).checked = false;
perm_text = '-w-';
break;
case '3':
document.getElementById(${1 + offset}).checked = false;
document.getElementById(${2 + offset}).checked = true;
document.getElementById(${3 + offset}).checked = true;
perm_text = '-wx';
break;
case '4':
document.getElementById(${1 + offset}).checked = true;
document.getElementById(${2 + offset}).checked = false;
document.getElementById(${3 + offset}).checked = false;
perm_text = 'r--';
break;
case '5':
document.getElementById(${1 + offset}).checked = true;
document.getElementById(${2 + offset}).checked = false;
document.getElementById(${3 + offset}).checked = true;
perm_text = 'r-x';
break;
case '6':
document.getElementById(${1 + offset}).checked = true;
document.getElementById(${2 + offset}).checked = true;
document.getElementById(${3 + offset}).checked = false;
perm_text = 'rw-';
break;
case '7':
document.getElementById(${1 + offset}).checked = true;
document.getElementById(${2 + offset}).checked = true;
document.getElementById(${3 + offset}).checked = true;
perm_text = 'rwx';
break;
default:
document.getElementById(${1 + offset}).checked = false;
document.getElementById(${2 + offset}).checked = false;
document.getElementById(${3 + offset}).checked = false;
perm_text = '---';
}
return perm_text;
}
/**
Takes 3 letters (r, w, x, - ex. 'rw-') and an offset (0,3,6)
Returns the octal num and sets the appropriate checkboxes
*/
function text_to_check_and_octal(letters, offset) {
let perm_num = 0; // the octal number to return
// add up the oct num and set the check boxes
for (let i = 0; i < 3; i++) {
current_let = letters.substring(i, i + 1);
if (current_let == 'r') {
document.getElementById(${i + 1 + offset}).checked = true;
perm_num += 4;
} else if (current_let == 'w') {
document.getElementById(${i + 1 + offset}).checked = true;
perm_num += 2;
} else if (current_let == 'x') {
document.getElementById(${i + 1 + offset}).checked = true;
perm_num += 1;
} else {
document.getElementById(${i + 1 + offset}).checked = false;
}
}
return perm_num;
}
Chmod Generator allows you to quickly and visually generate permissions in numerical and symbolic.

Syntax

$ chmod [options] <permissions> <file> 

#Example

$ chmod 755 foo.txt
$ chmod +x quickref.py
$ chmod u-x quickref.py
$ chmod u=rwx,g=rx,o= quickref.sh

#Change files and directories recursively

$ chmod -R 755 my\_directory

The chmod command stands for "change mode"