A freshly provisioned VPS is exposed to the internet the moment it boots. Automated scanners begin probing SSH ports within minutes; brute-force attempts against root passwords start within hours. This guide covers 12 concrete hardening steps that transform a default Ubuntu server into a significantly more resilient system — applicable to any Hong Kong VPS or Linux server.
These steps are ordered by impact: the first four eliminate the most common attack vectors and should be completed before you do anything else with your server.
Step 1: Change Root Password and Create a Sudo User
# Set a strong root password immediately
passwd root
# Create a non-root admin user
adduser adminuser
usermod -aG sudo adminuser
# Verify sudo access
su - adminuser
sudo whoami # Should output: root
From this point forward, use adminuser for all operations. Reserve root for emergencies only.
Step 2: Set Up SSH Key Authentication
Password-based SSH authentication is the most common attack vector on public VPS. SSH keys eliminate it entirely.
# On your LOCAL machine (not the server):
ssh-keygen -t ed25519 -C "your-email@example.com" -f ~/.ssh/hk-vps
# Creates: ~/.ssh/hk-vps (private) and ~/.ssh/hk-vps.pub (public)
# Copy public key to server
ssh-copy-id -i ~/.ssh/hk-vps.pub adminuser@YOUR_VPS_IP
# Or manually:
cat ~/.ssh/hk-vps.pub | ssh adminuser@YOUR_VPS_IP "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Test that key login works before disabling passwords:
ssh -i ~/.ssh/hk-vps adminuser@YOUR_VPS_IP
Once confirmed, disable password authentication in /etc/ssh/sshd_config:
PasswordAuthentication no
PermitRootLogin no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
systemctl restart sshd
Critical: Keep your private key backed up securely. Losing it locks you out of your server unless you have console access via your VPS provider’s panel.
Step 3: Change the Default SSH Port
SSH on port 22 receives thousands of automated probes daily. Moving to a non-standard port eliminates most of this noise.
# In /etc/ssh/sshd_config, change:
Port 22222 # Choose any unused port between 1024–65535
# Allow the new port before restarting SSH
ufw allow 22222/tcp
systemctl restart sshd
Update your SSH config on your local machine:
# ~/.ssh/config
Host hk-vps
HostName YOUR_VPS_IP
Port 22222
User adminuser
IdentityFile ~/.ssh/hk-vps
Now connect with: ssh hk-vps
Step 4: Configure UFW Firewall
apt install ufw -y
# Default deny all incoming
ufw default deny incoming
ufw default allow outgoing
# Allow your services (adjust to your actual needs)
ufw allow 22222/tcp # SSH (new port)
ufw allow 80/tcp # HTTP
ufw allow 443/tcp # HTTPS
# Enable firewall
ufw enable
# Verify rules
ufw status verbose
Only open ports you actively use. Every open port is an attack surface. Database ports (3306 for MySQL, 5432 for PostgreSQL) should never be open to the internet — access them from your application on localhost.
Step 5: Install and Configure Fail2Ban
Fail2Ban monitors log files and automatically bans IPs that show signs of brute-force activity.
apt install fail2ban -y
cat > /etc/fail2ban/jail.local << 'EOF'
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 5
backend = systemd
[sshd]
enabled = true
port = 22222
logpath = %(sshd_log)s
[nginx-http-auth]
enabled = true
[nginx-limit-req]
enabled = true
EOF
systemctl enable --now fail2ban
fail2ban-client status
After a day of operation, check which IPs have been banned:
fail2ban-client status sshd
Step 6: Enable Automatic Security Updates
apt install unattended-upgrades -y
cat > /etc/apt/apt.conf.d/50unattended-upgrades << 'EOF' Unattended-Upgrade::Allowed-Origins { "${distro_id}:${distro_codename}-security"; }; Unattended-Upgrade::AutoFixInterruptedDpkg "true"; Unattended-Upgrade::MinimalSteps "true"; Unattended-Upgrade::Remove-Unused-Dependencies "true"; Unattended-Upgrade::Automatic-Reboot "false"; Unattended-Upgrade::Mail "admin@yourdomain.com"; EOF cat > /etc/apt/apt.conf.d/20auto-upgrades << 'EOF'
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
EOF
systemctl enable --now unattended-upgrades
Security patches are applied automatically. The Automatic-Reboot "false" setting queues kernel updates without forcing unplanned reboots — schedule maintenance windows for kernel upgrades manually.
Step 7: Secure Shared Memory
# Add to /etc/fstab to mount /run/shm with restricted permissions
echo "tmpfs /run/shm tmpfs defaults,noexec,nosuid 0 0" >> /etc/fstab
mount -o remount /run/shm
Shared memory is used by some attack techniques to escalate privileges. Mounting it with noexec,nosuid prevents execution of code stored there.
Step 8: Disable Unused Services
# List running services
systemctl list-units --type=service --state=running
# Common services to disable on a web server VPS:
systemctl disable --now avahi-daemon # mDNS (not needed on a server)
systemctl disable --now cups # Printing (not needed)
systemctl disable --now bluetooth # Bluetooth (not relevant on VPS)
# Check open ports and identify unknown listeners
ss -tlnp
Every running service that isn’t required for your application is a potential attack vector. Lean toward disabling anything you cannot explain.
Step 9: Configure Sysctl Security Parameters
cat >> /etc/sysctl.conf << 'EOF'
# IP spoofing protection
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# Ignore ICMP redirects
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
# Disable source packet routing
net.ipv4.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0
# Log martian packets
net.ipv4.conf.all.log_martians = 1
# Disable IPv6 if not used
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
# Protect against SYN flood attacks
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 5
EOF
sysctl -p
Step 10: Set Up File Integrity Monitoring with AIDE
AIDE (Advanced Intrusion Detection Environment) monitors critical system files for unexpected changes — alerting you if an attacker modifies system binaries or configuration files.
apt install aide -y
# Initialise the database (takes 5–10 minutes)
aideinit
cp /var/lib/aide/aide.db.new /var/lib/aide/aide.db
# Schedule daily check
crontab -l | { cat; echo "0 5 * * * aide --check | mail -s 'AIDE Report' admin@yourdomain.com"; } | crontab -
After a legitimate system update, reinitialise the AIDE database:
aideinit && cp /var/lib/aide/aide.db.new /var/lib/aide/aide.db
Step 11: Secure MySQL / PostgreSQL
MySQL
# Run the security wizard
mysql_secure_installation
# Answer: Y to all prompts — removes anonymous users, test database, remote root login
# Verify no remote access for root
mysql -u root -p -e "SELECT user, host FROM mysql.user WHERE user='root';"
# host should only show 'localhost', never '%'
# Bind MySQL to localhost only
echo "bind-address = 127.0.0.1" >> /etc/mysql/mysql.conf.d/mysqld.cnf
systemctl restart mysql
PostgreSQL
# Bind to localhost only
echo "listen_addresses = 'localhost'" >> /etc/postgresql/*/main/postgresql.conf
# Restrict pg_hba.conf to local connections only
# Ensure no entries with host 0.0.0.0/0
systemctl restart postgresql
Step 12: Enable Audit Logging
<code">apt install auditd audispd-plugins -y # Log all sudo commands cat >> /etc/audit/rules.d/audit.rules << 'EOF' -w /etc/passwd -p wa -k identity -w /etc/shadow -p wa -k identity -w /etc/sudoers -p wa -k sudoers -a always,exit -F arch=b64 -S execve -k exec EOF systemctl enable --now auditd # View recent audit events ausearch -ts recent | aureport --summary
Audit logs provide a forensic trail if your server is ever compromised — essential for understanding what happened and when.
Security Checklist Summary
| Step | Action | Priority |
|---|---|---|
| 1 | Create non-root sudo user | Critical |
| 2 | SSH key authentication, disable passwords | Critical |
| 3 | Change SSH port from 22 | High |
| 4 | Configure UFW firewall | Critical |
| 5 | Install Fail2Ban | High |
| 6 | Automatic security updates | High |
| 7 | Secure shared memory | Medium |
| 8 | Disable unused services | Medium |
| 9 | Sysctl network hardening | Medium |
| 10 | File integrity monitoring (AIDE) | Medium |
| 11 | Bind databases to localhost | Critical |
| 12 | Audit logging | Medium |
Ongoing Security Practices
- Review
auth.logweekly —grep "Failed password" /var/log/auth.log | tail -20shows recent attack attempts - Rotate SSH keys annually — generate new key pairs and update
authorized_keys - Monitor for new listening ports — run
ss -tlnpafter any software installation - Keep backups offsite — a compromised server may have encrypted backups destroyed by ransomware; offsite copies are your recovery path
- Review crontabs — attackers often add persistence via crontabs:
crontab -landcat /etc/cron.d/*
Conclusion
These 12 steps address the most common attack vectors against public-facing VPS instances — brute-force SSH, open databases, unpatched vulnerabilities, and privilege escalation. The first four steps (SSH keys, firewall, Fail2Ban, auto-updates) should be completed within the first 30 minutes of server provisioning on any new Hong Kong VPS.
Security is ongoing, not a one-time configuration. The monitoring and audit practices in the final section ensure that your hardened baseline stays intact as your application evolves.
Start with a secure foundation: Browse Server.HK Hong Kong VPS plans — all plans include KVM isolation and DDoS protection; the application-layer hardening above is your responsibility.