online posts 16
linux and the command line 101

task 4.1 · chapter 4 of 8: users, permissions and sudo · 25 min

Who may do what

objective

Read the permissions in ls -l, find a file only root can read, use sudo, set permissions with chmod, and install a missing tool with apt.

you edit, in the labanswers/4.1.ymlwork/

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
LessonUsers, permissions and sudo
Whereyour codespace’s terminal. work/ is yours to make things in; Git ignores it
Your answersanswers/4.1.yml
new to this? step by stepbeginner
  1. ls -l /etc/shadow: its owner, group and permissions. cat /etc/shadow, then sudo head -n 1 /etc/shadow.
  2. mkdir -p work, touch work/private.txt, chmod 600 work/private.txt.
  3. Write work/run.sh (echo 'echo "it runs"' > work/run.sh), chmod 755 work/run.sh, run work/run.sh.
  4. Challenge: traceroute is missing. sudo apt update, then sudo apt install traceroute.
  5. Fill in answers/4.1.yml, then make check T=4.1.

more in meet the lab

1 explain

Owner, group, everyone else

Reading ls -l

Each line of ls -l starts with ten characters: the type, then three sets of three permissions.

-rw-r----- 1 root shadow 543 Nov  7 10:44 /etc/shadow
│└┬┘└┬┘└┬┘   └┬─┘ └─┬──┘
│ │  │  │     │     └── the file's group
│ │  │  │     └──────── its owner
│ │  │  └────────────── everyone else (other): ---  nothing
│ │  └───────────────── the group:             r--  read
│ └──────────────────── the owner:             rw-  read and write
└────────────────────── the type: - a file, d a folder
LetterOn a fileOn a folder
r readread itlist what’s in it
w writechange itadd, rename and delete files in it
x executerun it as a programgo into it (cd)

in one sentence Three sets of rwx: the owner’s, the group’s, and everyone else’s.

The numbers

Each set is also written as one digit, adding r = 4, w = 2 and x = 1. So rw- is 6, r-x is 5, rwx is 7 and --- is 0. Three digits cover the three sets:

NumberLettersTypical for
600rw-------private files: SSH keys, passwords
644rw-r--r--ordinary files: anyone may read, only you may change
755rwxr-xr-xprograms and folders: anyone may run or enter, only you may change
700rwx------private folders, like ~/.ssh

chmod 600 file sets them by number; chmod +x file and chmod o-w file add or take away one letter.

root and sudo

root is the user that permissions don’t stop. You aren’t root, and that’s on purpose: a mistake as root can break the whole machine. sudo command runs one command as root, when you’re allowed to. On a server it asks for your password and logs what you ran; in this codespace it doesn’t ask.

Installing software changes folders only root may write to, so apt, Debian’s package manager, needs sudo. apt update refreshes the list of what can be installed; apt install name installs it.

in one sentence sudo runs one command as root; installing with apt is one of the things that needs it.

2 show

A file you can’t read

$ ls -l /etc/shadow
-rw-r----- 1 root shadow 543 Nov  7 10:44 /etc/shadow
$ cat /etc/shadow
cat: /etc/shadow: Permission denied
$ id
uid=1000(vscode) gid=1000(vscode) groups=1000(vscode),104(docker),997(clab_admins),998(pipx),999(nvm),1001(ssh)

/etc/shadow holds the users’ password hashes. You’re vscode, not root and not in the shadow group, so you fall under other, and other has ---. id lists who you are and every group you’re in.

3 follow along

Read, then set, permissions

  1. Look at the file and try to read it:

    run in the terminal
    ls -l /etc/shadow
    cat /etc/shadow
    

    The owner, the group, and the last words of cat’s message go in shadow_owner, shadow_group and without_sudo.

  2. Now as root, just its first line:

    run in the terminal
    sudo head -n 1 /etc/shadow
    

    It works. The long string after root: is a hash, or * if the account has no password.

  3. Make yourself a folder to work in, and a private file in it. touch makes an empty file:

    run in the terminal
    mkdir -p work
    touch work/private.txt
    ls -l work
    chmod 600 work/private.txt
    ls -l work
    

    Watch the -rw-r--r-- turn into -rw-------.

  4. A tiny program, then try to run it before and after chmod:

    run in the terminal
    echo 'echo "it runs"' > work/run.sh
    work/run.sh
    chmod 755 work/run.sh
    work/run.sh
    

    The first try says Permission denied: no x, no running.

  5. Fill in the three answers:

    editanswers/4.1.ymlCtrl+P → 4.1.yml
    shadow_owner: root
    shadow_group: shadow
    without_sudo: Permission denied
    

in one sentence chmod 600 is private, chmod 755 is a program anyone can run; sudo does one thing as root.

4 challenge

Install what’s missing

Chapter 8 uses traceroute, and the codespace doesn’t have it:

$ traceroute
bash: traceroute: command not found

Install it with apt. Then traceroute --version to prove it’s there, and run make check T=4.1.

hint

Without sudo, apt can’t write where software lives. Read the error it prints: it says so.

hint

A fresh codespace has an empty package list, so apt says Unable to locate package. sudo apt update fetches the list first.

show the solution
sudo apt update
sudo apt install traceroute
traceroute --version

apt asks Do you want to continue? only when it needs to install other packages too; traceroute has none to add here, so it just installs. -y answers yes for you, which scripts use. The program lands in /usr/sbin/traceroute, a folder only root may write to, which is the whole reason for sudo.

5 check: run it, then prove it

$ make check T=4.1

when it's right, you'll see

╭─ task 4.1 · who may do what
│  ✓ who owns /etc/shadow (follow along): root
│  ✓ its group (follow along): shadow
│  ✓ what cat /etc/shadow says without sudo (follow along): Permission denied
│  ✓ work/private.txt is 600: only you can read and write it (follow along)
│  ✓ work/run.sh is 755: everyone can run it, only you can change it (follow along)
│  ✓ traceroute is installed in the codespace (challenge)
╰─ 6/6 checks passed  verified