Chapter 10 · Lesson 9 of 10
Server security

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.

By the end you will

Run a read-only audit of listening ports, accounts, sudo rights, scheduled jobs and login history, then scan with lynis.

Time
30 min
Level
Intermediate
Check
4 questions
You need
  • 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#

bash
sudo ss -tulpn

The flags: t TCP, u UDP, l listening, p process, n numeric. Read the Local Address column:

  • 127.0.0.1 or [::1] means local only.
  • 0.0.0.0 or [::] 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#

bash
grep -v -E 'nologin|false' /etc/passwd
getent group sudo
ls -l /etc/sudoers.d/
sudo ls -l /home/*/.ssh/authorized_keys

The 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#

bash
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:

bash
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 --all

crontab -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#

bash
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 | tail

last 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:

bash
sudo apt install lynis
sudo lynis audit system

It 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:

bash
sudo ss -tulpn
getent group sudo
sudo lynis audit system --quick

The 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. Check docker ps too.
  • Run with sudo. Without sudo, ss -p hides processes owned by others and lastb is 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. Use zgrep for 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#

SymptomLikely causeFix
lastb: cannot open /var/log/btmp: Permission deniedNot run as rootUse sudo lastb
/var/log/auth.log: No such file or directoryLogging goes to the journal onlyUse journalctl -u ssh
ss shows no process namesMissing sudoRun sudo ss -tulpn
sudo: unable to resolve hostHostname missing in /etc/hostsAdd the hostname to /etc/hosts
crontab: no crontab for deployThe user has no tableNormal, nothing scheduled there
lynis: command not foundNot installedsudo apt install lynis
visudo: syntax errorBad sudoers editFix the line before saving, do not force it
Unknown user with a shellLeftover or intrusionCheck its keys, cron and logins, then lock or remove

Quick reference#

bash
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 system

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.

practice, nothing runs on your machine

Want a real machine? Free browser labs: Killercoda

Check what you learned4 questions
1Which command shows listening ports and the process behind each one?
2A service listens on 127.0.0.1:5432. Is it reachable from the internet?
3Where do extra sudo rules often live on Ubuntu?
4Which command shows recent failed login attempts?

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

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios