AP Cybersecurity · Study guide

Linux File Permissions (chmod, octal)

AP® Cybersecurity · Unit 5 · Applications, Data & Cryptography

Linux File Permissions (chmod, octal)

Reading rwx permission strings, setting them with chmod in numeric (octal) and symbolic form, and checking them with ls -l and getfacl.

1Authorization on a Linux file

Authorization is when an entity is granted permission to have a certain type of access to a resource. On Linux, every file carries its own access controls that say which users get which types of access. Each file has an owner (one user account) and a group (a named set of accounts), and everyone else counts as others.

2Reading a permission string

There are three types of access, always in this order:

LetterPermissionOn a file, it allows
rReadViewing the file's contents
wWriteMaking changes to the file
xExecuteRunning the file as a program

A - means that permission is absent, so read and execute without write shows as r-x. The three sets are written for the owner, then the group, then all other users, with no spaces: rwxrwxrwx.

Figure 1. Owner, group, others, always in that order, each with read, write, execute in that order. Add 4, 2, and 1 for each permission that is on.

3Viewing permissions: ls -l and getfacl

The command ls -l shows the current settings for owner, group, and others. A + at the end of the permissions means extra permissions have been set for that file (for specific users or groups), and they can be viewed with getfacl.

Three files in a clinic folder
$ ls -l
-rw-r--r--  1 drlee  nurses  2048 Nov  3 09:12 patients.csv
-rwxr-x---  1 drlee  nurses   512 Nov  3 09:12 report.sh
-rw-r-----+ 1 drlee  nurses   880 Nov  3 09:15 schedule.txt
  • patients.csv is rw-r--r--: the owner (drlee) can read and change it, the nurses group can read it, and every other user can read it too. For patient data, that last r is a problem.
  • report.sh is rwxr-x---: drlee can read, edit, and run it; nurses can read and run it; others have no access.
  • schedule.txt ends in +: run getfacl schedule.txt to see the extra entries.

4chmod, numeric method

To change permissions, use chmod. The numeric method is chmod ### filename: the first digit is the owner, the second the group, the third all others. Each digit is the sum of the permissions granted:

DigitRead (4)Write (2)Execute (1)Shown as
0---
1yes--x
2yes-w-
3yesyes-wx
4yesr--
5yesyesr-x
6yesyesrw-
7yesyesyesrwx
CommandResultMeaning
chmod 750 testrwxr-x---Owner read, write, execute; group read and execute; others nothing
chmod 543 testr-xr---wxOwner read and execute; group read only; others write and execute
chmod 777 testrwxrwxrwxEveryone can read, write, and execute (almost never a good idea)
chmod 640 testrw-r-----Owner read and write; group read; others nothing

5chmod, symbolic method

The symbolic method is chmod entity + or - permission filename. It adds or removes permissions without retyping the rest.

EntitiesOperatorsPermissions
u user owner · g group · o others · a all+ add · - remover read · w write · x execute

Entities and permissions can be combined in one command. Starting from rw-r-----:

CommandBeforeAfter
chmod ug+rx testfilerw-r-----rwxr-x---
chmod o+r testfilerw-r-----rw-r--r--
chmod go-w testfilerwxrwxrwxrwxr-xr-x
The key idea

Permissions read owner, group, others, each as r, w, x. In numeric chmod, add r = 4, w = 2, x = 1 for each digit. In symbolic chmod, pick who (u, g, o, a), add or remove (+ or -), and say which permission.

6Worked examples

Worked Example 1 Decode a permission string

What can the group do?

A file shows -rw-r-----. What can each entity do, and what is the numeric mode?

  1. 1
    Owner rw-: read and write; 4 + 2 = 6.
  2. 2
    Group r--: read only; 4.
  3. 3
    Others ---: nothing; 0.
Answer: Owner reads and writes, group reads, others have no access: 640.
Worked Example 2 Fix a permission

Patient data readable by everyone

In the listing above, patients.csv is rw-r--r--. Write a command that keeps drlee's and the nurses' access but removes everyone else's, both ways.

  1. 1
    Owner rw = 6, group r = 4, others none = 0.
  2. 2
    Numeric: chmod 640 patients.csv.
  3. 3
    Symbolic: only others' read must go, so chmod o-r patients.csv, which turns rw-r--r-- into rw-r-----.
Answer: chmod 640 patients.csv or chmod o-r patients.csv; both give rw-r-----.
Worked Example 3 Write the command

A script for the team

A script backup.sh should be readable, editable, and runnable by its owner; readable and runnable by its group; and readable and runnable by everyone else. What numeric command sets this?

  1. 1
    Owner rwx = 4 + 2 + 1 = 7.
  2. 2
    Group r-x = 4 + 1 = 5.
  3. 3
    Others r-x = 5.
Answer: chmod 755 backup.sh, shown as rwxr-xr-x.
Worked Example 4 Least privilege

The private key

A file holding a server's private key is set to chmod 777. Explain the risk and give a better setting.

  1. 1
    777 is rwxrwxrwx: every account on the system can read, change, or run it.
  2. 2
    Anyone who reads the private key can impersonate the server or decrypt its traffic.
  3. 3
    Only the owner needs to read and write it; no one else needs any access.
Answer: Use chmod 600, which gives rw-------.

7Common mistakes

Common Mistake 1 · Mixing up the order

It is always owner, group, others, and always read, write, execute. In 750 the 7 is the owner, not the others.

Common Mistake 2 · Adding the wrong values

Read is 4, write is 2, execute is 1. Read and write is 6, not 3; write and execute is 3.

Common Mistake 3 · Reaching for 777 to make something work

It gives every user full control. Grant the least access each entity needs.

Common Mistake 4 · Ignoring the + at the end

A + after the permissions means extra entries exist. Check them with getfacl.

Explain a permission change like this

“The file is currently rw-r--r--, so every user on the system can read patient data. Running chmod 640 patients.csv keeps read and write for the owner and read for the nurses group, and removes all access for others.”

Read the current setting, name who has too much access, and give the command with the result it produces.