This document outlines basic steps to harden an Ubuntu server for better security.
It's essential to update your system to ensure all packages are up to date and patched.
~~~ sudo apt update && sudo apt upgrade -y ~~~ * **Set up a firewall:** Use `ufw` (Uncomplicated Firewall) to configure basic firewall rules. ~~~ sudo ufw allow ssh sudo ufw enable sudo ufw status ~~~
Changing the default SSH port (22) can help reduce automated attacks.
~~~
sudo nano /etc/ssh/sshd_config
~~~
- Change `Port 22` to a custom value (e.g., `Port 2222`).
- Restart SSH:
~~~
sudo systemctl restart ssh
~~~
Disabling direct root login adds an extra layer of protection.
~~~
PermitRootLogin no
~~~
- Restart SSH:
~~~
sudo systemctl restart ssh
~~~
Set up SSH key pairs instead of using passwords.
~~~
ssh-keygen -t rsa -b 4096
~~~
- Copy the public key to the server:
~~~
ssh-copy-id user@your-server-ip
~~~
Avoid using the root account directly. Create a user with sudo privileges:
~~~ sudo adduser username sudo usermod -aG sudo username ~~~
Enforce password complexity using `pam_pwquality.so` by editing `/etc/pam.d/common-password`.
~~~ password requisite pam_pwquality.so retry=3 minlen=12 minclass=3 ~~~
Disable accounts that are not in use to minimize potential attack surfaces:
~~~ sudo usermod -L username ~~~
Enable unattended-upgrades to automatically apply security patches.
~~~ sudo apt install unattended-upgrades sudo dpkg-reconfigure --priority=low unattended-upgrades ~~~
Fail2Ban helps protect SSH and other services by blocking repeated failed login attempts.
~~~ sudo apt install fail2ban sudo systemctl enable fail2ban sudo systemctl start fail2ban ~~~
Install and configure the audit daemon to track login attempts and system changes.
~~~ sudo apt install auditd sudo systemctl enable auditd sudo systemctl start auditd ~~~
Use `systemctl` to list active services:
~~~ sudo systemctl list-units --type=service ~~~
Disable unnecessary services to minimize potential vulnerabilities:
~~~ sudo systemctl disable <service-name> sudo systemctl stop <service-name> ~~~
Remove any software you don't need to reduce the attack surface:
~~~ sudo apt autoremove sudo apt purge <package-name> ~~~
Install `libpam-google-authenticator` to enable 2FA on SSH logins.
~~~
sudo apt install libpam-google-authenticator
google-authenticator
~~~
Follow the prompts to set up 2FA, then configure SSH:
- Edit `/etc/pam.d/sshd` and add the line:
~~~
auth required pam_google_authenticator.so
~~~
- Edit `/etc/ssh/sshd_config` to ensure 2FA works:
~~~
ChallengeResponseAuthentication yes
~~~
- Restart SSH:
~~~
sudo systemctl restart ssh
~~~
Use `LUKS` (Linux Unified Key Setup) to encrypt sensitive data.
~~~
sudo cryptsetup luksFormat /dev/sdX
sudo cryptsetup luksOpen /dev/sdX my_encrypted_disk
sudo mkfs.ext4 /dev/mapper/my_encrypted_disk
~~~
Use tools like `rsync`, `duplicity`, or `Deja Dup` to automate regular backups of important files.