basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    ConfigLinuxSSH

    sshd_config: key-only login, no root, no passwords

    Your own file in /etc/ssh/sshd_config.d/ that turns off password and root logins and cuts the time bots get for guessing. The file name matters: sshd uses the first value it finds, not the last, and 50-cloud-init.conf can silently bring passwords back.

    How sshd reads its settings

    On Ubuntu 22.04 and later, the main file /etc/ssh/sshd_config includes a directory on its first line:

    Include /etc/ssh/sshd_config.d/*.conf

    Files from the directory are read in alphabetical order, then the rest of the main file. For most settings sshd uses the first value it finds. Two rules follow:

    • your settings go in a separate file in sshd_config.d/, and the main file stays untouched: on a package upgrade apt won’t ask whose version to keep;
    • your file’s name must sort before the others. Servers built from a provider’s image almost always have 50-cloud-init.conf there, often with PasswordAuthentication yes. Ours is 00-hardening.conf.

    sudo sshd -T shows what ends up in effect: it prints every setting with all files applied. It’s the only sure way to know — reading the files isn’t enough.

    The file, block by block

    /etc/ssh/sshd_config.d/00-hardening.conf
    PasswordAuthentication no
    KbdInteractiveAuthentication no
    PubkeyAuthentication yes
    Key part

    The heart of the file. With no password, guessing is useless: a bot is refused before any password check. KbdInteractiveAuthentication is a second path to a password through PAM; Ubuntu already turns it off in the main file, but write it out so you don’t depend on other files. Apply only once key login is tested (SSH keys).

    default:PasswordAuthentication yesKbdInteractiveAuthentication yes (no on Ubuntu)PubkeyAuthentication yes

    PermitRootLogin no

    Root doesn’t log in at all, not even with a key. Work as your own user with sudo: the log shows who did what, and a leaked key doesn’t hand over everything at once. The default prohibit-password lets root in with a key.

    default prohibit-password

    AllowUsers example-user

    Only the listed users get in. System users and accidental ones (ubuntu from the image, a user created by a package) won’t log in even if they end up with a key. A typo here locks everyone out — test from a second window (Keep the old session open).

    default all users

    MaxAuthTries 3
    LoginGraceTime 30

    Less time and fewer tries per connection. MaxAuthTries 3: after three wrong keys the connection drops — if the client’s agent holds more keys, you need IdentitiesOnly yes (~/.ssh/config). LoginGraceTime 30: 30 seconds to log in instead of two minutes, so half-open bot connections don’t hang around.

    default:MaxAuthTries 6LoginGraceTime 120

    X11Forwarding no
    AllowAgentForwarding no

    X11 forwarding is pointless on a server without graphics; Ubuntu’s main file turns it on. The server allows agent forwarding by default — turn it off: with it on, root on the server can use your agent while the session is open (ssh-agent).

    default:X11Forwarding no (yes on Ubuntu)AllowAgentForwarding yes

    ClientAliveInterval 300
    ClientAliveCountMax 2

    Abandoned sessions close themselves: every 5 minutes the server checks on the client and drops the connection after two missed replies. Idle sessions stay up — the client answers as long as it’s alive.

    default:ClientAliveInterval 0 — don’t checkClientAliveCountMax 3

    AllowTcpForwarding no

    Leave port forwarding on: tunnels to closed admin panels depend on it (SSH tunnels). Turn it off if outside users log in to the server.

    default yes

    Port 48731

    Change the port only if log noise gets in the way (Port 22). On Ubuntu 24.04 and 26.04 this needs daemon-reload and an ssh.socket restart — see below. Open the new port in the firewall before restarting.

    default 22

    What’s not here and why

    Setting Why it’s left out
    Protocol 2 protocol 1 has been gone since OpenSSH 7.4; sshd ignores the line
    UsePAM no Ubuntu relies on PAM for session accounting and motd; password login is closed anyway
    Ciphers, KexAlgorithms, MACs the default lists in OpenSSH 9.6 and 10.2 already exclude weak algorithms; hand-made lists go stale faster than anyone updates them
    PerSourcePenalties exists since OpenSSH 9.8 and is on by default: the server itself pushes back an address that keeps failing to log in. Works out of the box on Ubuntu 26.04; 24.04 (9.6) doesn’t have it — there CrowdSec stops the guessing (CrowdSec against SSH brute force)
    AllowGroups for when many people log in: create an ssh-users group, and a new person gets access with usermod -aG, no config edit. For one or two people AllowUsers is enough

    Apply it

    1. Keep the current session open

      Do every step in one session and test in a second, new one. A running session survives an sshd restart and any config error (Keep the old session open).

    2. Check the syntax

      sudo sshd -t && echo ok
      # ok

      On an error, sshd names the file and the line: /etc/ssh/sshd_config.d/00-hardening.conf line 7: Bad configuration option: …. No ok — don’t go further.

    3. See what will take effect

      sudo sshd -T | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin|allowusers|maxauthtries) '
      # permitrootlogin no
      # maxauthtries 3
      # passwordauthentication no
      # kbdinteractiveauthentication no
      # allowusers example-user

      If you see passwordauthentication yes, a file read before yours sets it.

    4. Restart sshd

      sudo systemctl restart ssh

      On Ubuntu the service is called ssh, not sshd. A restart doesn’t touch open sessions: each has its own process — in ps it’s sshd: example-user@pts/0, and since OpenSSH 9.8, sshd-session.

      If you changed Port or ListenAddress, on Ubuntu 24.04 and 26.04 do this instead:

      sudo systemctl daemon-reload
      sudo systemctl restart ssh.socket

      The port is held by ssh.socket (systemd), and sshd starts on the first connection. The systemd generator reads Port from sshd_config on daemon-reload; without it the old port stays. If systemctl is-enabled ssh.socket says disabled, the provider has switched back to the classic startup — then restart ssh is enough.

    5. Log in from a second window

      A new terminal window, a new session — and only after it works, close the first one. The checks are below.

    If it doesn’t work

    The file says PasswordAuthentication no, but the server still asks for a password

    Cause
    A file earlier in alphabetical order has PasswordAuthentication yes — most often 50-cloud-init.conf. In sshd the first value wins, so a line in 99-…conf or at the end of sshd_config changes nothing.
    Fix
    sudo grep -ri passwordauthentication /etc/ssh/sshd_config /etc/ssh/sshd_config.d/ shows every place. Name your file so it comes first (00-…), and check the effective value with sudo sshd -T | grep -i passwordauth.

    After restarting sshd, new sessions can’t get in: Connection refused

    Cause
    A config error: sshd didn’t start. The current session survives — it has its own process.
    Fix
    In the open session: sudo sshd -t names the line; fix it, then sudo systemctl restart ssh. No session left — the provider console (Provider console).

    sudo sshd -t says Missing privilege separation directory: /run/sshd

    Cause
    sshd is started through ssh.socket and hasn’t run yet, so /run/sshd doesn’t exist: ssh.service creates it on start (RuntimeDirectory=sshd) and removes it on stop.
    Fix
    The check itself has nothing to do with that directory: sudo mkdir -p /run/sshd && sudo sshd -t.

    Changed Port, but sshd still listens on 22

    Cause
    On Ubuntu 24.04 the port is held by ssh.socket, i.e. systemd. It reads Port from the config only on daemon-reload.
    Fix
    sudo systemctl daemon-reload && sudo systemctl restart ssh.socket; check with ss -tlnp | grep -E ':(22|48731) '.

    Root can still log in with a key

    Cause
    The default is PermitRootLogin prohibit-password: root’s password is closed, but keys are allowed.
    Fix
    Set PermitRootLogin no, but only after your own sudo user has logged in and run sudo -v (First login to a server).

    Added AllowUsers — and nobody can log in

    Cause
    A typo in the name, or you left yourself out: with AllowUsers, only the listed users get in.
    Fix
    Fix the line from the open session; test a login from a second window before closing the first (Keep the old session open).

    How to check

    From your computer, in the second window. Passwords should no longer be accepted — force the client to try only a password:

    ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password [email protected]
    # [email protected]: Permission denied (publickey).

    The parentheses list the login methods the server still offers. Only publickey means passwords are closed.

    Root can’t log in, not even with a key:

    ssh [email protected]
    # [email protected]: Permission denied (publickey).

    Your own login works:

    ssh [email protected] 'echo ok'
    # ok

    What the bots see — on the server, an hour after applying. Even with ssh.socket, the sshd log goes under ssh.service, so filter with -u ssh:

    sudo journalctl -u ssh --since -1h | grep -c 'Failed password'
    # 0