New Server Hardening- VPS

Guides on Setting up VPS servers and making sure they are as secure as possible

Linux Server Hardening- VPS

These steps go over ways to help harden your Linux Server, especially on a VPS. As VPS servers are public, adding additional security is crucial.

Requirements

More about these steps at dnuburgess GitHub

SSH Commands

Initial Server Setup

New Sudo User Setup
  1. Update System Packages to ensure your system is up to date
sudo apt update && sudo apt list --upgradable && sudo apt upgrade -y && sudo apt autoremove -y
  1. Set up Timezone, as accurate time is important for logs and scheduled tasks
sudo dpkg-reconfigure tzdata
  1. Create Non- Root User, never operate directly as root. Create a new user and give it sudo privileges
adduser <your_new_user>
sudo usermod -aG sudo <your_new_user>

Some VPS systems won't allow you to use public key for ssh that root has access to. You will need to manually add it to the list

  1. Create the .ssh Directory (if it doesn't exist)
mkdir -p ~/.ssh
chmod 700 ~/.ssh
  1. Add Public Key
echo "your_public_key_contents" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Relogin using ssh key toyour new user

Secure SSH Connection with SSH Key Only Authentication

Also, very important consideration is to disable password login, enable passkey login only to use ssh key you just set up, and few other security options

First create and share key to the server SSH Key Authentication Create and Share

  1. Login to server and edit SSH Config File
sudo nano /etc/ssh/sshd_config

Modify the file like picture below

image.png

  1. Save and Exit
  2. Restart SSH Service
sudo systemctl restart ssh

 

Firewall Setup

Setup ufw on the machine for extra security

  1. Install ufw (if not already installed)
sudo apt install ufw
  1. Set Default Policies by configuring the default policies to deny all incoming traffic and allow all outgoing traffic
sudo ufw default deny incoming
sudo ufw default allow outgoing
  1. Change SSH to a custom port
sudo nano /etc/ssh/sshd_config

#Port 22, delete '#' and put custom port number

  1. Run systemd socket activation
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket
sudo systemctl restart ssh
  1. Confirm that SSH is listening to new port
sudo ss -tulpn | grep ssh
# should show something like: 0.0.0.0:42
  1. Add SSH firewall rule and add rate limiting
sudo ufw allow 42/tcp
sudo ufw limit 42/tcp
  1. Add other necessary ports
sudo ufw allow https
sudo ufw allow http
# optional: sudo ufw allow 51820/udp
  1. Enable ufw
sudo ufw enable

Check the ufw status to make sure everything is running and all rules are set properly

sudo ufw status verbose

Set Up Automatic Security Updates

Enable automatic updates for any new security pathes

  1. Install Unattended Upgrades
sudo apt install unattended-upgrades apt-listchanges
  1. Reconfigure Unattended Upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades

Fall2ban Setup

Fail2ban watches your system logs for repeated failed login attempts. When it sees too many failures from the same IP, it automatically bans that IP using your firewall.

It protects services like:

It’s basically an automated bouncer for your server.

Configure Fall2ban

  1. Install Fall2ban
sudo apt update
sudo apt install fail2ban
  1. Enable SSH jail
sudo nano /etc/fail2ban/jail.local

Add

[sshd]
enabled = true
port = 42
logpath = /var/log/auth.log
maxretry = 5

Save and Exit

  1. Restart Fall2ban
sudo systemctl restart fail2ban
  1. Check status
sudo fail2ban-client status
sudo fail2ban-client status sshd