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

URL: https://levelrail.com/learn/security/audit-a-linux-server/
Updated: 2026-11-27

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](https://levelrail.com/learn/security/harden-ssh-server-configuration/)) 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](https://levelrail.com/learn/server-basics/find-what-is-using-a-port/)) and [advanced ufw firewall rules](https://levelrail.com/learn/security/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](https://levelrail.com/learn/linux/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](https://levelrail.com/learn/linux/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](https://levelrail.com/learn/security/tune-fail2ban-jails-and-bans/)) 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](https://levelrail.com/learn/server-basics/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](https://levelrail.com/learn/security/unattended-security-updates-and-reboots/)) 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

| 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

```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
```

## Related lessons

- [Harden SSH server configuration](https://levelrail.com/learn/security/harden-ssh-server-configuration/)): reduce what the login logs have to show.
- [Tune fail2ban jails and bans](https://levelrail.com/learn/security/tune-fail2ban-jails-and-bans/)): act on repeated failed logins.
- [Users, groups and sudo](https://levelrail.com/learn/linux/users-groups-and-sudo/)): the model behind accounts and privileges.
- [Cron vs systemd timers](https://levelrail.com/learn/linux/cron-vs-systemd-timers/)): where scheduled jobs live.
- [Read logs with journalctl](https://levelrail.com/learn/server-basics/read-logs-with-journalctl/)): query SSH and system messages.

## Next

Continue with [Backups against ransomware](https://levelrail.com/learn/security/backups-against-ransomware/)).

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

- [ss man page](https://man7.org/linux/man-pages/man8/ss.8.html)
- [sudoers man page](https://man7.org/linux/man-pages/man5/sudoers.5.html)
- [Ubuntu Server docs, security](https://ubuntu.com/server/docs/security-introduction)
