You have edited `/etc/ssh/sshd_config` on a remote Linux server you can only reach over SSH. What do you do before and after restarting sshd so a mistake in that file does not lock you out?
answer
- you are holding the tool you are editing
- parse-check before applying
- the open session is the lifeline
- prove it from a second connection
- dead-man's switch for risky edits
basics
~20 sParse-check the file first with sshd -t, keep your current session open, then reload or restart the unit and prove the change from a brand-new second session before closing the first. Have console access or a timed automatic rollback as the backup.
solid answer
~50 sThree habits, in order. First, validate: `sshd -t` parses the file and exits non-zero with the offending line on a syntax error, and `sshd -T` dumps the effective configuration so you can see what a directive actually resolved to. Second, never close the session you are holding — existing SSH sessions survive a reload and a restart, because each one is a separate child process and the shipped unit stops only the main daemon rather than the whole control group. That surviving session is your lifeline. Third, verify by opening a *new* connection from another terminal; only when that succeeds do you release the original. `systemctl reload` (a SIGHUP that makes sshd re-read the file) is preferable to `restart` for a config change. For genuinely risky edits, arm a timed rollback with `systemd-run --on-active=5m` to restore the backup, and know where your out-of-band console is.
code
bash · 4 linescp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sshd -t || echo 'config rejected, not applying'
systemd-run --on-active=5m /bin/sh -c 'cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config && systemctl reload ssh'
systemctl reload sshgo deeper
Know that sshd -t checks the config before you apply it, and that you should keep your current SSH session open until a brand-new connection has proved the change works.
Explain reload versus restart for this daemon, why established sessions survive either one, and how sshd -T shows the effective value when a duplicate directive earlier in the file wins.
Demonstrate the full remote-change discipline: backup, parse-check, timed automatic rollback, apply, verify in a second session, then release the first — and name your out-of-band console path.
Own the general rule for remote self-modifying changes — SSH, firewall, routing — that every one needs a verification step from a fresh connection and a rollback that fires without a human, plus periodically tested console access across the fleet.
## The failure this protects against A typo in `sshd_config`, or a directive that is valid but wrong, can leave sshd refusing to start or refusing *you* specifically. On a machine whose only access path is SSH, that is a trip to the console — or, on hardware without one, a physical trip. The whole procedure exists because the tool you are changing is the tool you are holding. ## Before: validate the file ``` sshd -t ``` The extended-test flag parses the configuration and checks the host keys, printing the file and line number of the first problem and exiting non-zero if there is one. It is cheap and it catches the whole class of typo errors. On most distributions `sshd` is not on a normal `PATH`, so run it as `/usr/sbin/sshd -t`. ``` sshd -T | grep -i clientalive ``` The uppercase form dumps the **effective** configuration — every directive with the value actually in force, defaults included. This is how you answer "did my edit take effect, or is it being overridden by an earlier directive or a drop-in file?" without restarting anything. OpenSSH takes the *first* value seen for most keywords, so a duplicate earlier in the file silently wins; `-T` is what makes that visible. Note that conditional blocks are only evaluated when you supply connection details, so read the dump with that in mind. Take a backup before editing at all — `cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak` — because your rollback plan needs something to roll back to. ## Applying it: reload rather than restart For a configuration change, `systemctl reload` is the right verb: the unit's reload action sends `SIGHUP` to the main daemon, which re-executes itself and re-reads the file. The unit name differs by distribution — `ssh.service` on Debian and Ubuntu, `sshd.service` on RHEL and its derivatives — and getting that wrong is its own small trap. The reassuring part, and the thing interviewers like to hear you know: **your existing session survives**. Each accepted connection is handled by a forked child process, and the shipped unit file stops only the main daemon rather than every process in its control group, so restarting the listener does not evict logged-in users. What a restart *does* do is close the door behind it — if the new configuration is broken and the daemon fails to come back, no new connection can be made, and you are down to the session you already hold. Which is exactly why you do not close it. ## After: prove it from a new connection Open a second terminal and connect fresh. If that succeeds, the change is safe and you can let the original go. If it fails, you still have a live root-capable shell to restore the backup and reload again. This ordering — hold, change, verify in a new session, release — is the single most valuable habit in the answer, and it generalises to firewall rules and network configuration on remote hosts. Check the daemon's own view too: ``` systemctl status ssh journalctl -u ssh --since '5 min ago' ``` A daemon that failed to start says so plainly in its log, usually naming the directive it did not like. ## The safety net for genuinely risky edits When you are changing something that could plausibly refuse your own login — authentication methods, the listening address, access lists — arm an automatic rollback before you apply it: ``` systemd-run --on-active=5m /bin/sh -c \ 'cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config && systemctl reload ssh' ``` That schedules a one-shot timer five minutes out. If your new session works, cancel the timer; if it does not, the machine repairs itself while you wait. It is the remote-hands equivalent of a dead-man's switch, and it is the difference between a five-minute inconvenience and a data-centre ticket. Beyond that, know your out-of-band path before you need it: an IPMI or iDRAC console, a cloud provider's serial console, or a hypervisor console. On a fleet, that access should be tested periodically, because discovering that the console credentials are wrong at the moment you are locked out is a bad time to find out. ## What a weak answer looks like "Just restart it and see" — with no validation, no held session, and no console. It works most of the time, which is precisely why the habit never forms, and then one afternoon it does not.
- Does restarting sshd disconnect the users currently logged in?No. Each session runs in its own forked child process, and the shipped unit is configured to stop only the main daemon rather than every process in its control group, so established sessions continue. What stops is the listener, so if the daemon fails to come back you simply cannot make *new* connections — which is why you keep the session you already have.
- You edited a directive but `sshd -T` shows the old value. What happened?Something earlier won. OpenSSH takes the first occurrence of most keywords, so a duplicate higher up the file, or a drop-in included before your edit, silently takes precedence. `sshd -T` reporting the effective value is exactly the tool for catching that — it saves you from concluding the daemon ignored a reload.
- What is the difference in practice between `sshd -t` and `sshd -T` when checking a change?`-t` answers "will it parse and can the daemon start" — syntax and host keys, non-zero exit on failure. `-T` answers "what will actually be in force", printing every directive with its resolved value including defaults. Run `-t` as a gate before applying and `-T` to confirm the semantics of what you wrote.
saying these in an interview costs you the question
- Restarts first and checks afterwards
- Closes the working session before verifying
- Assumes a restart drops all logged-in users
- Never takes a backup of sshd_config
- Has no idea where the out-of-band console is