Securing an Ubuntu 22.04 server that communicates over SSH on a public IP is critical, especially when it handles sensitive company backups. Below is a comprehensive guide to harden the server and ensure only authorized staff members can access it.
1. Update the System
Ensure all packages are up-to-date to patch known vulnerabilities:
sudo apt update && sudo apt upgrade -y
sudo apt dist-upgrade -y
sudo apt autoremove -y
2. Secure SSH Access
2.1. Change the Default SSH Port
Edit the SSH configuration file:
sudo nano /etc/ssh/sshd_config
Change the default port (e.g., to 2222):
Port 2222
Restart SSH:
sudo systemctl restart sshd
2.2. Disable Root Login
In /etc/ssh/sshd_config, ensure:
PermitRootLogin no
2.3. Use SSH Key Authentication Only
Disable password authentication:
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no
Generate SSH keys on your local machine and copy the public key to the server:
ssh-keygen -t ed25519
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server_ip -p 2222
2.4. Restrict SSH Access to Specific Users
Allow only authorized staff members:
AllowUsers user1 user2
2.5. Enable Two-Factor Authentication (2FA)
Install Google Authenticator:
sudo apt install libpam-google-authenticator
Run for the user:
google-authenticator
Edit /etc/pam.d/sshd and add:
auth required pam_google_authenticator.so
In /etc/ssh/sshd_config, set:
AuthenticationMethods publickey,keyboard-interactive
2.6. Limit SSH Access via Firewall (UFW)
Allow only the new SSH port and restrict to specific IPs:
sudo ufw allow 2222/tcp
sudo ufw allow from staff_ip to any port 2222
sudo ufw enable
3. Harden the System
3.1. Configure Fail2Ban
Install and configure Fail2Ban to block brute-force attacks:
sudo apt install fail2ban
sudo systemctl enable fail2ban
Edit /etc/fail2ban/jail.local:
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 1h
3.2. Disable Unnecessary Services
Stop and disable unused services:
sudo systemctl stop apache2
sudo systemctl disable apache2
3.3. Enable Automatic Security Updates
Install and configure unattended upgrades:
sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades
3.4. Configure AppArmor
Ensure AppArmor is enforcing profiles:
sudo aa-enforce /etc/apparmor.d/*
4. Monitor and Audit
4.1. Enable Logging and Auditing
Install and configure auditd:
sudo apt install auditd
sudo systemctl enable auditd
Edit /etc/audit/audit.rules to monitor critical files:
-w /etc/ssh/sshd_config -p wa -k ssh_config
-w /var/log/auth.log -p wa -k auth_log
4.2. Set Up Logwatch
Install Logwatch for log analysis:
sudo apt install logwatch
sudo logwatch --output mail --mailto admin@example.com --detail high
5. Backup and Disaster Recovery
5.1. Automate Backups
Use rsync or duplicity for encrypted backups:
sudo apt install duplicity
duplicity /path/to/backup scp://user@backup_server//backup/destination
5.2. Test Restore Procedures
Regularly test backup restoration to ensure data integrity.
6. Additional Security Measures
Final Steps
- Reboot the server to apply all changes:
sudo reboot
- Test SSH access with a non-root user and 2FA.
By following these steps, your Ubuntu 22.04 server will be significantly hardened against unauthorized access while ensuring only authorized staff can manage backups.