Sub topic 12
Hardening the SSH Server
Assume you can use sudo, edit text files, manage systemd services, and keep a second login session open. SSH hardening reduces the chance that attackers can guess credentials, reuse weak accounts, or abuse unnecessary features. Apply changes first on a test server.
Choose safe SSH settings
SSH is a network protocol for encrypted remote administration. The SSH daemon, sshd, reads its server configuration from /etc/ssh/sshd_config and included files. A configuration drop-in keeps local changes separate from the main package-managed file.
Create /etc/ssh/sshd_config.d/60-cis.conf:
LogLevel INFO
PermitRootLogin no
PermitEmptyPasswords no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
HostbasedAuthentication no
IgnoreRhosts yes
PermitUserEnvironment no
X11Forwarding no
MaxAuthTries 4
LoginGraceTime 60
ClientAliveInterval 15
ClientAliveCountMax 3
MaxStartups 10:30:60
MaxSessions 10PermitRootLogin no requires administrators to log in with a named account and then use sudo. Disabling password and keyboard-interactive authentication allows key-based access only. Do this only after confirming that an administrator can log in with a working key. ClientAliveInterval and ClientAliveCountMax close inactive sessions after three unanswered checks.
MaxStartups limits unauthenticated connection attempts. Its three values mean that the daemon starts dropping connections at 10 unauthenticated sessions, drops them with increasing probability until 30, and drops all new ones at 60.
> warning > Do not disable password authentication until you have tested public-key login in a separate terminal. Keep the existing session open while changing SSH settings.
Validate before reloading
sshd -t checks syntax without changing the running daemon. sshd -T prints the effective configuration after applying defaults and included files.
sudo sshd -t
sudo sshd -T | grep -Ei '^(permitrootlogin|passwordauthentication|kbdinteractiveauthentication|pubkeyauthentication|maxauthtries|x11forwarding|loglevel) '
sudo systemctl reload sshd
sudo systemctl status sshd --no-pagerSome distributions name the service ssh instead of sshd. A reload keeps existing sessions but makes new connections use the updated settings. The SSH daemon uses the first value it reads for most options, so the filename and the order of Include directives matter. Confirm the effective result with sshd -T rather than relying on the file you edited.
Understand the connection flow
Record the approved SSH port, allowed users or groups, authentication method, and exception rationale in your server baseline. A CIS recommendation is a control to evaluate, not permission to break a workload.
Exercise
Why should you run sshd -t and test a second login before ending the current session?
Show answer
sshd -t catches invalid syntax, while the second login proves that the effective settings still allow administrative access before the current session is closed.