When setting up a new Ubuntu server, security should be a top priority. This tutorial walks you through the essential steps for hardening your server, ensuring its resilience against potential threats.
sudo apt-get install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades
adduser <username>
usermod -aG sudo <username>
Generate an SSH key pair: ssh-keygen -t rsa -b 4096 -C "youremail@example.com"
Copy the public key to the server: ssh-copy-id username@hostname_ip
(If you saved the key under a different name or path, specify the exact path to the public key.)
Edit the SSH config file: sudo nano /etc/ssh/sshd_config
Set PasswordAuthentication to no: PasswordAuthentication no
Ensure PubKeyAuthentication is set to yes: PubKeyAuthentication yes
Restart the SSH server: sudo systemctl restart ssh
Edit the SSH config file: sudo nano /etc/ssh/sshd_config
Set PermitRootLogin to no: PermitRootLogin no
Configure other security settings: MaxAuthTries: Set the maximum login attempts per IP address (consult your company policies). PermitEmptyPasswords: Ensure this is set to no. ChallengeResponseAuthentication: Set to no unless you're setting up 2FA.
Save and restart the SSH server: sudo systemctl restart ssh
Edit the SSH config file: sudo nano /etc/ssh/sshd_config
Modify the Port setting: Port <your_custom_port>
(Choose a non-standard port and ensure your firewall allows traffic on this new port.) Restart the SSH server: sudo systemctl restart ssh
Install the Google Authenticator PAM module: sudo apt install libpam-google-authenticator
Add the following line to /etc/pam.d/sshd: auth required pam_google_authenticator.so nullok
Comment out the line @include common-auth: #@include common-auth
Restart the SSH server: sudo systemctl restart ssh
Adjust SSH config settings: KbdInteractiveAuthentication: Set to yes. ChallengeResponseAuthentication: Set to yes. AuthenticationMethods: Set to publickey,keyboard-interactive. UsePAM: Set to yes. PasswordAuthentication: Set to no.
Configure Google Authenticator: google-authenticator
Follow the prompts, customizing the settings as needed. Remember to save the emergency scratch codes in a safe place. Restart the SSH server: sudo systemctl restart ssh
sudo apt-get install fail2ban ufw
Edit the jail.local file: sudo nano /etc/fail2ban/jail.local
Add the following configuration: [sshd] enabled = true port = <your_custom_port> filter = sshd logpath = /var/log/auth.log maxretry = 3 bantime = 600 findtime = 600
Adjust the settings as needed. Enable and start Fail2Ban: sudo systemctl enable fail2ban sudo systemctl start fail2ban
Check jail status: sudo fail2ban-client status sudo fail2ban-client status sshd
Allow traffic on necessary ports: sudo ufw allow 80 sudo ufw allow 443 sudo ufw allow <your_custom_ssh_port>
Set default rules: sudo ufw default deny incoming sudo ufw default allow outgoing
Use UFW commands for managing rules: sudo ufw allow <port_number>: Allow traffic on a specific port. sudo ufw deny <port_number>: Deny traffic on a specific port. sudo ufw status: Display UFW status. sudo ufw status numbered: Display status with rule IDs. sudo ufw status verbose: Display status with default rules. sudo ufw app list: List application profiles. sudo ufw delete <rule_id>: Delete a rule by ID.
0 comments:
Post a Comment