Role You are a IT specialist which is best at pentests and internet security on ssh.…
- User
Role
You are a IT specialist which is best at pentests and internet security on ssh.
Task
Secure ubuntu 22.04 server which communicates on public ip via ssh.
Context
This server is responsible for keeping company's backup. Its crucial that only stuff member have access.
Assistant
A very important task! Securing an SSH server that's exposed to the public internet and holds critical company backups requires utmost care. I'll guide you through a step-by-step hardening process for your Ubuntu 22.04 server. Buckle up!
Before we start:
- Backup your current SSH config:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup - Ensure you have physical access or an out-of-band management (OOM) (e.g., IPMI, KVM-over-IP) in case something goes wrong, and you get locked out.
- Schedule a maintenance window to apply these changes, as some steps will restart the SSH service, causing a brief disconnect.
Phase 1: Initial Assessment & Updates (10 minutes)
- Update & Upgrade: Ensure your system is up-to-date:
sudo apt update && sudo apt full-upgrade -y - Install Fail2Ban (intrusion prevention):
sudo apt install fail2ban -y - Reboot (optional but recommended):
sudo reboot
Phase 2: SSH Hardening (30 minutes)
- Change the default SSH port (reduce botnet scans):
- Edit
/etc/ssh/sshd_config:sudo nano /etc/ssh/sshd_config - Change
Port 22to a custom port, e.g.,Port 22999(document this change!) - Save and exit (
Ctrl+X,Y,Enter)
- Edit
- Disable SSH protocol 1 (deprecated and insecure):
- In the same
/etc/ssh/sshd_configfile, add (or uncomment):Protocol 2
- In the same
- Restrict SSH to specific users (company staff):
- Create a group for SSH access:
sudo groupadd sshaccess - Add staff members to this group:
sudo usermod -aG sshaccess <username>(repeat for each staff member) - In
/etc/ssh/sshd_config, add:AllowGroups sshaccess
- Create a group for SSH access:
- Disable root login (use
sudoinstead):- In
/etc/ssh/sshd_config, change:PermitRootLogin no
- In
- Use SSH key-based authentication (more secure than passwords):
- On your staff members' workstations: generate SSH keys if they don't already have them:
ssh-keygen -t ed25519(orrsa -b 4096for older systems) - Copy their public keys (
id_ed25519.puborid_rsa.pub) to the server:ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 22999 user@server_ip(adjust port and user) - On the server, in
/etc/ssh/sshd_config, set:PubkeyAuthentication yesandPasswordAuthentication no(disables password auth)
- On your staff members' workstations: generate SSH keys if they don't already have them:
- Limit SSH authentication attempts:
- In
/etc/ssh/sshd_config, add:MaxAuthTries 3
- In
- Set a strong SSH banner ( warning for unauthorized access):
- Create a file:
sudo nano /etc/ssh/banner - Paste a stern message, e.g.: "Unauthorized access prohibited. Logins monitored."
- In
/etc/ssh/sshd_config, add:Banner /etc/ssh/banner
- Create a file:
Phase 3: Firewall & Fail2Ban Configuration (15 minutes)
- Configure UFW (Uncomplicated Firewall):
- Install:
sudo apt install ufw -y - Allow your custom SSH port:
sudo ufw allow 22999/tcp(adjust the port) - Deny all other incoming traffic by default:
sudo ufw default deny incoming - Enable UFW:
sudo ufw enable
- Install:
- Configure Fail2Ban for SSH:
- Create a new jail:
sudo nano /etc/fail2ban/jail.d/ssh.local - Paste:
- Create a new jail:
[ssh] enabled = true port = 22999 # Your custom SSH port filter = sshd logpath = /var/log/auth.log maxretry = 3 # 3 attempts, then ban bantime = 1h # Ban for 1 hour* Restart Fail2Ban: `sudo systemctl restart fail2ban`Phase 4: System & Kernel Hardening (15 minutes)
- Enable TCP Syn Cookies (protect against SYN flood attacks):
sudo nano /etc/sysctl.conf- Add:
net.ipv4.tcp_syncookies = 1 - Apply:
sudo sysctl -p /etc/sysctl.conf
- Disable IPv6 (if not needed, reduces attack surface):
sudo nano /etc/sysctl.conf- Add:
net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 net.ipv6.conf.lo.disable_ipv6 = 1* Apply: `sudo sysctl -p /etc/sysctl.conf`3. Harden kernel params: *
sudo nano /etc/sysctl.conf* Add:kernel.sysrq = 0 # Disable SysRq net.ipv4.icmp_echo_ignore_broadcasts = 1 # Ignore ICMP broadcasts net.ipv4.icmp_ignore_bogus_error_responses = 1 # Ignore bogus errors* Apply: `sudo sysctl -p /etc/sysctl.conf`Phase 5: Finalize & Verify (10 minutes)
- Restart SSH service:
sudo systemctl restart sshd - Verify SSH is listening on the new port:
sudo ss -tlnp | grep sshd - Test your SSH connection from a staff member's workstation:
ssh -p 22999 user@server_ip - Double-check Fail2Ban:
sudo fail2ban-client status ssh - Monitor logs for any issues:
sudo tail -f /var/log/auth.logandsudo journalctl -u sshd
Additional Recommendations:
- Set up email notifications for Fail2Ban bans:
sudo apt install sendmailand configure/etc/fail2ban/action.d/sendmail.conf - Implement Two-Factor Authentication (2FA) for SSH using Google Authenticator or YubiKey (advanced setup)
- Schedule regular security audits (e.g., OpenVAS, Lynis) and penetration tests (yes, hire me 😉)
- Consider setting up a hidden SSH port via
knockd(port knocking) for extra obscurity
Document everything! Share these changes with your team, and make sure they understand:
- The new SSH port
- SSH key management (backup their keys!)
- Group membership (
sshaccess) - 2FA procedures (if implemented)
Congratulations! Your Ubuntu 22.04 backup server is now seriously hardened against unauthorized access. Keep monitoring, updating, and stay vigilant!
If you encounter issues or need further assistance, feel free to ask. Now, go backup those company assets with peace of mind!
- Backup your current SSH config:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.