Monitor and audit SSH connection attempts
Learn how to monitor SSH activity and audit user login history for signs of unauthorized access.
Lesson Content
SSH connection attempts are recorded in /var/log/auth.log. Open this file to review authentication activity, including the username used, the source IP address, the connection port, and other details:
cat /var/log/auth.logFor a continually updating view while new attempts are recorded, use tail:
tail -f /var/log/auth.logThe lastlog command shows the most recent login attempt for every user on the server:
lastlogTo check the most recent login for one specific user, pass the username with the -u option:
lastlog -u usernameYou can combine monitoring of /var/log/auth.log with lastlog to audit SSH activity. The log shows the details of each authentication attempt, while lastlog gives you a quick summary of the latest login for each account.
If you suspect that someone accessed the server through a particular user account, inspect that user’s .bash_history file. It is stored in the user’s home directory and contains commands executed from a Bash prompt:
cat /home/username/.bash_historyReplace username with the account you are investigating. Review the commands for unexpected activity, such as account changes, modified files, downloaded tools, or attempts to hide evidence. This gives you another source of information when you are scrutinizing a potentially unauthorized SSH connection.
Join the discussion!
Comments
Want to comment on this lesson?
Log in to leave comments.