| |

LFCS 36 ๐Ÿง File Permissions โ€” rwx and Octal

โฑ๏ธ Estimated read: 15 min | |

Every file on a Linux system carries a set of permission bits. The bits decide who can read the file, who can write to it, and who can execute it. The permissions apply to three classes of user: the owner of the file, the members of the file’s group, and everyone else. The combination of three classes and three bits produces the nine-character string that ls -l prints, and the same combination can be written as a three-digit octal number. The two representations carry the same information, and the LFCS exam expects fluency in both.

Key point: The permission bits are not advisory. They are enforced by the kernel on every read, write, and execute. A process that does not have the required bit is denied the operation, regardless of what the application intends.


Why permissions exist

The multi-user problem. A Linux system serves many users. Each user has files that the others should not read: private documents, credentials, work in progress. Without permissions, every file would be readable by every user, and the system would have no privacy.

The shared-resource problem. Some files must be shared. A project directory is read by every member of the team. A web server’s document root is readable by the web server process. Permissions express which class of user may access which resource.

The execute problem. A file that contains a program must be executable to run. A file that contains data must not be executable, because executing it is meaningless and potentially dangerous. The execute bit distinguishes the two.

The accident problem. A user can overwrite the wrong file, delete the wrong directory, or run the wrong script. The permission bits are the first line of defense. A file that is read-only cannot be overwritten by accident.

The service-account problem. A daemon runs as a specific user, and the files it needs are owned by that user or its group. The permissions determine what the daemon can read, write, and execute. A misconfigured permission is a service that fails or a system that is exposed.

The script problem. A shell script with the execute bit runs as a program. The same script without the bit must be passed to the interpreter explicitly. The bit is what makes the file a command rather than a data file.

The audit problem. A review of the permission bits reveals what is shared, what is private, and what is exposed. The bits are the summary of the access policy, and they are the first thing an administrator checks.


a. The rwx bits

The nine permission bits are grouped into three sets of three. Each set applies to one class: the owner (user), the group, and the others. Within each set, the three bits are read, write, and execute.

The three classes

ClassSymbolApplies to
UseruThe owner of the file
GroupgThe members of the file’s group
OthersoEveryone else
AllaAll three classes

The three bits

BitSymbolOctalMeaning for a fileMeaning for a directory
Readr4Read the file’s contentsList the directory’s entries
Writew2Modify the file’s contentsCreate, delete, or rename entries
Executex1Execute the file as a programEnter the directory (cd, access a file by path)

The ls -l output

The ls -l command prints the permission string as the first column.

ls -l file.txt
# -rw-r--r-- 1 alice developers 1234 Jan 15 10:30 file.txt

The string is -rw-r--r--. The first character is the file type: - for a regular file, d for a directory, l for a symbolic link. The next nine characters are the permissions, in three groups of three: rw- for the owner, r-- for the group, r-- for the others.

The directory permissions

The directory permissions have a different meaning than the file permissions.

  • r on a directory means the entries can be listed with ls.
  • w on a directory means entries can be created, deleted, or renamed.
  • x on a directory means the directory can be entered with cd, and a file can be accessed by its path.

The x bit on a directory is required for any access to the files inside it. A directory with r but no x shows the names but does not allow access. A directory with x but no r allows access to known names but not listing.

The missing bit

When a bit is not set, ls -l prints a dash.

# Owner: read and write. Group: read. Others: nothing.
-rw-r-----

The last three characters are ---, which means no permissions for the others.

The two dashes

The permission string has ten characters. The first is the file type. The next nine are the permissions. The - in a permission position means the bit is not set.

The special bits

Three additional bits appear in the permission string when they are set.

BitSymbolPositionMeaning
setuids in user execute4th characterExecute as the file’s owner
setgids in group execute7th characterExecute as the file’s group
stickyt in others execute10th characterOnly the owner can delete

The setuid and setgid bits are represented by a lowercase s when the execute bit is set, and an uppercase S when the execute bit is not set. The sticky bit follows the same rule with t and T.

# setuid set, execute set:
-rwsr-xr-x

# setuid set, execute not set:
-rwSr-xr-x

# sticky set on a directory:
drwxrwxrwt

b. The octal notation

The permission bits are also written as a three-digit octal number. Each digit represents one class, and the value is the sum of the bits.

The octal values

BitValue
Read4
Write2
Execute1

The sum of the bits in a class is the digit for that class.

The common combinations

OctalStringMeaning
777rwxrwxrwxEveryone can do everything
755rwxr-xr-xOwner all, group and others read and execute
750rwxr-x---Owner all, group read and execute, others nothing
700rwx------Owner all, no one else
644rw-r--r--Owner read and write, everyone else read
640rw-r-----Owner read and write, group read
600rw-------Owner read and write, no one else
444r--r--r--Everyone read only
400r--------Owner read only

The digit arithmetic

Each digit is the sum of the bits for its class.

7 = 4 + 2 + 1 = rwx
6 = 4 + 2     = rw-
5 = 4 + 1     = r-x
4 = 4         = r--
3 = 2 + 1     = -wx
2 = 2         = -w-
1 = 1         = --x
0 = 0         = ---

The four-digit form

A four-digit octal number includes the special bits. The first digit is the sum of the setuid (4), setgid (2), and sticky (1) bits.

OctalSpecial bitsString
4755setuidrwsr-xr-x
2755setgidrwxr-sr-x
1777stickyrwxrwxrwt
6755setuid + setgidrwsr-sr-x
7777all threerwsrwsrwt

The chmod octal

The chmod command accepts the octal form.

chmod 644 file.txt
chmod 755 script.sh
chmod 700 private/

The command sets all nine bits at once, replacing the previous permissions.

The chmod symbolic

The chmod command also accepts the symbolic form.

chmod u+x script.sh
chmod g-w file.txt
chmod o=r file.txt
chmod a+r file.txt

The symbolic form adds (+), removes (-), or sets (=) the bits for the specified classes.

The umask

The umask command shows or sets the default permissions mask. The mask is subtracted from the default permissions for new files and directories.

umask
# 0022

The mask 0022 means the group and others write bits are removed from new files. A new file has 666 - 022 = 644. A new directory has 777 - 022 = 755.

The default permissions

TypeDefaultWith umask 022
File666644
Directory777755

The default file permissions never include the execute bit. The chmod +x is required to make a file executable.


c. The practical scenarios

The permission bits are used in specific scenarios. The scenarios are the common tasks that the LFCS exam tests.

The executable script

A shell script must have the execute bit for the user or the group that runs it.

chmod 755 deploy.sh
./deploy.sh

The 755 gives the owner full permissions, and the group and others read and execute.

The private key

An SSH private key must be readable only by the owner. The SSH client refuses to use a key with broader permissions.

chmod 600 ~/.ssh/id_rsa

The 600 gives the owner read and write, and no one else any access.

The shared directory

A shared directory for a team is writable by the group.

mkdir /srv/project
chgrp developers /srv/project
chmod 2775 /srv/project

The 2775 sets the setgid bit, so new files created in the directory inherit the developers group. The group has write access.

The public web directory

A web server’s document root is readable and executable by everyone, and writable by the owner.

chmod 755 /var/www/html
chmod 644 /var/www/html/index.html

The directory is 755 so the server can enter it and read the entries. The file is 644 so the server can read it.

The /tmp directory

The /tmp directory is world-writable, but the sticky bit prevents users from deleting each other’s files.

ls -ld /tmp
# drwxrwxrwt 1 root root ... /tmp

The t is the sticky bit. Only the owner of a file, the owner of the directory, or root can delete it.

The read-only file

A file that must not be changed is set to read-only.

chmod 444 report.txt

The owner cannot write to the file either. To change it, the owner must first change the permissions.

The append-only log

A log file that only grows is writable by the process that appends to it, and readable by the administrators.

chmod 640 /var/log/app.log

The owner can read and write, the group can read, and the others have no access.

The no-access directory

A directory that must be completely private is set to 700.

chmod 700 ~/private

The owner has full access, and no one else has any.

The temporary file with the sticky bit

A shared temporary directory for a group uses the sticky bit.

mkdir /srv/shared-tmp
chgrp developers /srv/shared-tmp
chmod 1770 /srv/shared-tmp

The 1770 gives the owner full access, the group full access, and the others nothing. The sticky bit prevents group members from deleting each other’s files.


Complete Example Session

# ============================================
# PART 1: THE BASIC PERMISSIONS
# ============================================

touch file.txt
ls -l file.txt
# -rw-r--r-- 1 alice alice 0 ... file.txt

# ============================================
# PART 2: THE OCTAL FORM
# ============================================

chmod 600 file.txt
ls -l file.txt
# -rw------- 1 alice alice 0 ... file.txt

chmod 644 file.txt
ls -l file.txt
# -rw-r--r-- 1 alice alice 0 ... file.txt

# ============================================
# PART 3: THE SYMBOLIC FORM
# ============================================

chmod u+x script.sh
chmod g-w file.txt
chmod o=r file.txt
chmod a+r file.txt

# ============================================
# PART 4: THE DIRECTORY
# ============================================

mkdir project
chmod 755 project
ls -ld project
# drwxr-xr-x 2 alice alice 4096 ... project

# ============================================
# PART 5: THE EXECUTABLE SCRIPT
# ============================================

cat > hello.sh << 'EOF'
#!/bin/bash
echo "Hello, world!"
EOF

chmod 755 hello.sh
./hello.sh
# Hello, world!

# ============================================
# PART 6: THE PRIVATE KEY
# ============================================

chmod 600 ~/.ssh/id_rsa
ls -l ~/.ssh/id_rsa
# -rw------- 1 alice alice 1675 ... id_rsa

# ============================================
# PART 7: THE SHARED DIRECTORY
# ============================================

sudo mkdir /srv/project
sudo chgrp developers /srv/project
sudo chmod 2775 /srv/project
ls -ld /srv/project
# drwxrwsr-x 2 root developers 4096 ... /srv/project

# ============================================
# PART 8: THE STICKY BIT
# ============================================

ls -ld /tmp
# drwxrwxrwt 1 root root 4096 ... /tmp

# ============================================
# PART 9: THE UMASK
# ============================================

umask
# 0022

umask 0077
umask
# 0077

# New files have 600, new directories have 700.

# ============================================
# PART 10: THE SPECIAL BITS
# ============================================

# setuid
chmod 4755 /usr/local/bin/program
ls -l /usr/local/bin/program
# -rwsr-xr-x 1 root root ... program

# setgid
chmod 2755 /srv/project
ls -ld /srv/project
# drwxr-sr-x 2 root developers ... /srv/project

# sticky
chmod 1777 /tmp
ls -ld /tmp
# drwxrwxrwt 1 root root ... /tmp

The ten parts showed the basic permissions, the octal form, the symbolic form, the directory, the executable script, the private key, the shared directory, the sticky bit, the umask, and the special bits.


Quick Reference

The Permission Bits

BitSymbolOctalFileDirectory
Readr4Read contentsList entries
Writew2Modify contentsCreate, delete entries
Executex1ExecuteEnter, access by path

The Octal Values

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

The Common Modes

OctalStringUse Case
755rwxr-xr-xExecutables, directories
644rw-r--r--Regular files
700rwx------Private directories
600rw-------Private files, keys
640rw-r-----Group-readable files
444r--r--r--Read-only files
777rwxrwxrwxWorld-writable (avoid)

The Special Bits

BitOctalSymbolMeaning
setuid4sRun as the file’s owner
setgid2sRun as the file’s group
sticky1tOnly the owner can delete

The chmod Forms

FormExample
Octalchmod 644 file
Symbolic addchmod u+x file
Symbolic removechmod g-w file
Symbolic setchmod o=r file
Symbolic allchmod a+r file

The umask

umaskFilesDirectories
022644755
027640750
077600700
002664775

Best Practices

โœ… Do This:

# Use 755 for executables and directories.
chmod 755 script.sh  # โœ…

# Use 644 for regular files.
chmod 644 index.html  # โœ…

# Use 600 for private keys.
chmod 600 ~/.ssh/id_rsa  # โœ…

# Use 700 for private directories.
chmod 700 ~/private  # โœ…

# Use 2775 for shared group directories.
chmod 2775 /srv/project  # โœ…

# Use the sticky bit for shared world-writable directories.
chmod 1777 /tmp  # โœ…

# Set the umask before creating files.
umask 022  # โœ…

# Verify with ls -l.
ls -l file.txt  # โœ…

โŒ Don’t Do This:

# Don't use 777 for anything.
chmod 777 /var/www/html  # โŒ World-writable

# Don't use 777 for directories.
chmod 777 uploads/  # โŒ Any user can delete files

# Don't forget the execute bit on a script.
chmod 644 script.sh  # โŒ ./script.sh fails

# Don't forget the read bit on a directory.
chmod 300 dir/  # โŒ ls fails

# Don't give the others write access.
chmod o+w file  # โŒ Anyone can modify

# Don't set setuid on a script.
chmod 4755 script.sh  # โŒ setuid is ignored on scripts

# Don't rely on the umask for security.
# Set the permissions explicitly.  # โŒ

Common Pitfalls

PitfallWhy It HappensFix
Script not executableMissing x bitchmod +x script.sh
Directory not listableMissing r bitchmod +r dir/
Directory not enterableMissing x bitchmod +x dir/
Key rejectedToo broadchmod 600 key
File world-writable777Use 755 or 644
setuid ignoredApplied to a scriptUse a binary
Group not inheritedNo setgid on the directorychmod g+s dir/
Cannot delete a fileSticky bit on the directoryOnly the owner can delete

Real-World Examples

1. Executable Script

chmod 755 deploy.sh

2. Private Key

chmod 600 ~/.ssh/id_rsa

3. Shared Directory

chmod 2775 /srv/project

4. Public File

chmod 644 /var/www/html/index.html

5. Private Directory

chmod 700 ~/private

6. Sticky Bit

chmod 1777 /tmp

7. Read-Only

chmod 444 report.txt

8. Group-Readable

chmod 640 /var/log/app.log

9. The umask

umask 077

10. The setgid Directory

chmod 2770 /srv/shared-tmp

Visual

The Permission String

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  THE PERMISSION STRING                                     โ”‚
โ”‚                                                            โ”‚
โ”‚  -rwxr-xr-x                                                โ”‚
โ”‚  โ”‚โ””โ”ฌโ”˜โ””โ”ฌโ”˜โ””โ”ฌโ”˜                                                โ”‚
โ”‚  โ”‚ โ”‚  โ”‚  โ””โ”€โ”€ Others: r-x (5)                               โ”‚
โ”‚  โ”‚ โ”‚  โ””โ”€โ”€โ”€โ”€โ”€ Group:  r-x (5)                               โ”‚
โ”‚  โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ User:   rwx (7)                               โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Type:   - (regular file)                      โ”‚
โ”‚                                                            โ”‚
โ”‚  The nine bits are grouped into three classes.             โ”‚
โ”‚  Each class has three bits: r, w, x.                       โ”‚
โ”‚  Each bit has a value: 4, 2, 1.                            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The Octal Arithmetic

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  THE OCTAL ARITHMETIC                                      โ”‚
โ”‚                                                            โ”‚
โ”‚  rwx = 4 + 2 + 1 = 7                                       โ”‚
โ”‚  rw- = 4 + 2     = 6                                       โ”‚
โ”‚  r-x = 4     + 1 = 5                                       โ”‚
โ”‚  r-- = 4         = 4                                       โ”‚
โ”‚  -wx =     2 + 1 = 3                                       โ”‚
โ”‚  -w- =     2     = 2                                       โ”‚
โ”‚  --x =         1 = 1                                       โ”‚
โ”‚  --- =           = 0                                       โ”‚
โ”‚                                                            โ”‚
โ”‚  Example: 755                                              โ”‚
โ”‚  User:  7 = rwx                                            โ”‚
โ”‚  Group: 5 = r-x                                            โ”‚
โ”‚  Other: 5 = r-x                                            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The Directory Permissions

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  DIRECTORY PERMISSIONS                                     โ”‚
โ”‚                                                            โ”‚
โ”‚  r (read)   โ†’ list the entries with ls                     โ”‚
โ”‚  w (write)  โ†’ create, delete, rename entries               โ”‚
โ”‚  x (execute)โ†’ enter the directory, access files by path    โ”‚
โ”‚                                                            โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚  โ”‚  drwxr-xr-x  /var/www/html                        โ”‚     โ”‚
โ”‚  โ”‚                                                   โ”‚     โ”‚
โ”‚  โ”‚  The web server can:                              โ”‚     โ”‚
โ”‚  โ”‚    - List the directory (r)                       โ”‚     โ”‚
โ”‚  โ”‚    - Enter the directory (x)                      โ”‚     โ”‚
โ”‚  โ”‚    - Read the files inside (x on the directory)   โ”‚     โ”‚
โ”‚  โ”‚                                                   โ”‚     โ”‚
โ”‚  โ”‚  The web server cannot:                           โ”‚     โ”‚
โ”‚  โ”‚    - Create or delete files (no w)                โ”‚     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The Special Bits

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  THE SPECIAL BITS                                          โ”‚
โ”‚                                                            โ”‚
โ”‚  SETUID (4)                                                โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚  โ”‚  -rwsr-xr-x                                       โ”‚     โ”‚
โ”‚  โ”‚  A program that runs as the file's owner.         โ”‚     โ”‚
โ”‚  โ”‚  Used by passwd, sudo, mount.                     โ”‚     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ”‚                                                            โ”‚
โ”‚  SETGID (2)                                                โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚  โ”‚  -rwxr-sr-x                                       โ”‚     โ”‚
โ”‚  โ”‚  On a file: run as the file's group.              โ”‚     โ”‚
โ”‚  โ”‚  On a directory: new files inherit the group.     โ”‚     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ”‚                                                            โ”‚
โ”‚  STICKY (1)                                                โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚  โ”‚  drwxrwxrwt                                       โ”‚     โ”‚
โ”‚  โ”‚  On a directory: only the owner can delete.       โ”‚     โ”‚
โ”‚  โ”‚  Used by /tmp.                                    โ”‚     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The umask

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  THE umask                                                 โ”‚
โ”‚                                                            โ”‚
โ”‚  New file default: 666                                     โ”‚
โ”‚  New directory default: 777                                โ”‚
โ”‚                                                            โ”‚
โ”‚  umask 022                                                 โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚  โ”‚  File:      666 - 022 = 644                       โ”‚     โ”‚
โ”‚  โ”‚  Directory: 777 - 022 = 755                       โ”‚     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ”‚                                                            โ”‚
โ”‚  umask 077                                                 โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚  โ”‚  File:      666 - 077 = 600                       โ”‚     โ”‚
โ”‚  โ”‚  Directory: 777 - 077 = 700                       โ”‚     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ”‚                                                            โ”‚
โ”‚  The umask is subtracted from the default.                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

ItemValue
rRead: 4
wWrite: 2
xExecute: 1
UserThe owner of the file
GroupThe members of the file’s group
OthersEveryone else
OctalThe sum of the bits per class
setuid4, run as the owner
setgid2, run as the group
sticky1, only the owner can delete
chmodChange the permissions
umaskThe default permission mask

Key takeaways:

  • The permission bits are grouped into three classes and three bits. The classes are user, group, and others. The bits are read, write, and execute. The nine bits are the entire access policy for the file.
  • The octal form is the sum of the bits per class. Each digit is 4 for read, 2 for write, and 1 for execute. The three digits represent the user, the group, and the others, in that order.
  • The directory permissions have different meanings. The r bit lists the entries, the w bit creates and deletes them, and the x bit enters the directory and accesses the files by path.
  • The execute bit is required for a script to run. A shell script with the execute bit runs as ./script.sh. Without the bit, it must be passed to the interpreter explicitly.
  • The setuid bit runs a program as the file’s owner. The bit is used by passwd, sudo, and mount. It is ignored on scripts for security reasons.
  • The setgid bit runs a program as the file’s group, or makes a directory inherit the group. On a directory, the setgid bit makes new files inherit the directory’s group, which is the standard pattern for shared team directories.
  • The sticky bit restricts deletion in a shared directory. Only the owner of the file, the owner of the directory, or root can delete the file. The /tmp directory uses the sticky bit.
  • The umask sets the default permissions. The mask is subtracted from the default permissions for new files and directories. A umask of 022 produces 644 for files and 755 for directories.
  • The chmod command changes the permissions. The octal form sets all nine bits at once. The symbolic form adds, removes, or sets the bits for the specified classes.

Remember: The permission bits are the kernel’s access control for every file on the system. The nine bits are grouped into three classes and three operations, and the octal form is the same information as a three-digit number. The directory permissions have different meanings than the file permissions, and the special bits add the setuid, setgid, and sticky behaviors. The chmod command changes the bits, and the umask sets the default. The result is a system where every file has an owner, a group, and a set of permissions, and where the access policy is enforced by the kernel on every operation.


Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!