Sign up Log in
n/tothost-vn · 26m ago · 37 views
u/TothostVerified merchantSilver
n/tothost-vnu/Tothost Verified Merchant · 26m ago

[Tutorial] Essential security hardening for a new VPS (Debian/Ubuntu)

Once a new VPS comes online, its public IP usually gets scanned and hit with SSH brute-force attempts within minutes. Below is a basic setup routine for Debian 11/12 and Ubuntu 22.04/24.04; done in order it takes about 10 minutes.

⚠️ Before you start, make sure you can log in through your provider's VNC / console, so you can recover if the SSH config gets messed up.

1. Update the system

apt update && apt upgrade -y

2. Create a regular user and give it sudo

adduser deploy
usermod -aG sudo deploy

You can replace deploy with whatever username you like.

3. Set up SSH key login

Generate a key on your local machine (skip if you already have one):

ssh-keygen -t ed25519 -C "my-vps"

Upload the public key to the VPS:

ssh-copy-id deploy@your-IP

If you're on Windows and don't have ssh-copy-id, just append the contents of your local ~/.ssh/id_ed25519.pub to /home/deploy/.ssh/authorized_keys on the VPS manually.

👉 Open a new terminal first and confirm you can log in with the key before moving on.

4. Disable password login and direct root login

sudo nano /etc/ssh/sshd_config

Change / verify these entries:

PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes

Note: some systems also have config files under /etc/ssh/sshd_config.d/ (for example 50-cloud-init.conf on cloud images) that can override the above settings, so check those too:

sudo grep -r "PasswordAuthentication" /etc/ssh/sshd_config.d/

Check the syntax and restart SSH (the service is called ssh on Debian/Ubuntu):

sudo sshd -t && sudo systemctl restart ssh

5. Enable the UFW firewall

sudo apt install ufw -y
sudo ufw allow 22/tcp          # if you changed the SSH port, use the new port here
sudo ufw allow 80,443/tcp      # only open these if you're hosting a site
sudo ufw enable
sudo ufw status

6. Install Fail2ban to stop brute force

sudo apt install fail2ban -y
sudo tee /etc/fail2ban/jail.local > /dev/null <<'EOF'
[sshd]
enabled  = true
backend  = systemd
maxretry = 5
findtime = 10m
bantime  = 1h
EOF
sudo systemctl enable --now fail2ban
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd

backend = systemd is there for systems like Debian 12 that don't create /var/log/auth.log by default.

7. Enable BBR (optional — improves TCP throughput on cross-border / high-latency links)

echo "net.core.default_qdisc=fq" | sudo tee -a /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
sysctl net.ipv4.tcp_congestion_control

If it prints bbr, you're good. Requires kernel 4.9+ (the default on Debian 10+ / Ubuntu 18.04+) and full virtualization like KVM or VMware — on OpenVZ / LXC containers you usually can't change this yourself.

8. Timezone and automatic security updates (optional)

sudo timedatectl set-timezone Asia/Shanghai
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure -plow unattended-upgrades

✅ Checklist

  • System updated
  • Regular user created and added to sudo
  • Key login works, password login and root login disabled
  • UFW enabled, SSH port allowed
  • Fail2ban running
  • BBR enabled (optional)

All of the above works as-is on TotHost Vietnam VPS (TOT K is KVM, TOT M is VMware). If you want to test the network first, try our Looking Glass: https://lg.tothost.vn/ — questions are welcome in the comments or in our Telegram group https://t.me/tothostvietnam

Translated by AI from 简体中文

3
6
Log into join the discussion.

6 replies

Sort order
  1. rewadi Bronze 21m ago

    thanks

    0
  2. chenchunmiao Bronze 20m ago

    Bookmark

    Translated by AI from 简体中文

    0
  3. James 💯 Legend MANAGER 18m ago

    Great post, thanks for sharing.

    Translated by AI from 简体中文

    0
  4. keka Silver 5m ago

    Learned something new.

    Translated by AI from 简体中文

    0
  5. Bigpeach Bronze 4m ago

    The "I don't get it" series

    Translated by AI from 简体中文

    0
  6. w-d Diamond just now

    学习了

    0