Course outline · 0% complete

0/29 lessons0%

Course overview →

Who can do what: permissions and chmod

lesson 6-1 · ~10 min · 16/29

Quiz

Warm-up from lesson 2-3: in `ls -l` output, what appears at the very start of each line (like -rw-r--r--)?

Reading -rw-r--r--

You will meet permissions the hard way soon: a script that refuses to run, a Permission denied on a file you can see, ssh rejecting your key because other users could read it. Knowing how to read the permission string turns each of those from a mystery into a ten-second fix.

Linux is multi-user — several accounts share one machine — so every file records who may do what. Three actions, three audiences:

  • Actions: read, write, execute (run it as a program, or enter it if it's a directory)
  • Audiences: the file's user (owner), its group, and others (everyone else)

The 10-character string packs all of that:

positionmeaning
1type: - file, d directory
2-4owner's r w x
5-7group's r w x
8-10others' r w x

So -rw-r--r-- means: regular file, owner can read+write, group can read, others can read. A dash means "not allowed".

-rwxr-xr--typeownergroupothers7544+2+1 (rwx)4+0+1 (r-x)4+0+0 (r--)= chmod 754r=4 w=2 x=1
Each rwx triplet is a number: r=4, w=2, x=1, added up. rwxr-xr-- reads as 754.

Changing permissions: chmod

chmod (change mode) speaks two dialects:

Numeric: one digit per audience, adding r=4, w=2, x=1.

chmod 755 script.sh   # rwx r-x r-x
chmod 600 secret.txt  # rw- --- ---

Symbolic: who, then + or -, then what.

chmod u+x script.sh   # give the owner execute
chmod go-r notes.txt  # remove read from group and others

The one you'll type most in your career is chmod +x script.sh: make a script executable. That's the doorway to unit 8.

Code exercise · bash

Run it. A fresh file starts as rw-r--r--, then chmod 755 turns on execute. The `cut -c1-10` just trims each ls -l line to its first 10 characters, the permission string. (umask 022 pins the default permissions so the output is predictable.)

Quiz

What does chmod 600 diary.txt allow?

Code exercise · bash

Your turn. Create `secret.txt`, lock it down so ONLY the owner can read and write it (one numeric chmod), and show the permission string. Expected output: ``` -rw------- ```