Who Did That? Tracing sudo and Logins on a Linux Server
The config changed overnight and nobody did it. Everyone says so. The logs disagree. How to read sudo logs and login records, record whole sudo sessions, watch key files with auditd, and why the logs shouldn't only live on the box.
Silas Voss
4 min read
The nginx config changed at 2:14 a.m. The site's been redirecting to a page about timeshares since. You ask the team. Nobody did it. Everyone was asleep. One person was "at a wedding". There was no wedding.
[Cut to a courtroom. The prosecutor holds up a printout. "Exhibit A: sudo vim /etc/nginx/sites-enabled/default, 2:14 a.m., user dave." The jury gasps. Dave, in the dock, slowly lowers a slice of wedding cake.]
Every Linux server keeps a record of who did what as root. Most people only learn where it lives during an incident. Let's learn it now, over coffee, while nothing is on fire. Here's how to read sudo logs and login records, and how to make them good enough to stand up in court. Well, in a post-mortem.
sudo writes everything down
Every time someone runs sudo, it logs who ran it, from which terminal, in which directory, as which user, and the full command. On systemd distros, ask the journal:
journalctl -t sudo --since yesterday
A typical line:
dave : TTY=pts/1 ; PWD=/home/dave ; USER=root ; COMMAND=/usr/bin/vim /etc/nginx/sites-enabled/default
On Debian and Ubuntu systems that also write classic text logs, the same lines land in /var/log/auth.log. On Fedora and RHEL it's /var/log/secure.
sudo grep 'COMMAND=' /var/log/auth.log | tail -20
Failed attempts show up too, including "user NOT in sudoers" entries. Someone poking at sudo who shouldn't be is worth an alert all by itself.
Who was even logged in?
last -n 20
last -x reboot shutdown | head
who
last lists recent logins with the user, where they came from and how long they stayed. last -x adds reboots and shutdowns ("why did the server restart at 3 a.m.?"). who shows who's on right now. On newer distros, lastlog is being replaced by lastlog2, so if one command is missing, try the other.
For SSH specifically, the server logs every success and failure:
journalctl -u ssh --since "02:00" --until "02:30" | grep -E 'Accepted|Failed'
(The unit is ssh on Debian and Ubuntu, and sshd elsewhere.) "Accepted publickey for dave from 198.51.100.23" tells you which key got in, which matters if Dave insists he was at the wedding without his laptop.
The weakness: sudo bash
Here's what the logs don't show. If someone runs sudo -i or sudo bash, the log says exactly that, COMMAND=/bin/bash, and then nothing about what they did inside that root shell. It's like a security camera that records someone walking into the vault, then switches to a nature documentary.
Two fixes, used together.
Fix 1: record the whole sudo session
sudo can record everything typed and shown during a session. Edit the policy safely with visudo (never edit /etc/sudoers directly; visudo checks the syntax so you can't lock everyone out of root):
sudo visudo -f /etc/sudoers.d/io-logging
Defaults log_output
Defaults!/usr/bin/sudoreplay !log_output
Then play sessions back like a film:
sudo sudoreplay -l
sudo sudoreplay <session-id>
You see exactly what scrolled past on their screen, at the original speed. Dave's 2:14 a.m. vim session, frame by frame. Popcorn optional. (Recordings take disk space and can contain anything that was shown on screen, including secrets, so keep them protected and decide how long to keep them.)
Fix 2: watch the files that matter with auditd
The Linux audit system records access to files you choose, however it happened, sudo or not:
sudo apt install auditd
sudo auditctl -w /etc/nginx/ -p wa -k nginx-config
sudo auditctl -w /etc/sudoers -p wa -k sudoers
-w watches a path, -p wa means writes and attribute changes, and -k tags the events with a name. Find them later:
sudo ausearch -k nginx-config -i --start today
-i translates user IDs into names. Rules added with auditctl vanish at reboot, so put them in a file under /etc/audit/rules.d/ to keep them. auditd also records the login user (the "auid"), so even after sudo -i, the event says who originally logged in. The vault camera keeps recording.
The rule that matters most: ship the logs off the box
Anyone who gets root can edit or delete the local logs. The first thing a competent intruder does is tidy up after themselves. So send your logs somewhere else, to a central log server or a monitoring service, as they're written. Logs that leave the box at 2:14 a.m. can't be "lost" at 2:15.
What to do today
And Dave: we know there was no wedding.
Security
More like this
Security
What fail2ban Actually Stops (and the Things It Was Never Going To)
fail2ban is the bouncer who throws out anyone who gets their name wrong three times. Useful, and not a security strategy. How to set up its SSH jail, check who it has banned, and know what it can't protect you from.
Security
How to Read a CVE Without Panicking: CVSS, KEV and EPSS Explained
A 9.8 CVE drops on a Friday and your group chat catches fire. Before you cancel the weekend, learn how to read a CVE: what the score measures, what it doesn't, and the two lists that tell you whether anyone is actually attacking it.
Security
nftables From Zero: A Linux Firewall You Can Actually Read
iptables rules read like a ransom note made from magazine letters. An nftables firewall reads like a list. Build a default-deny ruleset in one file, test it with a five-minute dead man's switch, and avoid the Docker trap.