32. Linux and Network Hardening
32.2 SSH Hardening and fail2ban
In short: SSH is the front door of every Linux server.
SSH is the front door of every Linux server. Lock it in this order.
1. Use keys, not passwords. EC2 already gives key login. For your own VM:
ssh-keygen -t ed25519 -C "ravi-laptop" # on your laptop
ssh-copy-id student@192.168.56.20 # copy the public key
2. Edit /etc/ssh/sshd_config:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
LoginGraceTime 30
AllowUsers ec2-user deploy
X11Forwarding no
ClientAliveInterval 300
ClientAliveCountMax 2
3. Test and restart:
sudo sshd -t # syntax check – no output means OK
sudo service sshd restart
# In a NEW terminal: ssh -i key.pem ec2-user@server (keep the old one open!)
Changing the SSH port (for example to 2222) only reduces noise in logs; it is not real security on its own.
4. fail2ban bans IPs that fail too many times:
sudo yum install -y fail2ban # from EPEL on some distributions
sudo tee /etc/fail2ban/jail.local <<'CONF'
[sshd]
enabled = true
maxretry = 5
findtime = 10m
bantime = 1h
CONF
sudo systemctl enable fail2ban
sudo service fail2ban start
sudo fail2ban-client status sshd # shows banned IPs
5. Best of all on AWS: keep port 22 closed and use Session Manager (Chapter 30).
Ravindra Bagale's Tip
You set PasswordAuthentication no and forgot to copy your key – locked out of the server for good! So first check that key login works, then turn off passwords, and restart only after sshd -t. Always keep one terminal open.
Ravindra Bagale's Tip – मराठी
PasswordAuthentication no लावले आणि key copy करायला विसरलात – server मधून कायमचे बाहेर! म्हणून आधी key ने login यशस्वी होते हे चेक करा, मग password बंद करा, आणि sshd -t नंतरच restart करा. एक terminal नेहमी उघडा ठेवा.
Ravindra Bagale's Tip – हिंदी
PasswordAuthentication no लगाया और key copy करना भूल गए – server से हमेशा के लिए बाहर! इसलिए पहले चेक करो कि key से login हो रहा है, फिर password बंद करो, और sshd -t के बाद ही restart करो. एक terminal हमेशा खुला रखो.
Lab
On your lab server apply the sshd_config settings above and fail2ban. From Kali, run Hydra (Chapter 22) against SSH and show that (1) password attempts are refused and (2) the Kali IP appears in fail2ban-client status sshd. Unban it afterwards with sudo fail2ban-client set sshd unbanip 192.168.56.10.
Extended teaching notes
Why this lesson matters
Hardening is deleting unnecessary risk: fewer packages, fewer open ports, fewer privileged accounts, faster patches, better logs.
Hardening related services
[('SSH', 'Key auth, no root login, Fail2ban or equivalent, SG limited to your IP/VPN.', '', ''), ('SMB / NFS / LDAP / SNMP', 'Do not expose to the internet; patch; strong auth; disable unused versions (e.g. SMBv1).', '', ''), ('SMTP', 'Authenticated relay only; SPF/DKIM/DMARC for your domains.', '', ''), ('DNS', 'Restrict zone transfers; monitor for unexpected records (crt.sh for certs).', '', ''), ('Firewalls', 'Host firewall + cloud SG/NSG; default deny; document every allow.', '', '')]
Real examples
Global: CIS Benchmarks exist for Linux, Windows and clouds. Indian company (sample): a Pune team requires SSH keys only and weekly patch windows for internet-facing hosts.
Mini case study
Suppose we are on a purple-team exercise for an Indian mid-size company (sample data). Red notes a technique from this lesson against a lab clone. Blue must show the control that stops it, the log that would have fired, and the ticket that hardens production. That is the standard this course trains you for.
Defender's worksheet (do this today)
- Write the lesson title, date and the exact lab target you are allowed to use.
- List three owned systems (VMs, apps, or cloud accounts) where this lesson applies.
- Run one safe check from the lesson on a lab system only. Paste the command and output.
- Write a two-sentence fix you would give a teammate.
- Name one related public advisory (CERT-In, CISA, NVD, or a vendor bulletin) with its date.
Indian workplace (sample data)
Suppose we are a junior analyst at Infosys, TCS, Wipro or a fintech SOC in Bengaluru/Pune. Turn this lesson into one checklist item with an owner, a due date and a place to store evidence (ticket + screenshot).
Global practice
Compare your notes with OWASP, CIS Benchmarks, NIST CSF/IR guidance or a cloud Well-Architected security question. Write five lines on what overlaps.
Everyday analogy
Explain the risk and the fix to a family member without jargon. If they can repeat the warning, you understand it well enough to teach juniors.
Key terms
- Control: a safeguard (patch, config, process, detection).
- Finding: a weakness backed by evidence.
- Remediation: the fix plus how you verified it.
- Scope: systems you have written permission to touch.
- Least privilege: only the access the task needs.
- Defence in depth: several layers so one failure does not end the story.
Common mistakes
- Skipping written permission "just for a quick scan".
- Practising on hostel, office or public targets without authorisation.
- Collecting tool output without writing the fix.
- Copying attack steps onto production to "verify". Defenders verify with versions, configs and patches.
- Publishing screenshots that contain secrets, cookies or internal hostnames.
Recap
- Evidence, fix, owner, date.
- Own lab only.
- Fix > spectacle.