Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

Your auth.log is full of Failed password and Invalid user attempts

ErrorFailed password for invalid user admin from 198.51.100.23 port 40122 ssh2

Short answer

Any public SSH port gets password guesses within hours; the log lines alone are not a breach. First confirm there is no Accepted line from an address you do not know. Then turn password logins off and keep root out in sshd, which removes the risk, and add a fail2ban sshd jail only to cut the noise and the load.

  • Applies to OpenSSH 9.x, fail2ban 1.1, Ubuntu 24.04 and Debian 13 paths
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Separate noise from damage. Count the failures and list the busiest sources, then look for the one line that matters: a successful login. If an Accepted line shows an address or user you do not recognise, stop and treat the server as compromised instead of following this page.

    # Debian and Ubuntu with a log file; use journalctl -u ssh if /var/log/auth.log does not exist
    sudo grep -c "Failed password" /var/log/auth.log
    sudo grep -E "Failed password|Invalid user" /var/log/auth.log | grep -oE "from [0-9a-f.:]+" | sort | uniq -c | sort -rn | head
    sudo grep "Accepted" /var/log/auth.log | tail -n 20
    last -i | head -n 20
  2. 2Match what you see, then jump to its fix

    1. 1sshd -T shows passwordauthentication yes, or the failures include root and common user names such as admin or test.Password and root logins are still allowed, so guesses can succeedFix
    2. 2The same few addresses appear thousands of times, and fail2ban is not installed or no sshd jail is listed.Nothing blocks a source address that keeps retryingFix
    3. 3fail2ban-client status sshd shows Currently failed: 0 and Total failed: 0 while the log keeps filling.The jail is on but never counts a failureFix
    4. 4Connections time out or are refused for you only, and your IP appears in the jail's banned list.A legitimate user or your own address got bannedFix

What does the error look like?

  • Failed password for invalid user admin from 198.51.100.23 port 40122 ssh2
  • Invalid user ubnt from 198.51.100.23 port 40122
  • Failed password for root from 198.51.100.23 port 40130 ssh2
  • Connection closed by authenticating user root 198.51.100.23 port 40130 [preauth]

Cause 1 of 4

Password and root logins are still allowed, so guesses can succeed

1Check

Read the effective settings, not just the main file. Include files are read in name order and the first value found for a keyword wins, so a drop-in such as 50-cloud-init.conf on cloud images can keep passwords on after you edit sshd_config.

sudo sshd -T | grep -iE '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin|pubkeyauthentication|maxauthtries)'
ls /etc/ssh/sshd_config.d/

2Fix

Install your public key first and prove it works in a second terminal. Then turn off password and keyboard-interactive logins (PAM challenge-response can act like a password) and root login. Name the file so it sorts before any other drop-in that sets the same keywords.

sudo tee /etc/ssh/sshd_config.d/00-hardening.conf >/dev/null <<'EOF'
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
MaxAuthTries 3
EOF
sudo sshd -t && sudo systemctl reload ssh   # unit is sshd on RHEL-style systems

3Verify

sshd -T now shows the new values, a key login from a new terminal works, and a forced password attempt is refused.

sudo sshd -T | grep -iE '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin)'
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no deploy@203.0.113.10

Confirmed by man.openbsd.org

Did this fix it?

Cause 2 of 4

Nothing blocks a source address that keeps retrying

1Check

fail2ban reads the log, counts failures per address within a time window and asks the firewall to drop that address for a while. Check whether it is installed and which jails run.

systemctl is-active fail2ban
sudo fail2ban-client status

2Fix

Install fail2ban and enable the sshd jail in a local file; never edit jail.conf itself, as package updates replace it. Put your own address in ignoreip so a typo cannot lock you out. This lowers noise and CPU use; the key-only settings above are what actually protect the account.

sudo apt install fail2ban

sudo tee /etc/fail2ban/jail.d/sshd-local.conf >/dev/null <<'EOF'
[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 203.0.113.50

[sshd]
enabled = true
mode = aggressive
maxretry = 4
findtime = 10m
bantime = 1h
EOF
sudo fail2ban-client -t && sudo systemctl restart fail2ban

3Verify

The jail is listed, and its failed and banned counters rise as attempts arrive.

sudo fail2ban-client status sshd

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 4

The jail is on but never counts a failure

1Check

fail2ban must read the place where sshd actually writes. A minimal Debian or Ubuntu image may log only to the systemd journal, with no auth.log, and a jail pointed at the wrong source sees nothing. Test the filter against the log by hand.

ls -l /var/log/auth.log
sudo fail2ban-client get sshd logpath
sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf | tail -n 8

2Fix

If there is no auth.log, make the jail read the journal. If the file exists but the path is unusual, set logpath for the jail explicitly. Restart and watch the counters.

# /etc/fail2ban/jail.d/sshd-local.conf, in the [sshd] section
backend = systemd

# or, for a log file in a non-default place
# logpath = /var/log/auth.log

3Verify

Make a few failed logins from a machine that is not in ignoreip; the Total failed counter should rise.

sudo fail2ban-client status sshd

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

A legitimate user or your own address got banned

1Check

Look at the banned list from another route, such as the provider console, and check whether your address is in it.

sudo fail2ban-client status sshd

2Fix

Release the address and add it, or its network, to ignoreip so it is not counted again. Do not lower the protection to avoid the lock-out.

sudo fail2ban-client set sshd unbanip 203.0.113.50
# then add the address to ignoreip in the jail file and reload
sudo fail2ban-client reload

3Verify

The address no longer appears in the banned list, and ssh connects.

sudo fail2ban-client status sshd

Confirmed by manpages.debian.org

Did this fix it?

How do I stop it coming back?

  • Treat keys-only login as the protection and fail2ban as a noise filter; never rely on the ban list alone.
  • Check for Accepted lines from unknown addresses on a schedule, not just the failure counts.
  • Limit who may log in with AllowUsers or AllowGroups, and restrict port 22 in the firewall to known networks when you can.
  • Keep your own addresses in ignoreip and keep a console or second access path to the server.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Security

The browser blocks a script because of your Content Security Policy

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.

The page's Content-Security-Policy does not allow what it tried to run or load.

5 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.