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 AlmaLinux 10 and Rocky Linux 9. Run the commands as root, or put sudo in front of them.
1. Who Is Logged In Right Now
w -f
02:11:48 up 3 days, 1:02, 1 user, load average: 0.33, 0.21, 0.10 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 203.0.113.25 02:11 0.00s 0.00s 0.03s w -f
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. Without -f, the command leaves the address out.
2. Successful Logins
The short table of the last logins, with the user, the address and the time:
last -n 10
root pts/0 203.0.113.25 Wed Dec 2 02:10 - 02:10 (00:00) alex pts/0 203.0.113.25 Tue Dec 1 09:12 - 09:40 (00:28) reboot system boot 6.12.0-264.16.1. Sat Nov 28 14:44 still running
The system log has more details. It shows how each login was made, with a password or with a key (publickey):
journalctl -u sshd --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 02:10:24 server01 sshd-session[57854]: Accepted publickey for root from 203.0.113.25 port 49188 ssh2: ED25519 SHA256:fs91ds0KPGDbQZJrgaKlFqqxV9ToFsCviTpAJD8Oxyz
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:fs91ds0KPGDbQZJrgaKlFqqxV9ToFsCviTpAJD8Oxyz 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.
3. Failed Attempts
The last failed logins, with the name that was tried and the address it came from:
lastb -n 10
admin ssh:notty 198.51.100.37 Wed Dec 2 02:07 - 02:07 (00:00) test ssh:notty 198.51.100.73 Wed Dec 2 02:07 - 02:07 (00:00)
How many passwords were tried without success in the last 24 hours:
journalctl -u sshd --since "24 hours ago" -g "Failed password" --no-pager | wc -l
On a server with a public address, a number in the thousands is usual. Which addresses try the most:
journalctl -u sshd --since "24 hours ago" -g "Failed password" --no-pager | grep -oE "from [0-9a-f.:]+" | sort | uniq -c | sort -rn | head
And which usernames they try:
journalctl -u sshd --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 admin
86 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. The same lines are also kept in the file /var/log/secure.
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 02:07:41 server01 sudo[1351]: alex : TTY=pts/0 ; PWD=/home/alex ; USER=root ; COMMAND=/usr/bin/dnf upgrade
5. 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. On AlmaLinux and Rocky Linux the SSH service is called
sshd. - Block addresses that keep trying, or allow SSH only from your own address: Protect Your AlmaLinux or Rocky Linux Server With firewalld and Fail2ban.
- Move SSH to another port. This does not make the server safer, but the log becomes much quieter: Change the SSH Port on AlmaLinux and Rocky Linux.
A Login You Do Not Recognise
- Check with
w -fwhether 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
- Change the period as you like, for example
--since "30 days ago"or--since today. - The last login of one user:
lastlog -u alex. - First steps on a new server: AlmaLinux and Rocky Linux Server: Quick Start Guide.