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.
answers/4.1.ymlwork/open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Lesson | Users, permissions and sudo |
|---|---|
| Where | your codespace’s terminal. work/ is yours to make things in; Git ignores it |
| Your answers | answers/4.1.yml |
new to this? step by stepbeginner
ls -l /etc/shadow: its owner, group and permissions.cat /etc/shadow, thensudo head -n 1 /etc/shadow.mkdir -p work,touch work/private.txt,chmod 600 work/private.txt.- Write
work/run.sh(echo 'echo "it runs"' > work/run.sh),chmod 755 work/run.sh, runwork/run.sh. - Challenge:
tracerouteis missing.sudo apt update, thensudo apt install traceroute. - Fill in answers/4.1.yml, then
make check T=4.1.
more in meet the lab
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
| Letter | On a file | On a folder |
|---|---|---|
r read | read it | list what’s in it |
w write | change it | add, rename and delete files in it |
x execute | run it as a program | go 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:
| Number | Letters | Typical for |
|---|---|---|
600 | rw------- | private files: SSH keys, passwords |
644 | rw-r--r-- | ordinary files: anyone may read, only you may change |
755 | rwxr-xr-x | programs and folders: anyone may run or enter, only you may change |
700 | rwx------ | 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.
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.
Read, then set, permissions
Look at the file and try to read it:
run in the terminalls -l /etc/shadow cat /etc/shadowThe owner, the group, and the last words of cat’s message go in
shadow_owner,shadow_groupandwithout_sudo.Now as root, just its first line:
run in the terminalsudo head -n 1 /etc/shadowIt works. The long string after
root:is a hash, or*if the account has no password.Make yourself a folder to work in, and a private file in it.
touchmakes an empty file:run in the terminalmkdir -p work touch work/private.txt ls -l work chmod 600 work/private.txt ls -l workWatch the
-rw-r--r--turn into-rw-------.A tiny program, then try to run it before and after
chmod:run in the terminalecho 'echo "it runs"' > work/run.sh work/run.sh chmod 755 work/run.sh work/run.shThe first try says Permission denied: no
x, no running.Fill in the three answers:
editanswers/4.1.ymlCtrl+P → 4.1.ymlshadow_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.
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.1when 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