online posts 16
linux and the command line 101

task 4.2 · chapter 4 of 8: users, permissions and sudo · 20 min

Too open, too closed

objective

Three files with the wrong permissions: a private key ssh refuses to use, a script that won’t run, and a notes file anyone can change. Read what each error says, and fix each with chmod.

you edit, in the labanswers/4.2.ymlwork/

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

given
LessonUsers, permissions and sudo
Set it upmake break T=4.2
The keywork/keys/lab_key: ws1 accepts it for user netops
Your answersanswers/4.2.yml
new to this? step by stepbeginner
  1. make break T=4.2, then ls -l work work/keys work/bin.
  2. ssh -i work/keys/lab_key netops@ws1: read the warning. Type yes if it asks about ws1’s key first.
  3. work/bin/hello.sh: Permission denied.
  4. Fix each with chmod, try again, fill in answers/4.2.yml, then make check T=4.2.

more in meet the lab

1 explain

Permissions that are wrong both ways

Too open

Some programs refuse to work when a file is readable by people who shouldn’t read it. ssh is the strict one: a private key, the file that proves who you are (chapter 6), must be readable by you alone. If the group or everyone else can read it, ssh won’t use it, because anyone else on the machine could copy it.

Too closed

The opposite mistake: a script without x can’t be run as a program, however correct the code in it is. The shell says Permission denied, which sounds like you aren’t allowed. You are; the file just isn’t marked as a program.

Writable by everyone

A file with w for other can be changed by every user on the machine. ls -l shows it as a w in the last set, like -rw-rw-rw-. It rarely breaks anything straight away, which is why it’s easy to miss.

in one sentence Keys must be private, programs need x, and nothing shared should be writable by everyone.

2 show

What make break leaves

$ ls -l work/keys work/bin work/team-notes.txt
-rw-rw-rw- 1 vscode vscode   64 Nov 11 05:52 work/team-notes.txt

work/bin:
total 4
-rw-r--r-- 1 vscode vscode 47 Nov 11 05:52 hello.sh

work/keys:
total 8
-rw-r--r-- 1 vscode vscode 399 Nov 11 05:52 lab_key
-rw-r--r-- 1 vscode vscode  89 Nov 11 05:52 lab_key.pub

lab_key.pub is the public half of the key pair; anyone may read that one. lab_key is the private half.

3 follow along

Meet the three errors

  1. Set the task up, and look at what it made:

    run in the terminal
    make break T=4.2
    ls -l work/keys work/bin work/team-notes.txt
    
  2. Try the key. If ssh first asks Are you sure you want to continue connecting?, type yes: that’s ws1 introducing itself, and chapter 6 is all about it.

    run in the terminal
    ssh -i work/keys/lab_key netops@ws1
    

    A box of @ signs, a warning in capitals, bad permissions, and then a password prompt: ssh ignored the key. Press Ctrl+C. The capitalised word that starts the warning goes in ssh_warning.

  3. Try the script:

    run in the terminal
    work/bin/hello.sh
    

in one sentence ssh says a key is too open; the shell says a script without x is Permission denied.

4 challenge

Fix all three

Change only the permissions, not the files’ owner or contents:

  1. work/keys/lab_key: only you can read it. Then ssh -i work/keys/lab_key netops@ws1 logs in without a password. exit comes back.
  2. work/bin/hello.sh: it runs, and only you can change it.
  3. work/team-notes.txt: everyone can read it, only you can change it.

Fill in ssh_warning and run make check T=4.2.

hint

Each of the three is one row of the table of numbers in task 4.1.

hint

ls -l after each chmod, to see that it did what you meant.

show the solution
chmod 600 work/keys/lab_key
chmod 755 work/bin/hello.sh
chmod 644 work/team-notes.txt
ssh -i work/keys/lab_key netops@ws1 hostname
work/bin/hello.sh
ssh_warning: UNPROTECTED

ssh’s warning is UNPROTECTED PRIVATE KEY FILE!, and it names the permissions it found: 0644. 400 works for the key too (read-only, even for you), and 700 works for the script. The checker accepts anything that’s private for the key, and runnable, but not writable by others, for the script.

5 check: run it, then prove it

$ make check T=4.2

when it's right, you'll see

╭─ task 4.2 · too open, too closed
│  ✓ the word ssh shouted about the key (challenge): ...
│  ✓ work/keys/lab_key: only you can read it (challenge): it is ...
│  ✓ and ssh logs in to ws1 with it (challenge)
│  ✓ work/bin/hello.sh runs, and only you can change it (challenge): it is ...
│  ✓ work/team-notes.txt: everyone can read it, not everyone can change it (challenge): it is ...
╰─ 5/5 checks passed  verified