basforge

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

    RU

    Editing sshd or the firewall? Keep the old session open

    Before you touch sshd_config, AllowUsers, the port or ufw rules, keep the current SSH session open and test the login from a second window. Close the first only after the second has logged in and run sudo.

    Why it works

    Every SSH session has its own process. systemctl restart ssh restarts only the main sshd, the one that accepts new connections — sessions already open are left alone. A config error, a wrong AllowUsers, passwords closed while the key doesn’t work — all of that breaks new logins, and from the old session you put everything back.

    The firewall is the same: ufw enable warns Command may disrupt existing ssh connections, but an established connection keeps working: one of the first rules in /etc/ufw/before.rules lets packets of open connections through (--ctstate RELATED,ESTABLISHED -j ACCEPT). A new connection without a rule for port 22 won’t get in.

    What it looks like

    window 1 (keep open)                   window 2
    sudo nano /etc/ssh/sshd_config.d/…
    sudo sshd -t && sudo systemctl restart ssh
                                           ssh web1
                                           sudo -v          ← logged in, sudo works
    exit                                   …carry on

    If the second session can’t log in, fix it in the first: sudo rm your file or git checkout (Server configs in git), restart again, try again from the second window. If both sessions are lost, the provider console is what’s left (Provider console).

    For changes that can cut you off completely (firewall, network, port) there’s also auto-rollback: a script puts the old config back if nobody confirms within 2 minutes that login still works (An edit script with automatic rollback).