ssh hardening (Copy 2)
ssh hardening (Copy 2)
## Introduction
**Prerequisites**:
- Ubuntu 22.04 VPS with OpenSSH installed (`sudo apt install openssh-server`).
- Root or sudo access.
- Basic Linux command-line knowledge (e.g., editing files with `nano`).
- A client machine to test SSH connections.
**Safety Tip**: Always test changes in a virtual machine (VM) or keep a backup SSH
session open to avoid lockouts.
---
ls /etc/ssh/sshd_config.bak
cat /etc/ssh/sshd_config.bak
PermitRootLogin no
ssh root@server_ip
Press Enter for no passphrase (or set one for extra security).
Set:
bash
PasswordAuthentication no
ssh username@server_ip
Change or add:
bash
Port 2222
Note: Update firewall rules (Step 8) before testing to avoid lockouts. This is
security through obscurity; combine with other measures.
6. Restrict SSH Users
Description: Limit SSH access to specific users to prevent unauthorized accounts
from logging in.
Why: Reduces the attack surface by restricting access to trusted users.
Steps:
Edit the SSH configuration:
bash
Add:
bash
AllowUsers username
ssh otheruser@server_ip
It should fail.
Note: List multiple users if needed (e.g., AllowUsers user1 user2).
7. Set Idle Timeout
Description: Automatically disconnect idle SSH sessions to reduce the risk of
hijacking.
Why: Idle sessions can be exploited if a user leaves a terminal open.
Steps:
Edit the SSH configuration:
bash
Add or modify:
bash
ClientAliveInterval 300
ClientAliveCountMax 0
300 seconds (5 minutes) before checking; 0 means disconnect after one missed check.
Test: Log in, remain idle for 5 minutes, and verify disconnection.
Note: Adjust ClientAliveInterval based on use case (e.g., 600 for 10 minutes).
8. Configure Firewall for SSH
Description: Use a firewall to restrict SSH access to trusted IPs or ports,
blocking unauthorized traffic.
Why: Limits exposure to only necessary network connections.
Steps:
Install Uncomplicated Firewall (UFW):
bash
bash
Verify rules:
bash
Test: Attempt SSH from an allowed IP (should succeed) and a disallowed IP (should
fail).
Note: Allow the new SSH port before enabling UFW to avoid lockouts.
9. Install and Configure Fail2Ban
Description: Deploy Fail2Ban to block IPs after repeated failed login attempts,
protecting against brute-force attacks.
Why: Automated attacks try thousands of credentials; Fail2Ban stops them
dynamically.
Steps:
Install Fail2Ban:
bash
Add or modify:
ini
[sshd]
enabled = true
port = 2222
maxretry = 3
bantime = 600
Restart Fail2Ban:
bash
Verify status:
bash
ssh wronguser@server_ip
Note: Adjust maxretry and bantime (e.g., 3600 for 1-hour bans).
10. Audit SSH Configuration
Description: Use ssh-audit to identify weak ciphers, algorithms, or configurations
and apply recommended fixes.
Why: Ensures the SSH server uses modern, secure settings and detects
misconfigurations.
Steps:
Install ssh-audit:
bash
ssh-audit server_ip:2222
Review the output and apply recommendations, such as disabling weak ciphers:
Edit /etc/ssh/sshd_config:
bash
Ciphers [email protected],[email protected],[email protected]
KexAlgorithms [email protected]
MACs [email protected],[email protected]
Restart SSH:
bash
Combine Defenses: Use multiple layers (keys, firewall, Fail2Ban) for robust
security.
Classroom Activities
Hands-On Lab: Apply steps 1–4 on a provided Ubuntu VM, then test SSH access.
Audit Challenge: Run ssh-audit and propose fixes based on the report.
Discussion: Debate the trade-offs of changing the SSH port vs. relying on Fail2Ban.
End of Guide
---
Follow one of these methods to convert the Markdown content into a professional PDF
for your class:
\begin{document}
\maketitle
\tableofcontents
\newpage
\section{Introduction}
This guide provides step-by-step instructions to harden an SSH server on a
Ubuntu 22.04 VPS...
\section{Resources}
\begin{itemize}
\item Mozilla SSH Guidelines: \url{https://infosec.mozilla.org}
\item SSH Audit Tool: \url{https://github.com/jtesta/ssh-audit}
\item LabEx SSH Labs: \url{https://labex.io}
\item Ubuntu Security Guide: \url{https://ubuntu.com/security}
\end{itemize}
\end{document}
```
4. **Paste Steps**:
- Add each step as a `\section` with `\textbf{Description}`, `\textbf{Why}`,
etc., as shown.
- Use `\lstinline{}` for inline commands or `\begin{lstlisting}` for code
blocks.
5. **Compile and Download**:
- Click “Compile” in Overleaf to generate the PDF.
- Download via `Menu > Download > PDF`.
- Name it `SSH_Hardening_Guide_Ubuntu_VPS.pdf`.
6. **Distribute**:
- Share the PDF with students.
---
---
---
---
---
### **Customizations**
If you need specific additions, let me know:
- A shortened version (e.g., only steps 1–5 for a shorter class).
- A LaTeX template with advanced styling (e.g., colored code blocks).
- Integration with broader VPS hardening steps.
- A slide deck outline to complement the PDF.
Once you’ve created the PDF, you’ll have a professional, downloadable resource
ready for your beginner students. If you encounter issues during conversion or need
further help, share details, and I’ll guide you through the process!