Part 4 · Operations and troubleshooting · Lesson 09 · 55 minutes
Troubleshoot logins, permissions, sudo and services
- Find why an account cannot log in, why a file cannot be read and why a sudo rule does not work
- Read the status code and the journal of a service that does not start, and name the cause
- Repair each fault with the smallest change and prove it with the same test that failed
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
Four tickets arrive on the same morning. "Dana cannot log in." "Dana cannot read the Q3 report, although the file is readable by everyone." "The sudo rule for the ops account does nothing." "My service will not start." Each ticket has a cause that a few commands will show, and each has a trap that makes people guess. A script plants all of them on your practice system, and you work through them in the order of the questions: who, what, where.
Recall
A sudo rule lists "systemctl status cron". May the user also run "systemctl restart cron"?
No. A rule with arguments allows exactly those arguments (lesson 7 of Part 2). A different argument is a different command.
Which code does systemctl status show when systemd cannot run the program of ExecStart at all?
203/EXEC: the path is wrong or the file has no execute bit (lesson 8 of Part 2).
The idea
Every one of the four tickets is a question of identity and path: who is acting, and what may that identity reach? Ask the questions in this order.
- Who is it? → May they log in?: getent passwd NAME
- May they log in? → Can they reach it?: shell, lock, expiry
- Can they reach it? → May they use sudo?: namei -l PATH
- May they use sudo? → Does the unit start?: sudo -l -U NAME
| Question | Command | What to read |
|---|---|---|
| Does the account exist, with which shell and home? | getent passwd NAME | The last field is the shell: /usr/sbin/nologin refuses a login |
| Is the password locked or expired? | passwd -S NAME, chage -l NAME | Status L is locked, P has a password; the dates of expiry |
| Why can a file not be read? | namei -l PATH | Mode and owner of every part of the path: a user needs x on every directory above the file |
| What may a user run with sudo? | sudo -l -U NAME | The commands, with their full path and arguments |
| Is a sudo file valid? | visudo -cf FILE | parsed OK, or the line of the error |
| Why does a service fail? | systemctl status, journalctl -u NAME | The status code and the message |
Status codes of services that you meet often: 200/CHDIR (the working directory is missing), 203/EXEC (the program cannot be run, lesson 8 of Part 2) and 217/USER (the account of User= does not exist).
Worked example
The lab needs administrator rights. It makes two test accounts, kr_dana and kr_ops, a folder /srv/krdocs, a sudo rule in its own file and four service units. Run it only on a practice system. It gives kr_dana a random password that nobody knows, so that the account cannot be used from outside, and the script removes it again with down. On a newer release the wording of a message (for example "a password is required") can differ; read the code and the exit status. On the test machine each status shows a Drop-In line that is left out here.
1. Save the lab script and plant the faults
accesslab.sh
#!/bin/bash
# accesslab.sh - plants access and service faults on YOUR practice system. Run it with sudo.
# up make the test accounts, a folder with a blocked path, a sudo rule that does not match, four failing services
# down remove all of it
set -u
[ "$(id -u)" -eq 0 ] || { echo "run it with sudo"; exit 1; }
U=/etc/systemd/system
case "${1:-}" in
up)
# fault 1: an account whose shell is nologin
id kr_dana > /dev/null 2>&1 || useradd --create-home --shell /usr/sbin/nologin kr_dana
echo "kr_dana:$(head -c 18 /dev/urandom | base64)" | chpasswd
# fault 2: a readable file in a folder that others may not enter
mkdir -p /srv/krdocs/reports
echo "q3 numbers" > /srv/krdocs/reports/q3.txt
chmod 755 /srv/krdocs
chmod 700 /srv/krdocs/reports
chmod 644 /srv/krdocs/reports/q3.txt
# fault 3: a sudo rule for a command path that does not exist
id kr_ops > /dev/null 2>&1 || useradd --create-home --shell /bin/bash kr_ops
echo 'kr_ops ALL=(root) NOPASSWD: /usr/local/bin/systemctl status cron' > /etc/sudoers.d/kr-ops
chmod 440 /etc/sudoers.d/kr-ops
# faults 4 to 7: services that do not start
printf '[Unit]\nDescription=Runs as an account that does not exist\n\n[Service]\nUser=kr_ghost\nExecStart=/usr/bin/sleep 600\n' > $U/kr-user.service
printf '[Unit]\nDescription=Has a typo in a key\n\n[Service]\nExecStar=/usr/bin/sleep 600\n' > $U/kr-typo.service
printf '[Unit]\nDescription=Needs a unit that is not there\nRequires=kr-missing.service\nAfter=kr-missing.service\n\n[Service]\nExecStart=/usr/bin/sleep 600\n' > $U/kr-dep.service
printf '[Unit]\nDescription=Works in a folder that is not there\n\n[Service]\nWorkingDirectory=/srv/kr-missing\nExecStart=/usr/bin/sleep 600\n' > $U/kr-path.service
systemctl daemon-reload
echo "faults are planted"
;;
down)
for s in kr-user kr-typo kr-dep kr-path; do
systemctl stop $s 2> /dev/null
rm -f $U/$s.service
done
systemctl daemon-reload
systemctl reset-failed 2> /dev/null
rm -f /etc/sudoers.d/kr-ops
rm -rf /srv/krdocs
userdel -r kr_dana 2> /dev/null
userdel -r kr_ops 2> /dev/null
echo "faults are removed"
;;
*)
echo "usage: sudo bash accesslab.sh up|down"
exit 1
;;
esaccd ~/krlab/linux-operations
sudo bash accesslab.sh upfaults are planted2. Ticket 1: the account cannot log in
Reproduce first. su - NAME -c 'COMMAND' runs a command in a login shell of that account. Then ask the questions of the table: who, and why.
sudo su - kr_dana -c 'echo logged in'
getent passwd kr_dana
sudo passwd -S kr_danaThis account is currently not available.
kr_dana:x:1002:1002::/home/kr_dana:/usr/sbin/nologin
kr_dana P 2026-11-21 0 99999 7 -1The password status is P: a password is set, so the lock is not the cause. Read the account line:
nologin prints the message and ends. That is the cause. usermod -s SHELL NAME changes the shell.
sudo usermod -s /bin/bash kr_dana
sudo su - kr_dana -c 'echo logged in'logged inIf the status had been L (locked), sudo passwd -u NAME would be the repair, and chage -l NAME would show an account that has expired. Check all three before you guess.
3. Ticket 2: the file is readable, and cannot be read
The file has the mode 644, so every user may read it. Test as the user. sudo -u NAME COMMAND runs one command as another user. namei -l PATH walks the path and prints the mode and owner of each part. Run it with sudo: without it, namei stops at the folder that you may not enter, and shows only Permission denied for the rest.
sudo -u kr_dana cat /srv/krdocs/reports/q3.txt
sudo namei -l /srv/krdocs/reports/q3.txtcat: /srv/krdocs/reports/q3.txt: Permission denied
f: /srv/krdocs/reports/q3.txt
drwxr-xr-x root root /
drwxr-xr-x root root srv
drwxr-xr-x root root krdocs
drwx------ root root reports
-rw-r--r-- root root q3.txtThe mode of the file is fine. The folder reports has drwx------: only root may enter it, and a user needs x on every directory above a file. Open that one folder for others to enter and read, and test again.
sudo chmod 755 /srv/krdocs/reports
sudo -u kr_dana cat /srv/krdocs/reports/q3.txtq3 numbers4. Ticket 3: the sudo rule does nothing
sudo -l -U NAME lists what a user may run. sudo -n never asks for a password: it fails at once. Test as the user kr_ops, and compare the rule with the path of the real command.
sudo -u kr_ops sudo -n systemctl status cron
sudo -l -U kr_ops | tail -n 2
command -v systemctlsudo: a password is required
User kr_ops may run the following commands on krbox:
(root) NOPASSWD: /usr/local/bin/systemctl status cron
/usr/bin/systemctlThe rule names /usr/local/bin/systemctl, and the real program is /usr/bin/systemctl:
A rule matches a full path, so this one never matches. Fix the path in the rule file, and check the file with visudo -cf before you trust it (-c checks, -f names the file).
sudo sed -i 's#/usr/local/bin/systemctl#/usr/bin/systemctl#' /etc/sudoers.d/kr-ops
sudo visudo -cf /etc/sudoers.d/kr-ops
sudo -u kr_ops sudo -n systemctl status cron | head -n 1/etc/sudoers.d/kr-ops: parsed OK
● cron.service - Regular background program processing daemonAlways check the file first. A rule file with a syntax error can make sudo refuse every rule on the machine, and then nobody can repair it with sudo. visudo -cf finds the error before it harms. A rule with the colon missing after NOPASSWD shows:
/tmp/bad.sudo:1:58: syntax error
kr_ops ALL=(root) NOPASSWD /usr/bin/systemctl status cron
^Keep a second terminal with a root shell open when you edit sudo rules (lesson 7 of Part 2).
5. Ticket 4: the service does not start
The unit kr-user fails. Start it, and read the status and the journal in the order that lesson 8 of Part 2 taught.
sudo systemctl start kr-user
systemctl status kr-user --no-pager | grep -E "Active|Process"
sudo journalctl -u kr-user -n 3 --no-pager -o cat
getent passwd kr_ghost
echo "exit=$?" Active: failed (Result: exit-code) since Sat 2026-11-21 13:46:02 UTC; 16ms ago
Process: 4561 ExecStart=/usr/bin/sleep 600 (code=exited, status=217/USER)
kr-user.service: Failed to determine user credentials: No such process
kr-user.service: Main process exited, code=exited, status=217/USER
kr-user.service: Failed with result 'exit-code'.
exit=2The line about the credentials can be missing on a busy machine. The code 217/USER says that the account of User= could not be found, and getent confirms that kr_ghost does not exist (exit code 2). The repair is a correct account in the unit. Use kr_ops, which exists. Change it, make systemd read the file, clear the failed mark and start again.
sudo sed -i 's/kr_ghost/kr_ops/' /etc/systemd/system/kr-user.service
sudo systemctl daemon-reload
sudo systemctl reset-failed kr-user
sudo systemctl start kr-user
systemctl is-active kr-user
sudo systemctl stop kr-useractiveYour turn
Three more services are planted: kr-typo, kr-dep and kr-path. For each one, start it, read the status and the journal, name the cause and repair it with the smallest change. systemd-analyze verify FILE reads a unit file and prints its problems before you start it. Prove each repair with systemctl is-active. Answer from your own terminal.
Last, stop the three services and remove everything with sudo bash accesslab.sh down.
Check yourself
A file has mode 644, and a user still gets "Permission denied". What do you run?
namei -l PATH. A directory above the file may lack x for that user. The mode of the file only counts when every directory on the way can be entered.
passwd -S shows "P" and the user still cannot log in over SSH. Name two other causes.
The shell is nologin (or a missing program), the account has expired (chage -l), the home folder is missing or has wrong permissions, or the SSH server limits users (AllowUsers, lesson 4 of Part 3). The journal of the SSH service says which one.
Why check a sudoers file with visudo -cf before it goes live?
A syntax error can make sudo ignore all rules, and the administrator then cannot use sudo to repair it. The check finds the line of the error first.
systemctl start prints no error, but the service is not active. Where do you look?
At systemctl status for the code and at journalctl -u NAME for the messages. start only queues the job (lesson 8 of Part 2). systemd-analyze verify checks the unit file itself.
Field notes
- Log in as the account you debug, with
sudo -uorsu -, because your own rights hide the fault. A test as root proves nothing about a user. getfacl FILEshows access control lists. A file can be open to a user through an ACL thatls -lshows only as a+at the end of the mode (lesson 7 of Part 2).- Keep sudo rules in files of their own in
/etc/sudoers.d/, with mode0440, and name the full path and the exact arguments. A rule for a shell, an editor orALLis a root shell for that user. - A unit that the journal calls
bad unit file settingis read, not run: the typo is in the file.systemd-analyze verifyfinds it in one step, as doessystemd-analyze securityfor the exposure of a service that works. - Record in the ticket the command that failed, the message, the cause and the repair. The next person with "cannot log in" will search for it.