Course: Network and System Administration

In Class Lab: 15

Name: Special permissions and access control lists

Objective: Use the three special permissions, setuid, setgid and the sticky bit, and see the problem each one solves. Then use access control lists (ACLs) to give single users their own permissions on a directory, beyond the one owner and one group that standard permissions allow.

In Lab 6 you read and changed the standard permissions, and built a team directory for Ken and Richard. You also met three things that lab couldn't explain yet: the t at the end of the /tmp permission string, the chore of fixing the group of every new file in bell_plans, and the fact that a file has only one owner and one group. In this lab you will fix all three.

Note: This lab is done on your development server, NOT your production server. Connect with SSH from Terminal or PuTTY and log in as your own user.

This lab uses the users and the team directory you created in Lab 6: kthompson, rstallman, dritchie, and /usr/share/teams/bell_plans. Since Lab 7, those users can't log in with a password over SSH. You become them from your own session with sudo su - <user>, as in Lab 7, and type exit to come back.

Tip: Always check the prompt before you run a command. [kthompson@itfdn-dev01 ~]$ means you are Ken. If you are not sure, run whoami.

Tip: If you would rather keep a separate window open for each user, as in Lab 6, you can let them log in with your SSH key. See Appendix: Log in as the test users with SSH. It is optional, and every step in this lab works with sudo su - either way.

How to use the hints

Some steps give you every command. Others give you a Goal and ask you to work out the commands yourself. If you get stuck, open the hint. Once you have an answer, or if you are really stuck, open the solution and compare it with your own.

Lab overview

  1. Part 1: Read the special permissions
  2. Part 2: setuid, run a program as its owner - Task 1
  3. Part 3: setgid, keep the team's group - Task 2
  4. Part 4: The sticky bit, protect each user's files - Task 3
  5. Part 5: Access control lists - Task 4
  6. Part 6: Default ACLs and the mask - Task 5
  7. Part 7: Back up, remove and restore ACLs - Task 6
  8. Part 8: Save your notes in GitLab - Task 7

If you get stuck, check the Troubleshooting section at the end of this lab.

The examples in this lab use the username angus. Always use your username.


Part 1: Read the special permissions

Start with two files you have already seen. /usr/bin/passwd is the program that changes passwords:

ls -l /usr/bin/passwd ls -ld /tmp
-rwsr-xr-x. 1 root root ... /usr/bin/passwd drwxrwxrwt. 9 root root ... /tmp

Look at the letters where you would expect an x. passwd has an s in the user set, and /tmp has a t in the other set. These are two of the three special permissions:

Permission Shows as Number On a file On a directory
setuid s in the user set 4 The program runs as the file's owner, not as the user who started it. Not used on Linux.
setgid s in the group set 2 The program runs with the file's group. New files and directories inside get the directory's group.
sticky bit t in the other set 1 Not used on Linux. Only a file's owner, the directory's owner, or root can delete or rename a file inside.

Note: Linux ignores setuid and setgid on scripts, like the ones you wrote in Lab 11. They only work on compiled programs, such as passwd.

The fourth digit

In Lab 6 you used three digits with chmod, such as 755. The special permissions add a fourth digit in front. 4755 is setuid plus 755. The stat command shows all four digits:

First we just run stat to see the default details exposed.

stat /usr/bin/passwd

Next we use the -c option to display targeted results.

stat -c "%a %A %n" /usr/bin/passwd stat -c "%a %A %n" /tmp stat -c "%a %A %n" /etc/passwd
4755 -rwsr-xr-x /usr/bin/passwd 1777 drwxrwxrwt /tmp 644 -rw-r--r-- /etc/passwd
Part What it does
stat Shows details about a file.
-c "..." Prints only what you ask for, in the format you give.
%a The permissions as a number. A leading 0 is left out, so 644 is really 0644.
%A The permissions as a string, like ls -l.
%n The file name.

Lowercase and uppercase

Each special permission shares a spot with an x. Try it on two practice files. You will keep your files for this lab in ~/inclass/lab-15:

mkdir -p ~/inclass/lab-15 cd ~/inclass/lab-15 echo "practice file" > data1.txt echo "practice program" > data2.txt chmod 644 data1.txt chmod 755 data2.txt ls -l

data1.txt has no x anywhere, and data2.txt has x in every set. Now add setgid (2) to both, and look again:

chmod 2644 data1.txt chmod 2755 data2.txt ls -l
-rw-r-Sr--. 1 angus angus ... data1.txt -rwxr-sr-x. 1 angus angus ... data2.txt

Question: One file shows an uppercase S and the other a lowercase s. Why?

Answer

A lowercase s means the special permission and the x below it are both set. An uppercase S means the special permission is set, but the x is not. data1.txt has no group x, so it shows S. A setgid bit on a file nobody can run does nothing, so an uppercase S on a file usually means a mistake. You will see in Part 4 that an uppercase T on a directory can be normal.

Now use setuid (4) instead:

chmod 4644 data1.txt chmod 4755 data2.txt ls -l
-rwSr--r--. 1 angus angus ... data1.txt -rwsr-xr-x. 1 angus angus ... data2.txt

The s moved to the user set. The setgid bit is gone, because a number sets all four digits at once.

special perms example

You can also use letters, as with chmod g+w in Lab 6:

Command What it does
chmod u+s file Adds setuid.
chmod g+s dir Adds setgid.
chmod +t dir Adds the sticky bit.
chmod u-s file Removes setuid. g-s and -t remove the other two.

Part 2: setuid, run a program as its owner

In Lab 6 you saw that only root can read /etc/shadow, where the password hashes are kept. Yet you can change your own password with passwd, without sudo, and passwd has to write to that file. Compare the two:

ls -l /etc/shadow ls -l usr/bin/passwd
----------. 1 root root ... /etc/shadow -rwsr-xr-x. 1 root root ... /usr/bin/passwd

passwd is owned by root and has setuid, so it runs as root no matter who starts it. Root can write to /etc/shadow. The program itself checks who started it, and only lets a normal user change their own password.

See it happen

The id command prints who you are. Make a copy of it, give the copy to root, and add setuid:

cd ~/inclass/lab-15 cp /usr/bin/id myid sudo chown root:root myid sudo chmod u+s myid ls -l myid
Command What it does
cp /usr/bin/id myid Copies the id program. You own the copy.
sudo chown root:root Makes root the owner. Only root can give a file away.
sudo chmod u+s Adds setuid. You no longer own the file, so this needs sudo too.

ls -l should show -rwsr-xr-x. 1 root root. Now run the real id, then your copy:

id ./myid
uid=1000(angus) gid=1000(angus) groups=1000(angus),10(wheel),1003(research) context=... uid=1000(angus) gid=1000(angus) euid=0(root) groups=1000(angus),10(wheel),1003(research) context=...

Your copy prints an extra field, euid=0(root). The uid is still you, the user who started the program. The euid (effective user ID) is root, and it is the one Linux checks when the program opens a file. For as long as myid runs, it has root's permissions.

Note: Do chown before chmod. Changing a file's owner removes setuid, so a program can't keep setuid after it is given away.

Warning: A setuid root program can do anything root can. Never add setuid to a copy of a shell, an editor, or any program that wasn't written to be setuid. id only prints information, which is why it is safe for this test.

You don't need myid any more. Delete it:

rm myid

rm asks remove write-protected regular file 'myid'?. Type y and press Enter.

Question: Root owns myid, and you are not root. Why were you allowed to delete it?

Answer

Deleting a file changes the directory that lists it, not the file itself. You own ~/inclass/lab-15 and have w on it, so you can delete any file in it. rm only asks first because you can't write to the file. Keep this in mind for Part 4.

Task 1: Find the setuid programs

Goal: List every setuid program in /usr/bin, with its owner and permissions. Pick one you have used, and explain why it needs to run as root.

Hint
Solution
find /usr/bin -perm -4000 find /usr/bin -perm -4000 | xargs ls -l

Your list will include programs such as these, all owned by root:

Program Why it needs root
passwd Writes your new password hash to /etc/shadow.
sudo, su Run commands as another user, which only root can do.
mount, umount Attach and detach file systems.
chage, gpasswd, newgrp Change password ages and groups in files only root can change.

Going further: find the setgid programs too. Errors from directories you can't read are sent to /dev/null, as in Lab 5:

find /usr/bin /usr/libexec -perm -2000 2>/dev/null | xargs ls -l

You may see write, with the group tty, and ssh-keysign, with the group ssh_keys. Each runs with a group that lets it reach files a normal user can't.


Part 3: setgid, keep the team's group

In Lab 6, Task 5, Ken couldn't add to Richard's ideas.txt because the new file got Richard's primary group, rstallman, instead of research. You fixed it with chgrp. With setgid on the directory, Linux does that for you.

First, check that the team directory from Lab 6 is still there:

ls -ld /usr/share/teams/bell_plans ls -l /usr/share/teams/bell_plans
drwxrwx---. 2 kthompson research ... /usr/share/teams/bell_plans

Note: If you get No such file or directory or Permission denied, see Troubleshooting.

Without setgid

Become Richard, and create a file:

sudo su - rstallman cd /usr/share/teams/bell_plans echo "Richard's plan before setgid" > before.txt ls -l before.txt exit
-rw-rw-r--. 1 rstallman rstallman ... before.txt

The group is rstallman again, so Ken can't write to it.

Add setgid

Only the owner of a directory, or root, can change its permissions. Ken owns bell_plans, so Ken adds setgid:

sudo su - kthompson cd /usr/share/teams/ chmod g+s /usr/share/teams/bell_plans ls -ld /usr/share/teams/bell_plans exit
drwxrws---. 2 kthompson research ... /usr/share/teams/bell_plans

The group set is now rws. Become Richard again, and create a second file:

sudo su - rstallman cd /usr/share/teams/bell_plans echo "Richard's plan after setgid" > after.txt ls -l exit
-rw-rw-r--. 1 rstallman research ... after.txt -rw-rw-r--. 1 rstallman rstallman ... before.txt ...

after.txt got the directory's group, research, with no chgrp. The group already has rw-, so Ken can change it.

Note: New files on RHEL give the group write permission (rw-rw-r--) because of a setting called the umask. This lab doesn't change it.

Question: Why is before.txt still in the rstallman group?

Answer

setgid only decides the group of new files. Files that were there before keep the group they had. You fix those the way you did in Lab 6, with chgrp.

Task 2: New directories and old files

Goal: As Richard, create a directory called drafts inside bell_plans, and a file inside drafts. Before you look, predict the group of each, and whether drafts gets setgid too. Then fix before.txt so it belongs to research.

Hint
Solution
sudo su - rstallman cd /usr/share/teams/bell_plans mkdir drafts echo "first draft" > drafts/draft1.txt ls -ld drafts ls -l drafts chgrp research before.txt ls -l ls -l drafts exit
drwxrwsr-x. 2 rstallman research ... drafts -rw-rw-r--. 1 rstallman research ... draft1.txt

A new directory gets the group and the setgid bit, so everything below bell_plans stays in research, however deep it goes.

Going further: mv keeps a file's group, because it doesn't create a new file. cp does create one, so the copy gets the directory's group.


Part 4: The sticky bit, protect each user's files

In Part 2 you saw that deleting a file needs w on the directory, not on the file. /tmp gives everyone rwx, so without the t, anyone could delete anyone's files there. Check it, and create a file of your own:

ls -ld /tmp echo "user: $USER date: $(date)" > /tmp/srv-info.txt ls -l /tmp/srv-info.txt

Become Ken, and try to delete your file:

sudo su - kthompson rm /tmp/srv-info.txt

rm asks remove write-protected regular file. Type y, and you see:

rm: cannot remove '/tmp/srv-info.txt': Operation not permitted

Still as Ken, create a file of his own, then go back to your user:

echo "Content created on $HOSTNAME by $USER" > /tmp/content-tracker.txt ls -l /tmp/content-tracker.txt exit

Now try to delete Ken's file, without sudo:

whoami rm /tmp/content-tracker.txt

Question: You have w on /tmp. Why can't you delete content-tracker.txt?

Answer

/tmp has the sticky bit. In a sticky directory, only the file's owner, the directory's owner (root, for /tmp) or root can delete or rename a file. You are none of those for Ken's file. With sudo you are root, so sudo rm would work.

Clean up both files. Yours needs nothing special. Ken's needs root:

rm /tmp/srv-info.txt sudo rm /tmp/content-tracker.txt

The team has the same problem

bell_plans gives the whole research group rwx. That lets the team share files, but it also lets any member delete any other member's work. Watch Richard delete Ken's notes:

sudo su - kthompson echo "Ken's notes, please keep" > /usr/share/teams/bell_plans/ken-notes.txt exit sudo su - rstallman rm /usr/share/teams/bell_plans/ken-notes.txt ls /usr/share/teams/bell_plans exit

rm doesn't even ask, because Richard can write to the file through the research group. ken-notes.txt is gone.

Task 3: Protect Ken's notes

Goal: Change bell_plans so that team members can't delete files they don't own. Before you look, predict how the permission string will change. Then have Ken create ken-notes.txt again, and prove that Richard can't delete it. Can Richard still change what is in it?

Hint
Solution
sudo su - kthompson chmod +t /usr/share/teams/bell_plans ls -ld /usr/share/teams/bell_plans echo "Ken's notes, please keep" > /usr/share/teams/bell_plans/ken-notes.txt exit sudo su - rstallman rm /usr/share/teams/bell_plans/ken-notes.txt echo "Richard was here" >> /usr/share/teams/bell_plans/ken-notes.txt cat /usr/share/teams/bell_plans/ken-notes.txt exit
drwxrws--T. 2 kthompson research ... /usr/share/teams/bell_plans

The T is uppercase because other has no x. That is normal here: the sticky bit still protects the research members from each other, and everyone else is kept out anyway.

Richard's rm gets Operation not permitted, but his echo >> works. The sticky bit only stops delete and rename. Richard can still change the file, because the research group has rw- on it.

Ken owns bell_plans, so he can still delete anyone's files in it, as root can in /tmp.


Part 5: Access control lists

Standard permissions give a file one owner, one group, and everyone else. In Lab 6, the only way to let Dennis into the team's files was to add him to research, which would give him everything the team has. An access control list (ACL) adds entries for named users and groups, each with their own permissions.

Command What it does
getfacl Get a file's ACL. Shows its entries.
setfacl Set a file's ACL. Adds, changes or removes entries.

Both are installed on RHEL, and the RHEL file system supports ACLs without any setup.

A directory only root can use

Create a directory at the top of the file system that only root can use:

sudo mkdir /secure sudo chmod 700 /secure ls -ld /secure ls /secure getfacl /secure
drwx------. 2 root root ... /secure ls: cannot open directory '/secure': Permission denied getfacl: Removing leading '/' from absolute path names # file: secure # owner: root # group: root user::rwx group::--- other::---
Line What it means
user::rwx The owner (the user with no name between the colons).
group::--- The group owner.
other::--- Everyone else.

With no ACL, getfacl shows the same three sets as ls -l. Remember the Removing leading '/' message. You will need it in Part 7.

Note: getfacl works even though ls is denied. Like ls -ld in Lab 6, it only reads information about the directory, not what is inside it.

Add an entry for yourself

sudo setfacl -m u:$USER:rx /secure ls -ld /secure getfacl /secure ls /secure touch /secure/test.txt
Part What it does
-m Modifies the ACL: adds an entry, or changes one that exists.
u:$USER:rx An entry for the user named in $USER, with r and x. r-x works too. Use g:name: for a group.
drwxr-x---+ 2 root root ... /secure
# file: secure # owner: root # group: root user::rwx user:angus:r-x group::--- mask::r-x other::---

The . at the end of the permission string is now a +, which means the file has an ACL. Your named entry, user:angus:r-x, lets you list the directory with ls, but touch is still Permission denied, because you have no w.

The mask is the most that any named entry, or the group owner, can get. setfacl sets it for you to cover the entries you add.

Common Pitfall: When a file has an ACL, the group set in ls -l shows the mask, not the group owner. ls -ld /secure shows r-x for the group, but the root group has ---. Always check with getfacl.

Quick reference

Command What it does
getfacl file Shows a file's or a directory's ACL.
setfacl -m u:name:rwx file Adds or changes the entry for a user.
setfacl -m g:name:rx file Adds or changes the entry for a group.
setfacl -m u:a:rwx,u:b:rx file Adds several entries at once, separated by commas.
setfacl -x u:name file Removes the entry for a user. You give no permissions.
setfacl -b file Removes all ACL entries, leaving only the standard permissions.
-R With getfacl or setfacl, applies to a directory and everything inside it.

Task 4: Let Ken and Dennis in

Goal: Give kthompson full access (rwx) to /secure, and give dritchie read only access (r-x, so he can list and enter it). rstallman gets no entry. Check the ACL, then test as each of the three users: can they list /secure, and can they create a file in it?

Hint
Solution
sudo setfacl -m u:kthompson:rwx,u:dritchie:rx /secure getfacl /secure
# file: secure # owner: root # group: root user::rwx user:angus:r-x user:dritchie:r-x user:kthompson:rwx group::--- mask::rwx other::---

The mask grew to rwx to cover Ken's entry. Now test:

sudo su - kthompson touch /secure/ken.txt ls -l /secure exit sudo su - dritchie ls -l /secure touch /secure/dennis.txt exit sudo su - rstallman ls /secure exit
User ls /secure touch
kthompson Works Works
dritchie Works Permission denied
rstallman Permission denied Not tried

Richard has no named entry, so the other entry, ---, applies to him.


Part 6: Default ACLs and the mask

Look at ken.txt, the file Ken created in Task 4:

getfacl /secure/ken.txt

It has no named entries. The ACL on a directory controls the directory. It isn't copied to the files created inside it. For that, a directory needs a default ACL: entries that every new file and directory inside it gets. Only directories have a default ACL.

Add default entries for Ken and Dennis. A d: in front of an entry makes it a default entry:

sudo setfacl -m d:u:kthompson:rwx,d:u:dritchie:rx /secure getfacl /secure
# file: secure # owner: root # group: root user::rwx user:angus:r-x user:dritchie:r-x user:kthompson:rwx group::--- mask::rwx other::--- default:user::rwx default:user:dritchie:r-x default:user:kthompson:rwx default:group::--- default:mask::rwx default:other::---

setfacl filled in the default:user::, default:group::, default:other:: and default:mask:: lines from the directory's own permissions. getfacl -d /secure shows only the default entries.

Now become Ken, and create a new file:

sudo su - kthompson cd /secure echo "hardware details report" > srv-info.txt echo "dell r760 online host: ul-srv1" >> srv-info.txt ls -l srv-info.txt getfacl srv-info.txt exit
-rw-rw----+ 1 kthompson kthompson ... srv-info.txt
# file: srv-info.txt # owner: kthompson # group: kthompson user::rw- user:dritchie:r-x #effective:r-- user:kthompson:rwx #effective:rw- group::--- mask::rw- other::---

The new file got the default entries. But look at the #effective comments. echo creates files without x, so the mask on the new file is rw-. Every named entry is limited by the mask:

Entry Entry gives Mask Effective
user:kthompson:rwx rwx rw- rw-
user:dritchie:r-x r-x rw- r--

So Dennis can read srv-info.txt, but not change or run it. The mask is the safety limit on everything an ACL can give, and only the owner's user:: and the other:: entries ignore it.

Question: Who can change srv-info.txt? Can you read it? Try cat /secure/srv-info.txt.

Answer

Only Ken (and root) can change it. Dennis can read it. You can't: you have r-x on the directory, so you can list it, but there is no default entry for you. The new file gives you other::---. Access to a directory doesn't carry over to the files inside it.

Task 5: Read Ken's new files

Goal: Make sure your own user can read every new file Ken creates in /secure from now on. Test it by having Ken create srv-info2.txt. Can you read srv-info.txt now? If not, give yourself read access to that one file too.

Hint
Solution
sudo setfacl -m d:u:$USER:rx /secure sudo su - kthompson echo "second report" > /secure/srv-info2.txt exit cat /secure/srv-info2.txt cat /secure/srv-info.txt

srv-info2.txt works, and srv-info.txt is still Permission denied. Fix the old file:

sudo setfacl -m u:$USER:r /secure/srv-info.txt cat /secure/srv-info.txt

Ken owns srv-info.txt, so he could also run setfacl -m u:angus:r srv-info.txt himself, without sudo. The owner of a file can change its ACL.

Going further: to fix every file and directory at once, use sudo setfacl -R -m u:$USER:rX /secure. An uppercase X adds x only to directories, and to files that already have it, so text files don't become programs.


Part 7: Back up, remove and restore ACLs

ACLs take time to build, and one wrong command can remove them. getfacl output is also a backup that setfacl can read back. Save the ACLs of /secure and everything in it:

cd ~/inclass/lab-15 getfacl -R /secure > secure-acl-backup.txt cat secure-acl-backup.txt
# file: secure # owner: root # group: root user::rwx ... # file: secure/srv-info.txt ...

The Removing leading '/' message goes to your screen, not the file, because it is an error message (Lab 5). Each path in the backup starts with secure, not /secure. Keep that in mind.

Note: You don't need sudo for this backup. Your ACL entry gives you r-x on /secure, which is enough to list the files and read their ACLs.

Remove all the entries

-b removes every ACL entry, normal and default, and leaves only the standard permissions:

sudo setfacl -b /secure getfacl /secure ls -ld /secure ls /secure

/secure is back to drwx------., and you have locked yourself out. Without -R, -b only changed /secure itself. The files inside still have their ACLs.

Restore from the backup

setfacl --restore reads a getfacl file. Because the paths in your backup start with secure, setfacl looks for them in the directory you are in. Run it from /, and go back afterwards:

cd / sudo setfacl --restore=$HOME/inclass/lab-15/secure-acl-backup.txt getfacl /secure ls /secure cd ~/inclass/lab-15
Part What it does
cd / Makes secure in the backup mean /secure.
sudo A restore also sets the owner and group from the backup, which needs root.
--restore= The backup file to read.
$HOME Your home directory. A ~ right after = isn't expanded by the shell, so $HOME is used instead.

Your entries, and the default entries, are back.

Common Pitfall: If you run the restore from your home directory, you get an error such as setfacl: secure: No such file or directory, because there is no secure directory there. You don't need to edit the backup. Just cd / first.

Tip: getfacl -R -p /secure keeps the leading / in the backup, so it can be restored from any directory.

Task 6: Dennis leaves the project

Goal: Dennis has left the project. Remove only his entries from /secure, both the normal entry and the default entry, and prove he can no longer list it. Then it turns out that was a mistake: put everything back from your backup, and check that Dennis's entries have returned.

Hint
Solution
sudo setfacl -x u:dritchie /secure sudo setfacl -x d:u:dritchie /secure getfacl /secure sudo su - dritchie ls /secure exit

Dennis gets Permission denied. His entries on the files inside /secure are still there, but he can't get past the directory, just as in Lab 6.

Put it back:

cd / sudo setfacl --restore=$HOME/inclass/lab-15/secure-acl-backup.txt getfacl /secure | grep dritchie cd ~/inclass/lab-15
user:dritchie:r-x default:user:dritchie:r-x

Part 8: Save your notes in GitLab

Add what you have learned to your GitLab repository from Lab 10:

cd ~/git/gitlab/itfdn-140-inclass-$USER git pull echo "# Special permissions and ACLs The following are notes and tips related to **setuid**, **setgid**, the **sticky bit**, **getfacl** and **setfacl**. " >> acl-tips.md cat acl-tips.md

Goal: Add at least three tips or commands to acl-tips.md that you would want next time. Add a link to the file at the end of README.md. Commit both files in one commit, and push. In GitLab, click the link in your README and check that it opens your notes.

Hint
Solution
vim acl-tips.md

Add your own tips, then save with :wq. For example:

## Special permissions `chmod g+s dir` makes new files in `dir` get its group. `chmod +t dir` stops users deleting each other's files. ## Give one user access `sudo setfacl -m u:name:rx dir`, then check with `getfacl dir`. ## Back up and restore `getfacl -R /dir > backup.txt`, then `cd /` and `sudo setfacl --restore=backup.txt`.

Add the link, then check, commit and push:

echo "" >> README.md echo "[Special permissions and ACLs](acl-tips.md)" >> README.md git status git diff git add README.md acl-tips.md git commit -m "docs: add special permissions and ACL tips and link from README" git push git log --oneline

Lab summary

You have now:

bell_plans now shows drwxrws--T, and /secure keeps the ACL you restored.

Questions to consider

  1. Why does passwd need setuid? Why is a bug in a setuid root program more dangerous than a bug in a normal program?
  2. What does setgid do on a program, and what does it do on a directory?
  3. In bell_plans, what problem does setgid solve, and what problem does the sticky bit solve? Can Ken still delete Richard's files? Why?
  4. When would you use an ACL instead of adding a user to a group?
  5. Dennis's default entry was r-x, but on Ken's new file it was only r--. Why?
  6. A user can list a directory but can't read the new files in it. What would you check?
  7. Why does a backup made with getfacl -R /secure need to be restored from /?

Troubleshooting

Q. sudo su - kthompson says "user kthompson does not exist".

A. You haven't created the users from Lab 6. Go back to Lab 6, Part 2, and create kthompson and rstallman. dritchie comes from Lab 2.

Q. /usr/share/teams/bell_plans says "No such file or directory".

A. You didn't finish Lab 6. Recreate the team directory with these commands, then carry on:

sudo mkdir -p /usr/share/teams/bell_plans sudo chown root:research /usr/share/teams sudo chmod 770 /usr/share/teams sudo chown kthompson:research /usr/share/teams/bell_plans sudo chmod 770 /usr/share/teams/bell_plans

Q. ls -ld /usr/share/teams/bell_plans says "Permission denied" as my own user.

A. Your user isn't in research, which was Lab 6, Task 6. Run sudo usermod -aG research $USER, then log out and log in again. The other steps in Parts 3 and 4 run as Ken or Richard, so they work either way.

Q. chmod says "Operation not permitted".

A. Only the owner of a file or directory, or root, can change its permissions. Check the prompt, or run whoami. The changes to bell_plans must be made as kthompson.

Q. I ran chmod g+s but no s appears.

A. If the owner isn't a member of the file's group, Linux quietly leaves setgid off. Check with id kthompson that Ken is in research.

Q. rm asks "remove write-protected regular file?"

A. You can't write to the file, so rm checks first. Type y to try anyway. Whether the delete works depends on the directory, as you saw in Parts 2 and 4.

Q. setfacl says "Invalid argument near character" or "invalid user".

A. Check the entry. It must be u:name:perms or g:name:perms, with no spaces, and the user or group must exist. Check a user with id name.

Q. setfacl --restore says "No such file or directory".

A. The paths in your backup start with secure, not /secure. Run cd / first, then the restore again. See Part 7.

Q. I can list /secure, but cat on a file inside it says "Permission denied".

A. Your entry on the directory doesn't apply to the files. Check the file with getfacl. See Task 5.

Q. ls -l shows rw for the group, but the group can't write.

A. On a file with an ACL, the group set in ls -l is the mask. Check the real group:: entry with getfacl.

Q. git push is rejected.

A. Your GitLab repository has a change you don't have yet. Run git pull, then git push again, as in Lab 10.

If you can't solve a problem, post it in this week's Teams channel, or reach out by email for help.


Appendix: Log in as the test users with SSH

Note: This appendix is optional. Do it on your development server only. The test users don't exist on your production server, and shouldn't.

Since Lab 7, your development server refuses passwords over SSH, so dritchie, kthompson and rstallman can't log in. In this appendix you give them the same key you already use, by hand, just as you built your own authorized_keys in Lab 7. Then you can open a window for each user, as in Lab 6.

Give Dennis your key

Step 1: As your own user on your development server, show your keys:

cat ~/.ssh/authorized_keys

You should see two lines: your laptop key from Lab 7, ending in your key comment, and the dev01 key from Lab 9, ending in dev01. Copy the whole laptop line, from ssh-ed25519 to the comment. It must stay one line.

Tip: You can also copy your public key from your laptop, as in Lab 7, Part 5: Windows from the PuTTYgen box, Mac and Linux with cat ~/.ssh/id_ed25519.pub.

Step 2: Become Dennis, and build his .ssh directory and authorized_keys file the way you built yours. Type echo ', paste your key, then type ' >> ~/.ssh/authorized_keys:

sudo su - dritchie mkdir ~/.ssh chmod 700 ~/.ssh echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...your key... yourname-mac' >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys ls -la ~/.ssh ssh-keygen -l -f ~/.ssh/authorized_keys exit
Command What it does
sudo su - dritchie You work as Dennis, so the files you create are owned by dritchie.
mkdir, chmod 700 Creates Dennis's private .ssh directory.
echo '...' >> Adds your public key to the end of Dennis's authorized_keys.
chmod 600 Only Dennis can read or write the file. This is the stricter choice the Lab 7 question suggested.
ssh-keygen -l -f Prints the key's fingerprint. It should match your own key's.

Common Pitfall: Don't create these files with sudo mkdir or sudo echo. They would be owned by root, not Dennis, and sshd refuses keys in a file with the wrong owner. You saw bad ownership or modes in Lab 7, Task 3.

Step 3: Test it from your laptop, in a new window. Your agent already holds your key, so you are not asked for a passphrase or a password.

whoami should print dritchie.

Your turn: Ken and Richard

Goal: Give kthompson and rstallman your key in the same way. Then open three windows, one for each of your own user, kthompson and rstallman, as in Lab 6.

Hint

Repeat Step 2 after sudo su - kthompson, then after sudo su - rstallman. Type exit in between. Paste the same key each time.

Solution
sudo su - kthompson mkdir ~/.ssh chmod 700 ~/.ssh echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...your key... yourname-mac' >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys ls -la ~/.ssh exit

Repeat with sudo su - rstallman. Then, from your laptop, use ssh kthompson@dev01 and ssh rstallman@dev01, or save PuTTY sessions dev01-kthompson and dev01-rstallman.

Question: Your one key now logs in as four different users. On a real server, why would that be a bad idea?

Answer

Anyone with your private key can log in as all four users, and the server's log can't tell you which person logged in, only which key was used. On a real server, each person has their own account and their own key, and a key is removed when that person leaves. Here the users are only test accounts for one person, you, on a development server, so sharing a key keeps the setup simple.

Stretch: A key for each user

Goal: Create a separate key pair for each test user, load them all into your agent, and replace your shared key in each user's authorized_keys with that user's own key. Prove which key each login used.

Hint
Solution

Mac and Linux, on your laptop. Use a passphrase for each key:

ssh-keygen -t ed25519 -f ~/.ssh/id_dritchie -C "dritchie-test" ssh-keygen -t ed25519 -f ~/.ssh/id_kthompson -C "kthompson-test" ssh-keygen -t ed25519 -f ~/.ssh/id_rstallman -C "rstallman-test" ssh-add ~/.ssh/id_dritchie ~/.ssh/id_kthompson ~/.ssh/id_rstallman ssh-add -l cat ~/.ssh/id_dritchie.pub
Part What it does
-f ~/.ssh/id_dritchie Saves the private key as id_dritchie and the public key as id_dritchie.pub.
-C "dritchie-test" The comment, so you can tell the keys apart in authorized_keys and ssh-add -l.
ssh-add with several files Loads every key you name into the agent.

ssh-add -l should now list four keys: your own, and one for each test user.

Windows: in PuTTYgen, generate a key with the comment dritchie-test, set a passphrase, click Save private key as dritchie.ppk, and copy the public key from the box. Repeat for Ken and Richard. Add each .ppk to Pageant, and check View Keys lists all four.

On your development server, add Dennis's new key, then remove the shared one:

sudo su - dritchie echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...new key... dritchie-test' >> ~/.ssh/authorized_keys vim ~/.ssh/authorized_keys

In Vim, move to the line that ends in your laptop key comment, press dd, then save with :wq. Check that only Dennis's key is left:

wc -l ~/.ssh/authorized_keys ssh-keygen -l -f ~/.ssh/authorized_keys exit

Repeat for Ken and Richard with their own keys. Then log in from your laptop as each user, and check which key was used on the server, as your own user:

sudo grep Accepted /var/log/secure | tail -n 3

Each line ends with the fingerprint of the key that logged in. Match them with ssh-add -l (Mac and Linux) or Pageant's View Keys (Windows). Each user should match their own key, not yours.

Your agent offers each key in turn until the server accepts one. That is why one agent can hold keys for all four users.

Going further: sshd allows 6 tries by default. With many keys in your agent, you may see Too many authentication failures before the right key is offered. On Mac and Linux, add a Host block for each user to ~/.ssh/config that names the key to use. UTM users set HostName to their VM's IP address and leave out Port:

Host dev01-dritchie HostName 127.0.0.1 Port 2222 User dritchie IdentityFile ~/.ssh/id_dritchie IdentitiesOnly yes

IdentitiesOnly yes makes ssh offer only the key in IdentityFile, so ssh dev01-dritchie works on the first try. Windows users set the key under Connection -> SSH -> Auth -> Credentials in the saved session.


Lab complete