Audit a Linux server (ports, users, sudo, cron, logins, lynis)
A server audit is a read-only review of what listens on the network, who can log in and use sudo, what runs on a schedule and who tried to get in, so you spot anything unexpected.
- Updated Nov 27, 2026
- Markdown
Run a read-only audit of listening ports, accounts, sudo rights, scheduled jobs and login history, then scan with lynis.
- SSH access to an Ubuntu or Debian server with sudo
- A server you own or administer
An audit answers four questions about a server: what is reachable from the network, who can log in, what runs without anyone starting it, and who has tried to get in. Everything below reads state and changes nothing, so it is safe to run on a live machine you administer. Do this after hardening SSH and again every few months, because servers drift. Things get installed, users stay after people leave, and a test service gets left running.
How it works#
Linux keeps the facts in a few places. The kernel knows which sockets are open (ss). /etc/passwd, /etc/group and /etc/sudoers.d/ define accounts and privileges. cron and systemd timers hold scheduled jobs. Login records go to /var/log/wtmp (successes, shown by last), /var/log/btmp (failures, shown by lastb), the systemd journal and, on many systems, /var/log/auth.log. An audit walks through these places and compares what it finds with what you expect. The step that matters is the comparison: anything you cannot explain is the finding.
Step 1: List what listens on the network#
sudo ss -tulpnThe flags: t TCP, u UDP, l listening, p process, n numeric. Read the Local Address column:
127.0.0.1or[::1]means local only.0.0.0.0or[::]means every interface, including the public one, unless the firewall blocks it.- A specific private address means that network only.
For each public listener, ask whether you meant it. SSH, a web server and nothing else is a common healthy answer. A database on 0.0.0.0:5432 is a problem. Docker published ports appear as docker-proxy or are handled in firewall rules, so compare with docker ps. See find what is using a port and advanced ufw firewall rules.
Step 2: Review accounts#
grep -v -E 'nologin|false' /etc/passwd
getent group sudo
ls -l /etc/sudoers.d/
sudo ls -l /home/*/.ssh/authorized_keysThe first lists accounts that can have an interactive shell. Service accounts normally use nologin. For each real user, ask whether the person still needs access. The second shows who is in the sudo group. The third lists extra sudo rule files, and the fourth shows who has SSH keys. Open each key file and check that every key belongs to someone you know. Remove a user’s access with sudo deluser name or by deleting the key line. More background in users, groups and sudo.
Step 3: Check sudo rights#
sudo -l
sudo -l -U deploy
sudo cat /etc/sudoers.d/*sudo -l prints what the current user may run, and -U shows it for another user. Look for broad rules such as NOPASSWD: ALL. They are sometimes needed (cloud images often ship one), but each should be a conscious choice. Edit sudoers files only with sudo visudo -f /etc/sudoers.d/name, which checks syntax first. A syntax error in sudoers can lock you out of sudo, so keep another root-capable session open.
Step 4: Review scheduled jobs#
Cron jobs live in several places:
sudo crontab -l
sudo crontab -l -u deploy
ls -l /etc/cron.d/ /etc/cron.daily/ /etc/cron.hourly/
cat /etc/crontab
systemctl list-timers --allcrontab -l -u shows another user’s table. The /etc/cron.* directories hold system jobs. Timers are the systemd equivalent, see cron vs systemd timers. Unknown entries, especially ones that download and run scripts (curl ... | sh) or run from /tmp, are a red flag worth following up at once.
Step 5: Read login history#
last -n 10
sudo lastb | head
sudo journalctl -u ssh --since "24 hours ago" | grep -E 'Accepted|Failed' | tail -n 20
sudo grep 'Failed password' /var/log/auth.log | taillast shows who logged in and from where. Check each source address and time. A login you do not recognize means treat the server as compromised: change credentials, review the account, and consider rebuilding. lastb and the journal show failed attempts. A steady stream from scattered addresses is normal for any public SSH port, and what fail2ban is for. The unit may be called sshd on some distributions. On releases where auth.log is missing, the journal is the source. See read logs with journalctl.
Step 6: Run lynis for a broader scan#
Lynis is an open source auditing tool in the Ubuntu and Debian repositories:
sudo apt install lynis
sudo lynis audit systemIt checks many settings and ends with warnings and suggestions plus a hardening index. It reads configuration and does not change it. Treat each suggestion as something to evaluate. Some do not apply to your server, and some are worth doing. Save the report location it prints, and rerun after changes to see the effect. Pair it with unattended security updates so the basics stay patched.
Check it worked#
Write down what you found and what you changed, then re-run the same commands:
sudo ss -tulpn
getent group sudo
sudo lynis audit system --quickThe listener list should now contain only services you can name. The sudo group should contain only current admins. Keep the output from the first run to compare against next time.
Edge cases and gotchas#
- Containers hide listeners. A container’s internal ports do not show in the host
ss. Only published ports do. Checkdocker pstoo. - Run with sudo. Without sudo,
ss -phides processes owned by others andlastbis denied, which makes the audit look cleaner than it is. - Locked accounts. An account can have a shell but a locked password and still be reachable by SSH key. Check keys, not only passwords.
- Log rotation. Old failures move to rotated files such as
auth.log.1. Usezgrepfor compressed ones. - Time zone. Log times use the server’s zone. Compare with your own when looking for a login at a given moment.
- Audit is a snapshot. A clean result today says nothing about next month. Schedule it.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
lastb: cannot open /var/log/btmp: Permission denied | Not run as root | Use sudo lastb |
/var/log/auth.log: No such file or directory | Logging goes to the journal only | Use journalctl -u ssh |
ss shows no process names | Missing sudo | Run sudo ss -tulpn |
sudo: unable to resolve host | Hostname missing in /etc/hosts | Add the hostname to /etc/hosts |
crontab: no crontab for deploy | The user has no table | Normal, nothing scheduled there |
lynis: command not found | Not installed | sudo apt install lynis |
visudo: syntax error | Bad sudoers edit | Fix the line before saving, do not force it |
| Unknown user with a shell | Leftover or intrusion | Check its keys, cron and logins, then lock or remove |
Quick reference#
sudo ss -tulpn
grep -v -E 'nologin|false' /etc/passwd
getent group sudo
sudo -l -U <user>
sudo crontab -l -u <user>
systemctl list-timers --all
last -n 10
sudo lastb | head
sudo lynis audit systemRelated lessons#
- Harden SSH server configuration: reduce what the login logs have to show.
- Tune fail2ban jails and bans: act on repeated failed logins.
- Users, groups and sudo: the model behind accounts and privileges.
- Cron vs systemd timers: where scheduled jobs live.
- Read logs with journalctl: query SSH and system messages.
Next#
Continue with Backups against ransomware.
Try it: A quick server audit
Check listeners, accounts, sudo and logins on a server you administer. All steps are read-only.
Want a real machine? Free browser labs: Killercoda
Check what you learned
Frequently asked questions
How do I see what is listening on my server?
Run sudo ss -tulpn. It lists TCP and UDP listeners with the owning process. Anything bound to 0.0.0.0 or [::] is reachable from outside unless a firewall blocks it.
How do I list all users who can log in on Linux?
Look at /etc/passwd for accounts whose shell is not /usr/sbin/nologin or /bin/false, for example with grep -v -E 'nologin|false' /etc/passwd. Then check which of them have keys in authorized_keys.
How do I see failed login attempts?
Use sudo lastb for failed logins, sudo journalctl -u ssh for SSH messages, or grep 'Failed password' /var/log/auth.log where that file exists. Recent Ubuntu releases may log mostly to the journal.
Is lynis safe to run?
lynis audit system reads the configuration and reports warnings and suggestions. It does not change settings. Treat the suggestions as prompts to investigate, not as orders.
Sources
Keep going: guides and tutorials
- Manage Secrets Self-Hosted: Env Vars and Master Key Rotation Set env vars and encrypted secrets on a self-hosted server, apply changes safely, and rotate the master key with no downtime without locking yourself out.
- Install a Self-Hosted PaaS on Your Own Server in Five Minutes One installer download, one setup token, one sample app. The full Levelrail install on a fresh Linux server, with the checks and the fixes.