SSH Hardening là bước bảo mật quan trọng nhất cho server Linux. Mặc định, SSH cho phép root login, password authentication, và lắng nghe trên port 22 — tất cả đều là mục tiêu của automated attacks. Theo thống kê, hàng triệu SSH servers bị brute-force mỗi ngày. Bài viết này hướng dẫn cách harden SSH server từ cơ bản đến nâng cao.
Tại sao SSH Hardening quan trọng?
Mỗi ngày, hàng triệu SSH servers trên internet bị scanner tự động quét port 22. Chúng tìm kiếm:
- Brute-force attacks: Thử hàng nghìn password phổ biến.
- Default credentials: root/root, admin/admin.
- Zero-day exploits: Lỗ hổng trong phiên bản SSH cũ.
Một server chưa harden có thể bị crack trong vài giờ. Kết hợp với UFW Firewall và WireGuard VPN để bảo mật toàn diện.
1. Cấu hình sshd_config cơ bản
File cấu hình SSH nằm tại /etc/ssh/sshd_config. Đây là nơi đầu tiên cần hardening.
# /etc/ssh/sshd_config
# Chỉ dùng SSHv2
Protocol 2
# Tắt root login
PermitRootLogin no
# Tắt password authentication
PasswordAuthentication no
PermitEmptyPasswords no
# Chỉ cho phép public key
PubkeyAuthentication yes
# Thay đổi port (giảm 90% automated scans)
Port 2222
# Giới hạn login attempts
MaxAuthTries 3
# Timeout sau 5 phút idle
ClientAliveInterval 300
ClientAliveCountMax 2
# Tắt các tính năng không cần
X11Forwarding no
AllowTcpForwarding no
Permit Tunneling no
# Chỉ cho phép user cụ thể
AllowUsers admin user1 user2
Sau khi chỉnh sửa, test và restart SSH service:
# Test cấu hình trước khi restart
sshd -t
# Restart SSH service
systemctl restart sshd
2. Key-Based Authentication
SSH keys an toàn hơn password rất nhiều. Key pair gồm public key (đặt trên server) và private key (giữ trên máy client).
Tạo SSH Key
# RSA 4096 bit
ssh-keygen -t rsa -b 4096 -C "your-email@domain.com"
# Hoặc Ed25519 (nhanh hơn, khuyến nghị)
ssh-keygen -t ed25519 -C "your-email@domain.com"
Copy Public Key lên Server
# Cách 1: ssh-copy-id
ssh-copy-id -p 2222 user@server-ip
# Cách 2: Thủ công
cat ~/.ssh/id_ed25519.pub | ssh -p 2222 user@server-ip \
"mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
3. Fail2Ban: Auto-Block Attackers
Fail2ban tự động đọc logs và block IP sau N failed login attempts. Must-have cho bất kỳ SSH server nào.
# Cài đặt
apt update && apt install fail2ban -y
# Tạo config riêng (không bị ghi đè khi update)
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
# Sửa /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 86400
findtime = 600
# Enable và start
systemctl enable fail2ban
systemctl start fail2ban
# Kiểm tra
fail2ban-client status sshd
4. Two-Factor Authentication (2FA)
Thêm lớp bảo mật thứ hai với Google Authenticator. Ngay cả khi hacker có SSH key, họ vẫn cần mã từ điện thoại.
# Cài đặt
apt install libpam-google-authenticator -y
# Setup cho user
su - youruser
google-authenticator
# Trả lời các câu hỏi:
# - Time-based tokens? Y
# - Update .google_authenticator? Y
# - Disallow multiple uses? Y
# - Rate limiting? Y
Bật 2FA trong SSH:
# /etc/ssh/sshd_config
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive
# /etc/pam.d/sshd
auth required pam_google_authenticator.so
# Restart
systemctl restart sshd
5. Firewall Rules
Kết hợp với UFW Firewall để giới hạn SSH access chỉ từ trusted IPs:
# Cho phép SSH chỉ từ specific IP
ufw allow from 192.168.1.0/24 to any port 2222
# Hoặc dùng iptables
iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 2222 -j ACCEPT
iptables -A INPUT -p tcp --dport 2222 -j DROP
6. Monitoring SSH Logs
Theo dõi SSH access để phát hiện attacks sớm:
# Real-time monitoring
tail -f /var/log/auth.log | grep sshd
# Đếm failed attempts
grep "Failed password" /var/log/auth.log | wc -l
# Top attackers
grep "Failed password" /var/log/auth.log | \
awk '{print $(NF-5)}' | sort | uniq -c | sort -nr | head -10
# Successful logins
grep "Accepted" /var/log/auth.log | tail -20
7. Port Knocking (Tùy chọn)
Port knocking ẩn SSH port — chỉ mở khi gửi đúng sequence packets. Ngăn chặn hoàn toàn automated scans.
# Cài đặt
apt install knockd -y
# Cấu hình /etc/knockd.conf
[options]
LogFile = /var/log/knockd.log
[openSSH]
sequence = 7000,8000,9000
seq_timeout = 10
command = /sbin/iptables -I INPUT -s %IP% -p tcp --dport 2222 -j ACCEPT
tcpflags = syn
[closeSSH]
sequence = 9000,8000,7000
seq_timeout = 10
command = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 2222 -j ACCEPT
tcpflags = syn
# Enable knockd
sed -i 's/START_KNOCKD=0/START_KNOCKD=1/' /etc/default/knockd
systemctl restart knockd
8. Advanced Security
- SSH Certificates: Thay vì key pairs, dùng certificates để quản lý access dễ hơn.
- Jump Host: Không connect trực tiếp vào production server, dùng jump host.
- VPN: SSH chỉ accessible qua VPN như WireGuard.
Checklist SSH Hardening
- [ ] Đổi port SSH (không phải 22)
- [ ] Tắt root login
- [ ] Tắt password authentication
- [ ] Deploy SSH keys cho tất cả users
- [ ] Cài đặt và cấu hình Fail2Ban
- [ ] Bật 2FA cho user quan trọng
- [ ] Cấu hình firewall giới hạn IP
- [ ] Test SSH với 2 sessions trước khi đóng session cũ
Kết Luận
SSH hardening là bắt buộc cho bất kỳ server Linux nào. Không có giải pháp magic bullet, nhưng với các bước trên, bạn sẽ giảm đáng kể nguy cơ bị tấn công.
- Bắt đầu từ key-based authentication và fail2ban.
- Thêm 2FA cho bảo mật cao hơn.
- Luôn test kỹ trước khi đóng session SSH cũ.
Kết hợp với Docker Security và PostgreSQL Setup để có security stack hoàn chỉnh cho server.