Overview
Regularly reviewing who logged in, from where, and what they did helps you detect unauthorised access early. This article covers the built-in tools for auditing logins and active sessions on a Linux Pod.
Before you begin
- Root or sudo access.
- Log rotation keeps old entries for a limited time, so check regularly rather than relying on history from months ago.
Checking who has logged in
- Recent successful logins:
bash
last -a | head -20
- Failed login attempts:
bash
sudo lastb | head -20
- Currently logged-in users and their activity:
bash
who
w
- Authentication log (search for accepted and failed SSH logins):
bash
sudo grep "Accepted" /var/log/auth.log
sudo grep "Failed password" /var/log/auth.log
On RHEL-family systems the file is /var/log/secure. With systemd, you can query the journal:
bash
sudo journalctl -u ssh --since "7 days ago" | grep -i accepted
- Look for privilege escalation using sudo:
bash
sudo grep "sudo:" /var/log/auth.log
- List listening services and open connections:
bash
sudo ss -tulpn
Steps to act on findings
- Investigate any login from an unexpected country, IP or time.
- Confirm the account should exist; disable or delete unknown users:
bash
sudo passwd -l <user>
sudo userdel -r <user>
- Remove unrecognised SSH keys from
~/.ssh/authorized_keys. - Rotate passwords and keys if you suspect compromise.
- Add Fail2ban and key-only SSH to prevent repeats.
Troubleshooting
- Problem:
lastbshows nothing. Solution: On some distributions failed logins are only in the auth log; read/var/log/auth.logor/var/log/secureinstead.
Related articles
- [Hardening SSH]
- [Installing and configuring Fail2ban]
- [Incident response: what to do if you are compromised]
