Practise this module for free on a real VM
Sign up for freeSSH Hardening
You harden your server VM's SSH configuration against the most common attack vectors — disable root login, switch off password auth, limit login attempts, detect dead connections with keep-alive. 7 tasks, about 60 minutes.
Harden SSH configuration
What is SSH — and why "hardening"?
SSH (Secure Shell) is the standard protocol for logging into a remote server encrypted and administering it from the command line. The SSH service is called sshd and listens on port 22 by default.
Because SSH is the main gateway to a server, it is the most popular attack target: bots try usernames and passwords around the clock — this is called brute force. Hardening means configuring the service so that such attacks come to nothing.
Where is it configured?
All settings live in the file /etc/ssh/sshd_config. After every change you must reload the service for it to take effect:
sudo systemctl reload sshYour goal in this lesson
You harden the SSH configuration against the most common attacks: disable root login, switch off password authentication, limit login attempts, detect dead connections, restrict access to allowed users, and set a login banner.
Exercises
1. Disable root login
Concept: root login.
rootis the administrator account with unlimited rights. Allowingrootto log in directly over SSH is a "game-over" risk: whoever guesses the password instantly owns the whole server. Secure servers forbid direct root login — you log in as a normal user and usesudowhen needed.Open the config with
sudo nano /etc/ssh/sshd_config, find thePermitRootLoginline and set it to:PermitRootLogin noSave with
Ctrl+O,Enter, exit withCtrl+X. Then reload the service:sudo systemctl reload sshCheck:
sudo sshd -T | grep permitrootloginmust outputpermitrootlogin no.2. Switch off password authentication
Concept: public-key vs. password login. With password login you type a password — bots can brute-force it. With public-key authentication the user holds a private key and the server only knows the matching public key. Without the private key nobody gets in — orders of magnitude safer and impossible to guess.
Switch off password login in
/etc/ssh/sshd_config:PasswordAuthentication noThen reload:
sudo systemctl reload sshYour existing public-key access is unaffected — so you will not lock yourself out.
Check:
sudo sshd -T | grep passwordauthenticationmust showpasswordauthentication no.3. Limit login attempts to 3
Concept:
MaxAuthTries. This directive limits how many authentication attempts are allowed per connection. The default is6— giving brute-force tools needlessly many tries. With only three attempts the server closes the connection sooner, and protection tools like fail2ban kick in faster.Set in
/etc/ssh/sshd_config:MaxAuthTries ____Then reload:
sudo systemctl reload sshCheck:
sudo sshd -T | grep maxauthtriesmust show the new value.4. Detect dead SSH connections (keep-alive every 5 minutes)
Concept: keep-alive. When a connection drops (Wi-Fi off, laptop closed), the server often does not notice for a long time — the dead session stays open.
ClientAliveIntervaldefines after how many seconds without data from the client the server asks, through the encrypted channel, whether the other side is still there. IfClientAliveCountMaxanswers in a row are missing (default: 3), it drops the connection. A client that answers stays connected — even if nobody types. So this is not a logout for inactivity. The value is given in seconds.Set in
/etc/ssh/sshd_config:ClientAliveInterval ____Then reload:
sudo systemctl reload sshCheck:
sudo sshd -T | grep clientaliveintervalmust show the new value in seconds.5. Allow SSH only for the student user
Concept: whitelist with
AllowUsers. Instead of allowing all users and blocking individuals (blacklist), you allow only explicitly named users (whitelist) — far safer.Add the allowed users in
/etc/ssh/sshd_config:AllowUsers ____ ssh-validatorThen reload:
sudo systemctl reload ssh⚠️ Important:
ssh-validatormust be in the list — otherwise you lock out the platform validator and the task cannot pass.Check:
sudo sshd -T | grep allowusersmust contain your own user andssh-validator.6. Enable login banner
Concept: login banner. A banner is a notice shown before login. It serves legally as a warning ("authorized access only") and deters attackers.
First create the warning text, then point to it in the SSH config.
Create the banner file:
echo 'Authorized access only. All activity is monitored.' | sudo tee /etc/issue.netSet the banner line in
/etc/ssh/sshd_config:____ /etc/issue.netThen reload:
sudo systemctl reload sshCheck:
sudo sshd -T | grep bannermust show/etc/issue.net.7. Reload sshd and verify public-key auth
Concept: effective vs. merely configured. Changes to
sshd_configtake effect only aftersystemctl reload ssh.sudo sshd -Tshows the effective configuration: all files together (includingsshd_config.d/) plus defaults — and stops with file and line on a typo. It reads the files, not the running service: whether the reload worked is shown bysystemctl status ssh.Make sure public-key authentication is enabled, reload the service, and verify the result in the effective configuration.
sudo systemctl reload ssh sudo sshd -T | grep -i pubkeyauthenticationIt must contain
pubkeyauthentication yes.Check: The checker looks whether the SSH service is running and whether the effective configuration contains
pubkeyauthentication yes. It cannot see whether you reloaded — that part is yours.
Knowledge check: SSH hardening
No VM needed, about 3 minutes. You recall what you practiced in this module – wrong answers come back at the end.
Related courses
Now practice it yourself
Reading is good – doing is better. Practise this module on a real Linux VM, right in your browser. A free account is all it takes.
Start for freeLab content under CC BY 4.0 – free to use with attribution (© TechLogia).
