How to Audit Who Logged Into Your VPS and Spot Suspicious Logins

Every login to your VPS leaves a trace. The problem is that most owners never look at those traces until something breaks. If you audit who logged into your VPS on a regular schedule, you catch a stranger’s session while it is still one login instead of a full breach. This guide covers the commands on Ubuntu and Debian: the login history, the SSH auth log, and the checks that separate your own session from an intruder’s.

Audit Who Logged Into Your VPS With last, lastb, and w

The first command to check ssh logins Linux stores for you is last. It reads the file /var/log/wtmp and lists successful logins, newest first, with the username, source IP or hostname, login time, and session length. Your vps login history is the first place to look when something feels off.

last -20
last -i -20
last -f /var/log/wtmp.1 | head -30

The -i flag shows IP addresses instead of hostnames, which makes it easier to spot an address you do not recognize. The third command reads the rotated log from last month, because wtmp only covers recent weeks. Look for logins at hours you were asleep, logins from countries you never visit, and any login as root if you thought root login was disabled.

Failed attempts live in a separate file, /var/log/btmp, and the command is lastb. Run it as root or with sudo. If it prints nothing, either nobody has failed a login or the file is empty, which is normal on a fresh server. On an internet-facing server, expect plenty of failed root logins from bots. That alone is not a breach. It becomes one when a failed streak is followed by a success from the same IP.

For a live view, w and who show who is logged in right now and what they are running.

Read the SSH Auth Log for the Full Story

The last command gives you the summary. The auth log gives you the detail. On Ubuntu and Debian it is /var/log/auth.log; on RHEL-based systems it is /var/log/secure. Every SSH connection attempt lands here, accepted or rejected, with the method used.

grep "Accepted" /var/log/auth.log
grep "Accepted publickey" /var/log/auth.log
grep "Failed password" /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -nr | head -20
journalctl -u ssh --since "7 days ago" | grep Accepted

The first two commands list every successful login and whether it used a password or an SSH key. A line saying Accepted password for root on a server where you only ever log in with a key is a red flag on its own. The third command counts failed password attempts per IP so you can see which addresses are hammering you. The last command does the same job through systemd’s journal, which is handy when the plain log file has already rotated away; use zgrep "Accepted" /var/log/auth.log*.gz to search the compressed rotated logs.

If the log for the day in question is missing or has a gap, treat that as evidence too. Attackers sometimes delete log lines after breaking in.

How to Spot a Suspicious Login

One suspicious ssh login at 3 a.m. from an IP you have never seen is worth investigating, even if nothing else looks wrong. Work through this checklist against every login you cannot explain:

  • Unknown IP address. Compare the source IP against your own addresses and your team’s. A quick lookup shows the country and hosting provider. A login from a datacenter in a country where you have no users is a strong signal.
  • Odd hours. Logins in the middle of the night, on a weekend, or during a holiday when nobody on your team works are worth a second look.
  • Wrong method. Password logins after you switched the server to key-only access mean someone either has a password that still works or the setting never took effect.
  • Root logins. If you disabled root login in sshd_config, any successful root login means the setting is not active.
  • Failed attempts, then success. Hundreds of failed attempts from one IP followed by a single success is the classic shape of a guessed or leaked password.

No single item on this list proves a breach. Two or three together usually do. When in doubt, assume the login was hostile: overreacting costs an afternoon of password changes, underreacting can cost you the server.

Check for New SSH Keys and Users You Did Not Create

A login audit is incomplete if you only check the logs. An attacker who gets in once usually leaves a way back in, and the most common one is an extra SSH key added to an authorized_keys file. Compare every key on the server against the keys you actually issued.

cat /root/.ssh/authorized_keys
cat /home/*/.ssh/authorized_keys
awk -F: '$3 >= 1000 {print $1}' /etc/passwd
getent group sudo
lastlog

Read each key line and ask whether you know whose it is. A key with a comment you do not recognize, or one added on a date when nobody on your team touched the server, should be removed. The awk command lists regular user accounts; any name you do not recognize needs explaining. getent group sudo shows who can become root, and lastlog shows the most recent login per account, which makes dormant accounts that suddenly became active easy to spot.

While you are here, check for scheduled jobs and startup entries you did not add. A new cron job or systemd service can phone home and survive a password change. Check crontab -l for every user, the files in /etc/cron.d/, and systemctl list-timers --all. Disable anything you cannot explain rather than deleting it, so you keep a copy to examine later.

What to Do When You Find a Suspicious Login

Finding a strange login is the start of the work, not the end. Move in this order so you do not destroy evidence or tip off the intruder before you have locked them out.

  1. Save the evidence first. Copy /var/log/auth.log, /var/log/wtmp, and the journal to a safe place before you change anything. If you need help later, these files are the record of what happened.
  2. End the session. If the user is still logged in, kill their processes with pkill -KILL -u username and check w again to confirm they are gone.
  3. Remove the way in. Delete the unknown SSH key, disable or delete the unknown account, and change the passwords of every account that shares a password with the compromised one.
  4. Block the source. Add the attacker’s IP to your firewall and to fail2ban so it cannot try again. If the login came through a password, switch the server to key-only login and disable password authentication entirely.
  5. Look for persistence. Check cron jobs, systemd services, and startup scripts for anything added around the time of the login. If you find a backdoor, do not trust a cleanup of the same system. If the signs point to a deeper compromise, follow the checklist in our guide on how to detect a compromised server and consider rebuilding from a known-good backup.
  6. Rotate everything the account could reach. API keys, database passwords, and deployment tokens that lived on that server should be treated as exposed until you replace them.

If the login turns out to be yours, a teammate’s, or a monitoring script you forgot about, note it down. A short list of known IPs and service accounts makes the next audit much faster.

Make the Next Audit Take Five Minutes

Audit is a habit, not a one-off rescue. A weekly five-minute check catches intruders early, and a few setup changes make each check almost automatic. The default log rotation on Ubuntu keeps about four weeks of auth logs, which is enough for a weekly routine. If you want months of history, raise the rotation count in /etc/logrotate.d/rsyslog and send a copy of the logs off the server so an attacker cannot erase them.

Then reduce the noise. Most of what fills your auth log is bot traffic trying the root password, and setting up fail2ban for SSH bans those IPs after a few failures so your logs stay readable. Pair that with key-only login by following our guide on how to harden SSH on Ubuntu 24.04, and add a second factor with our walkthrough for two-factor authentication for SSH so a stolen key alone is not enough to get in. If you are weighing the options, our comparison of fail2ban, SSH keys, and port knocking lays out what each method actually stops.

Finally, put the audit on a schedule. A small script that emails you the output of last and the count of failed logins each Monday morning turns this from a chore you forget into a report that arrives on its own. The first time it flags a login you do not recognize, you will be glad the habit was already in place.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.