Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Part 2 · System administration · Lesson 07 · 35 minutes

Give limited rights with sudo rules and ACLs

After this lesson you can
  • Run a command as another user and write a sudo rule that a checker accepts
  • Install a sudo rule for one exact command, and for a group
  • Give one user access to one file with an access control list
Lab files of this lesson (11)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.

Scenario

The finance team has its accounts (lesson 6). Now two requests arrive. A team member must read the status of one service as root, but nobody gets full root. An auditor must read a single file, but must not join the team. You solve both with the smallest right that works: one sudo rule for one command, and one ACL entry for one file. You use accounts whose names start with kr_, so you can remove them at the end.

Recall

Which command lists the groups of kr_ana, and which one lists the members of the group kr_ops?

id kr_ana (or groups kr_ana) lists the groups of an account. getent group kr_ops prints the line of the group, and its last field holds the members.

Which command shows whether the password of kr_ana is usable, locked or empty, and which letters does it print?

sudo passwd -S kr_ana. The second field is P (usable password), L (locked) or NP (no password).

The idea

The normal bits (owner, group, other) are coarse. Two tools give a right to one named person without changing the bits for everyone. The directory /etc/sudoers.d/ holds the rules for administrator rights, and an access control list (ACL) adds a rule for a named user or group on one file.

A line of a sudo rule
kr_anawho (a group starts with %)ALLon which hosts(root)as which userNOPASSWD:no password is asked/usr/bin/systemctl...the one command that is allowedkr_anawho (a group starts with %)ALLon which hosts(root)as which userNOPASSWD:no password is asked/usr/bin/systemctl...the one command that is allowed

A rule with a full path and arguments allows only that command with those arguments.

CommandWhat it does
sudo -u NAME COMMANDRun one command as another user
visudo -cf FILECheck a sudo rule file without installing it
sudo -l -U NAMEList what a user may run through sudo
getfacl, setfaclShow and set access control lists

Worked example

You need the package acl (the set-up installed it; if it is missing, run sudo apt install acl). The user at the prompt is you.

1. Make the accounts again

Lesson 6 removed its accounts. Make kr_ana (the team member) and kr_ben (the auditor) with the commands that you know.

sudo groupadd -g 2100 kr_dev
sudo useradd -m -u 2001 -g kr_dev -s /bin/bash -c "Ana Diaz" kr_ana
sudo groupadd -g 2102 kr_aud
sudo useradd -m -u 2002 -g kr_aud kr_ben

2. Run a command as another user

sudo -u kr_ana COMMAND runs one command as kr_ana. su - kr_ana opens a login shell as that user. id -un prints only the user name (-u asks for the user, -n for the name and not the number). sh -c 'TEXT' runs the text as a tiny script: here it goes to the home folder with cd, prints its path, makes an empty file with touch and lists it.

sudo -u kr_ana id -un
sudo -u kr_ana sh -c 'cd; pwd; touch hello; ls -l hello'
kr_ana
/home/kr_ana
-rw-r--r-- 1 kr_ana kr_dev 0 Nov 21 21:03 hello

3. Write a sudo rule

Now give kr_ana one administrator right: to read the status of the service cron, and nothing else. The rule lives in its own file in /etc/sudoers.d/. Never save a broken file: one syntax error stops sudo for everybody. visudo -cf FILE checks a file without installing it. install -m 440 -o root -g root copies it with the owner and the mode that sudo demands. The rule means: user kr_ana, on all hosts, as root, without a password, may run this exact command. For a group, write % before the name: %kr_ops ALL=(root) NOPASSWD: ... applies to every member of the group kr_ops. Without the %, visudo reads kr_ops as a user name, accepts the rule, and the group gets nothing.

rule.sudo

# kr_ana may read the status of the cron service as root
kr_ana ALL=(root) NOPASSWD: /usr/bin/systemctl --no-pager status cron

Save it as rule.sudo in your lab folder, and write a broken copy to compare.

cd ~/krlab/linux-admin
echo 'kr_ana ALL=(root NOPASSWD /usr/bin/systemctl --no-pager status cron' > bad.sudo
visudo -cf rule.sudo
visudo -cf bad.sudo
sudo install -m 440 -o root -g root rule.sudo /etc/sudoers.d/kr_ana
sudo -l -U kr_ana | tail -n 2
sudo -u kr_ana sudo -n /usr/bin/id
sudo -u kr_ana sudo -n /usr/bin/systemctl --no-pager status cron | head -n 2
rule.sudo: parsed OK
bad.sudo:1:18: syntax error
kr_ana ALL=(root NOPASSWD /usr/bin/systemctl --no-pager status cron
                 ^~~~~~~~~
User kr_ana may run the following commands on krbox:
    (root) NOPASSWD: /usr/bin/systemctl --no-pager status cron
sudo: a password is required
● cron.service - Regular background program processing daemon
     Loaded: loaded (/usr/lib/systemd/system/cron.service; enabled; preset: enabled)

The checker found the missing bracket in bad.sudo. sudo -l -U kr_ana lists what the rule allows for that user. The next command tries id as root through kr_ana, and sudo refuses (-n means "never ask for a password"), because id is not in the rule. The last command is the one command that the rule names: it works, and it prints the first two lines of the status (the same as in lesson 8). A rule with a full path and arguments allows only that. --no-pager is there on purpose: without it, systemctl status can open a pager, and a pager can start a shell as root. NOPASSWD is only here so that you can test the rule. For a real person, leave it out.

4. Give one user one file with an ACL

Groups are coarse: the auditor kr_ben should read one file, but must not join the team. An access control list (ACL) adds a rule for a named user or group on top of the normal bits. getfacl shows the list, and setfacl -m (modify) adds an entry: u:kr_ben:r means "user kr_ben, read".

sudo mkdir /srv/kr_share
echo budget | sudo tee /srv/kr_share/budget.txt
sudo chown root:kr_dev /srv/kr_share /srv/kr_share/budget.txt
sudo chmod 750 /srv/kr_share
sudo chmod 640 /srv/kr_share/budget.txt
sudo -u kr_ana cat /srv/kr_share/budget.txt
sudo -u kr_ben cat /srv/kr_share/budget.txt
budget
budget
cat: /srv/kr_share/budget.txt: Permission denied

The first budget is the output of tee, the second is read by kr_ana, and kr_ben is refused. He needs the right to enter the folder (x) and to read the file. The folder is closed to everyone but root and kr_dev, so use sudo to look at the file below. getfacl -p keeps the / at the start of the path in its header.

sudo setfacl -m u:kr_ben:x /srv/kr_share
sudo setfacl -m u:kr_ben:r /srv/kr_share/budget.txt
sudo getfacl -p /srv/kr_share/budget.txt
sudo ls -l /srv/kr_share/budget.txt
sudo -u kr_ben cat /srv/kr_share/budget.txt
# file: /srv/kr_share/budget.txt
# owner: root
# group: kr_dev
user::rw-
user:kr_ben:r--
group::r--
mask::r--
other::---

-rw-r-----+ 1 root kr_dev 7 Nov 21 21:03 /srv/kr_share/budget.txt
budget
Who may read budget.txt
kr_anabudget.txtkr_bensetfaclreads it, as group kr_devread refusedadds user:kr_ben:rreads it, now allowedkr_anabudget.txtkr_bensetfacl1234
  1. kr_ana → budget.txt: reads it, as group kr_dev
  2. kr_ben → budget.txt (blocked): read refused
  3. setfacl → budget.txt: adds user:kr_ben:r
  4. kr_ben → budget.txt: reads it, now allowed

The + after the bits in ls -l tells you that an ACL exists, so look at getfacl. The mask is the most that named users and groups can get. setfacl -x u:kr_ben FILE removes one entry, and setfacl -b FILE removes all of them.

5. Remove what you made

Clean up the lab. userdel may print mail spool ... not found: that is harmless.

sudo userdel -r kr_ana
sudo userdel -r kr_ben
sudo groupdel kr_dev
sudo groupdel kr_aud
sudo rm -f /etc/sudoers.d/kr_ana
sudo rm -rf /srv/kr_share

Your turn

Run this block once. It makes the accounts and the file of the task and is not the task. tee and sudo are those of the worked example.

sudo groupadd -g 2200 kr_ops
sudo useradd -m -u 2003 -g kr_ops -s /bin/bash kr_cleo
sudo useradd -m -u 2004 -g kr_ops -s /usr/sbin/nologin kr_dan
sudo mkdir -p /srv/kr_lab
echo "quarterly report" | sudo tee /srv/kr_lab/report.txt > /dev/null
sudo chown root:root /srv/kr_lab/report.txt
sudo chmod 600 /srv/kr_lab/report.txt

There are no steps here. Do this on your practice system:

  1. Write a sudo rule in the file /etc/sudoers.d/kr_ops that lets the group kr_ops run /usr/bin/journalctl --no-pager -u cron as root without a password. Check it with visudo before you install it.
  2. With an ACL, let kr_cleo read and write /srv/kr_lab/report.txt, and kr_dan read it only.

Answer from your own system. The page cannot read your system, so test the rule as well: run the allowed command once as kr_cleo with sudo -u kr_cleo sudo -n /usr/bin/journalctl --no-pager -u cron | head -n 3, and it must print log lines and not ask for a password.

Last, clean up: sudo userdel -r kr_cleo, sudo userdel -r kr_dan, sudo groupdel kr_ops, sudo rm /etc/sudoers.d/kr_ops and sudo rm -rf /srv/kr_lab.

Check yourself

Why do you write a sudo rule with visudo -cf and not with an editor on /etc/sudoers?

A syntax error in the sudoers file makes sudo refuse to work for everyone, and then you may have no way to become root. visudo locks the file and checks it before it saves. A file in /etc/sudoers.d/ checked with visudo -cf has the same safety, and can be added and removed with one file.

The rule allows "/usr/bin/journalctl --no-pager -u cron". Does it allow the same command with the extra option -n 1?

No. A rule with arguments allows exactly those arguments. That limits what the user can do. Take care with a rule for a program that can start a shell, such as an editor or a pager.

You write "kr_ops ALL=(root) NOPASSWD: /usr/bin/id" and visudo accepts it. Why does the group kr_ops get nothing?

Without %, visudo reads kr_ops as the name of a user. The syntax is right, so the checker accepts it, but no user has that name. Write %kr_ops for a group.

ls -l shows "-rw-r-----+" for a file. What does the "+" mean, and which command shows the rest?

An ACL exists on the file, with entries beyond the owner, group and other bits. getfacl FILE lists them.

Field notes

  • Least privilege: a sudo rule for one exact command is better than ALL. Do not allow a program that can run another program (a shell, vim, less, find), because it gives full root.
  • Keep a second terminal with a root shell open while you edit sudo rules, and test with sudo -l -U NAME before you close it.
  • Give rights to groups, not to people: %GROUP in a rule and setfacl -m g:GROUP:r for a file. When a person changes teams, you change a group only.
  • A + is all that ls -l shows of an ACL, so it is forgotten. Audit with getfacl -R FOLDER and remove what is not needed (setfacl -b).