SSH 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, close idle sessions. 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, close idle sessions, 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:
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:
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. With3the server closes the connection sooner, and protection tools like fail2ban kick in faster.Set in
/etc/ssh/sshd_config:MaxAuthTries 3Then reload:
sudo systemctl reload sshCheck:
sshd -T | grep maxauthtriesmust showmaxauthtries 3.4. Close idle sessions after 5 minutes
Concept: idle timeout. An open SSH session in an unattended terminal is an entry point — someone could take it over.
ClientAliveIntervaldefines after how many seconds of inactivity the server ends an idle session.300= 5 minutes.Set in
/etc/ssh/sshd_config:ClientAliveInterval 300Then reload:
sudo systemctl reload sshCheck:
sshd -T | grep clientaliveintervalmust showclientaliveinterval 300.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 student 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:
sshd -T | grep allowusersmust containstudentandssh-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:Banner /etc/issue.netThen reload:
sudo systemctl reload sshCheck:
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. And onlysshd -Tshows the actually loaded configuration — looking at the file is not enough, because a typo could stop it from loading at all.Make sure public-key authentication is enabled, reload the service, and verify the result:
sudo systemctl reload ssh sshd -T | grep -i pubkeyauthenticationThe output must contain
pubkeyauthentication yes.Check: The validator confirms that
sshdwas reloaded and public-key auth is effectively active.
Now practice it yourself
Reading is good – doing is better. Start this course 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).
