Every server on the Internet is found by automatic programs within hours, and from then on they try usernames and passwords over SSH all day. That is normal, and it is harmless as long as nobody gets in. This guide shows how to see who is logged in now, who logged in before, how many attempts failed and what to do about it.
The examples use Ubuntu 26.04 LTS. Run the commands as root, or put sudo in front of them.
1. Who Is Logged In Right Now
w
01:46:31 up 3 days, 11:02, 1 user, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 203.0.113.25 01:46 0.00s 0.01s 0.00s w
Each line is one session: the user, the address it comes from (FROM) and the time of the login. You should recognise every line; your own session is one of them.
2. Successful Logins
The SSH service writes every login to the system log. This shows the successful ones of the last 7 days:
journalctl -u ssh --since "7 days ago" -g Accepted --no-pager
Dec 01 09:12:04 server01 sshd-session[41220]: Accepted password for alex from 203.0.113.25 port 51844 ssh2 Dec 02 01:46:30 server01 sshd-session[57854]: Accepted publickey for root from 203.0.113.25 port 49188 ssh2: ED25519 SHA256:kJ4R0vDNme0AwPpxg0w8jZfAD6OVAvBRo0F5aJuAxyz
Each line tells you the time, the user, the address and how the login was made: with a password or with a key (publickey). For a key, the text after SHA256: is its fingerprint. To see which of your keys it is, list the fingerprints of the keys that may log in:
ssh-keygen -lf ~/.ssh/authorized_keys
256 SHA256:kJ4R0vDNme0AwPpxg0w8jZfAD6OVAvBRo0F5aJuAxyz alex@laptop (ED25519)
A login from an address you do not know, at a time when you were not working, is the thing to look for. Change the period as you like, for example --since "30 days ago" or --since today.
3. Failed Attempts
How many passwords were tried without success in the last 24 hours:
journalctl -u ssh --since "24 hours ago" -g "Failed password" --no-pager | wc -l
3842
A number in the thousands is usual for a server with a public address. Which addresses try the most:
journalctl -u ssh --since "24 hours ago" -g "Failed password" --no-pager | grep -oE "from [0-9a-f.:]+" | sort | uniq -c | sort -rn | head
1071 from 198.51.100.37
251 from 198.51.100.73
251 from 198.51.100.70
And which usernames they try:
journalctl -u ssh --since "24 hours ago" -g "Failed password" --no-pager | grep -oE "for (invalid user )?[^ ]+" | sort | uniq -c | sort -rn | head
1500 for root
313 for invalid user ubuntu
203 for invalid user admin
86 for invalid user user
51 for invalid user test
invalid user means that this user does not exist on your server. The attempts on root are the ones that count, because that user exists everywhere.
4. Commands Run With sudo
Every use of sudo is logged too, with the user, the folder and the command:
journalctl _COMM=sudo --since "7 days ago" --no-pager | grep COMMAND
Dec 02 01:47:07 server01 sudo[58244]: alex : TTY=pts/0 ; PWD=/home/alex ; USER=root ; COMMAND=/usr/bin/apt update
5. Optional: the Command last
On other systems you may know the commands last and lastb. A new Ubuntu 26.04 does not have them. If you want a short table of logins, install the package that brings last back:
apt install wtmpdb
last
root ssh 203.0.113.25 Wed Dec 2 01:47 - still logged in wtmpdb begins Wed Dec 2 01:47:31 2026
The table starts on the day you install the package; older logins are only in the log from step 2.
6. What to Do
Many Failed Attempts
You cannot stop strangers from trying, but you can make sure that trying is useless:
- Log in with a key and switch passwords off. Then no password can be guessed at all: Log In to Your Linux Server With an SSH Key.
- Block addresses that keep trying, or allow SSH only from your own address: Protect Your Linux Server With a Firewall and Fail2ban.
- Move SSH to another port. This does not make the server safer, but the log becomes much quieter: Change the SSH Port of Your Linux Server.
- Ask for a code from your phone in addition to the password: Two-Step Login for SSH: Password Plus a Code From Your Phone.
A Login You Do Not Recognise
- Check with
wwhether the session is still open. - Change the passwords of
rootand of your users at once:passwdfor yourself,passwd alexfor the user alex. - Open
~/.ssh/authorized_keysof every user and delete keys you do not know. - If the stranger was logged in as
rootor usedsudo(step 4), you cannot know what was changed. The safe way is to save your data and reinstall the server: Back Up Your Linux Server Before a Reinstall.
Good to Know
- More about the system log, and how long it is kept: Find Errors in the Logs of Your Linux Server: journalctl.
- Users and sudo: Add Users and Give Them sudo on Your Linux Server.
- The times in the log are in the time zone of the server: Time Zone and Clock of Your Linux Server.