Server Hardening
SSH Server Configuration Hardening
Modifying SSH Configuration File
The primary SSH server configuration file is located at /etc/ssh/sshd_config
. Hardening involves strategic modifications to enhance security.
## Edit SSH configuration
sudo nano /etc/ssh/sshd_config
Key Hardening Parameters
Parameter |
Recommended Setting |
Purpose |
Port |
Non-standard port |
Reduce automated scanning |
PermitRootLogin |
no |
Prevent direct root access |
PasswordAuthentication |
no |
Enforce key-based authentication |
MaxAuthTries |
3 |
Limit login attempts |
AllowUsers |
specific_usernames |
Restrict user access |
Disable Unnecessary Authentication Methods
## Recommended configuration snippets
PermitRootLogin no
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no
SSH Key Management
Generating Strong SSH Keys
## Generate a strong RSA key
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_secure
Key Permissions
## Set correct key permissions
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Network-Level Hardening
graph TD
A[SSH Server] --> B{Firewall}
B --> |Allow Specific IP| C[Authorized Access]
B --> |Block| D[Unauthorized Access]
Firewall Configuration
## UFW (Uncomplicated Firewall) configuration
sudo ufw allow from 192.168.1.0/24 to any port 22
sudo ufw enable
Advanced Protection Techniques
Install Fail2Ban
## Install and configure Fail2Ban
sudo apt-get install fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo systemctl restart fail2ban
Monitoring and Logging
## Enable detailed SSH logging
sudo sed -i 's/#LogLevel INFO/LogLevel VERBOSE/' /etc/ssh/sshd_config
sudo systemctl restart sshd
LabEx Security Tip
LabEx recommends practicing these hardening techniques in controlled environments to develop robust SSH security skills.
Restart SSH Service
## Apply configuration changes
sudo systemctl restart sshd
Verification
## Check SSH configuration
sudo sshd -t