Sub topic 12
Hardening the SSH server
This chapter covers the SSH recommendations in the CIS benchmark for a server. It assumes you can edit files as root, use systemctl, and have already scanned the host with OpenSCAP (see the assessment chapter) and set up a default-deny firewall that allows TCP port 22.
Why SSH gets its own section
SSH is usually the only remote administration path into a server. It is exposed to the network and accepts credentials, so attackers probe it constantly. The CIS recommendations reduce what an attacker can try (root login, empty passwords, many guesses) and what a compromised session can do (X11 forwarding, user environment tricks). They also close idle sessions.
How sshd reads its configuration
The daemon sshd reads /etc/ssh/sshd_config. On current RHEL, Debian and Ubuntu releases, that file starts with an Include /etc/ssh/sshd_config.d/*.conf line. Drop-in files are the cleanest place for CIS settings, because package updates do not overwrite them.
The key rule is that sshd keeps the first value it reads for each keyword. Drop-ins are read in lexical order, so 10-local.conf beats 60-cis.conf. If the main file sets a keyword before the Include line, it beats every drop-in. This is the most common reason a setting appears to have no effect.
A CIS drop-in file
Create /etc/ssh/sshd_config.d/60-cis.conf. If your distribution ships a file with a lower number (for example 50-cloud-init.conf on some cloud images) that sets the same keywords, name yours so it sorts first, such as 01-cis.conf.
# /etc/ssh/sshd_config.d/60-cis.conf
LogLevel INFO
PermitRootLogin no
PermitEmptyPasswords no
HostbasedAuthentication no
IgnoreRhosts yes
PermitUserEnvironment no
X11Forwarding no
MaxAuthTries 4
LoginGraceTime 60
ClientAliveInterval 15
ClientAliveCountMax 3
MaxStartups 10:30:60
MaxSessions 10
# Validate the configuration, then print the effective values
sudo sshd -t
sudo sshd -T | grep -Ei 'permitrootlogin|maxauthtries|x11forwarding'The lines after the blank line are commands, not configuration. Run them in a shell, not inside the file.
What each setting does
| Keyword | Value | Purpose |
|---|---|---|
| `LogLevel` | INFO | Records logins and failed attempts. `VERBOSE` also logs key fingerprints, which helps trace which key was used. |
| `PermitRootLogin` | no | Forces administrators to log in as themselves and escalate with sudo, so actions are attributable. |
| `PermitEmptyPasswords` | no | Rejects accounts with blank passwords. |
| `HostbasedAuthentication` | no | Disables trust based on the client host. |
| `IgnoreRhosts` | yes | Ignores legacy `.rhosts` trust files. |
| `PermitUserEnvironment` | no | Stops users from setting environment variables through `~/.ssh/environment`, which could bypass restrictions. |
| `X11Forwarding` | no | Removes a graphical forwarding channel a server rarely needs. |
| `MaxAuthTries` | 4 | Limits guesses per connection. After half the limit, failures are logged. |
| `LoginGraceTime` | 60 | Seconds allowed to complete authentication. |
| `ClientAliveInterval` / `ClientAliveCountMax` | 15 / 3 | Sends a keepalive every 15 seconds and drops the session after 3 unanswered ones (about 45 seconds). |
| `MaxStartups` | 10:30:60 | Begins refusing 30% of unauthenticated connections once 10 are pending, rising to 100% at 60. |
| `MaxSessions` | 10 | Caps multiplexed sessions per connection. |
The CIS benchmark has named values that differ between versions (for example the idle-timeout numbers). Check the exact values in the benchmark version that your scan profile uses.
Validate before you reload
A syntax error or a bad value can stop sshd from restarting, and on a remote machine that locks you out. Use this order:
- Keep a second SSH session open, or a console session, for the whole procedure.
- Run
sudo sshd -t. It prints nothing on success and an error with a line number on failure. - Run
sudo sshd -Tand confirm the effective values.-Tprints every keyword in lowercase, so usegrep -i. - Reload the daemon with
sudo systemctl reload sshd(the unit issshon Debian and Ubuntu). - From a third terminal, open a new connection. Only close the old sessions once it works.
A reload keeps existing sessions alive. That is why a bad config only shows itself when you open a new connection.
Testing a single user or address
Settings can differ by Match block. To check what applies to a specific connection, pass the connection details:
sudo sshd -T -C user=alice,host=client.example.com,addr=192.0.2.10 | grep -Ei 'maxauthtries|permitrootlogin'Place Match blocks at the end of a file. Everything after a Match line belongs to it until the next Match or the end of the file, so a global keyword written below one applies only to that match.
Other CIS items to check
The benchmark has more SSH items than the drop-in above. Common ones are:
- File permissions:
/etc/ssh/sshd_configand the drop-ins must be owned by root with mode0600or tighter. Private host keys (/etc/ssh/ssh_host_*_key) must be root-owned and not readable by others. - Strong crypto: the benchmark asks you to avoid weak ciphers, MACs and key exchange algorithms. Set
Ciphers,MACsandKexAlgorithmsonly after checkingsshd -Toutput and your clients' support, because a list that is too strict breaks older tools. - A login banner:
Banner /etc/issue.net, with legal text in that file. - Access limits:
AllowUsers,AllowGroups,DenyUsersorDenyGroupsso that only expected accounts can log in. - Disabling
UsePAM: do not do this. The benchmark expects PAM to stay enabled so that account and session policy applies.
Password authentication and keys
The CIS profile does not mandate key-only login in every version, but it is the strongest practical step. Once keys work for all admins, add PasswordAuthentication no to the drop-in. Confirm with sshd -T | grep passwordauthentication, and test with a new session before closing the old one. If you keep passwords, the account lockout settings in the PAM chapter limit guessing across the system.
Common errors
- Setting has no effect. Another file set it first. Use
sshd -Tto see the winner, thengrep -ri keyword /etc/ssh/to find the earlier line. Bad configuration option. A typo or a keyword the installed OpenSSH version does not know.sshd -tnames the line.- Cannot connect after the firewall change. The SSH port in the firewall must match the
Portin the config, and SELinux must allow any non-default port (semanage port -a -t ssh_port_t -p tcp 2222). - OpenSCAP still fails the rule. The scanner may check the main file only, or expect an exact value. Read the rule's detail in the HTML report, then compare it with
sshd -T.
Exercise
You add MaxAuthTries 4 to /etc/ssh/sshd_config.d/60-cis.conf, but sshd -T still reports maxauthtries 6. Name two likely causes and how you would find the one at fault.
Show answer
Another file set the keyword first, because sshd keeps the first value it reads. This could be /etc/ssh/sshd_config above its Include line, or a drop-in with a name that sorts before 60-cis.conf, such as 50-cloud-init.conf. A second cause is that the daemon was not reloaded after the edit, or the Include line is missing or commented out. Run grep -ri maxauthtries /etc/ssh/ to find every definition and their order. Then check systemctl status sshd and the first lines of sshd_config for the Include. Fix it by removing the earlier line or renaming your drop-in so it sorts first, then run sshd -t and reload.
Exercise
Why should you keep an existing SSH session open and test with a new connection after reloading sshd?
Show answer
A reload does not end existing sessions, so they keep working even if the new configuration blocks logins. Only a new connection exercises the new settings. If the new connection fails, the open session lets you revert the change.