NewCode CLOUD — 20% off your first month on all cloud VPSClaim

Advanced SSH Hardening on Hong Kong VPS: 2FA, Certificates, and Audit (2026)

· 3 min read

Your Hong Kong VPS receives thousands of automated SSH attack attempts daily. Disabling password auth and changing the port is baseline hygiene — advanced hardening adds certificate-based authentication (no key distribution needed), TOTP two-factor, connection rate limiting at the kernel level, and complete audit trails of every SSH session.


Layer 1: Certificate-Based SSH (vs Key-Based)

<code"># SSH certificates are signed by a CA — revocation is instant by updating one file
# No need to distribute individual public keys to each server

# Create a CA (keep this offline and secure)
ssh-keygen -t ed25519 -f /root/ssh-ca -C "HK VPS SSH CA 2026"
# Creates: ssh-ca (private, KEEP SECRET) and ssh-ca.pub

# Sign a user's public key (issue a certificate)
ssh-keygen -s /root/ssh-ca \
  -I "alice@company" \        # Certificate identity (for audit logs)
  -n "deploy,alice" \         # Valid principals (usernames)
  -V "+52w" \                 # Valid for 52 weeks
  ~/.ssh/id_ed25519.pub       # User's existing public key
# Creates: id_ed25519-cert.pub

# On the VPS: trust the CA
echo "TrustedUserCAKeys /etc/ssh/ssh-ca.pub" >> /etc/ssh/sshd_config
cp ssh-ca.pub /etc/ssh/ssh-ca.pub
systemctl reload sshd

# Revoke a user: add their cert's serial to revoked keys list
ssh-keygen -k -f /etc/ssh/revoked-keys -s /root/ssh-ca -z SERIAL_NUMBER
echo "RevokedKeys /etc/ssh/revoked-keys" >> /etc/ssh/sshd_config

Layer 2: TOTP Two-Factor with PAM

<code">apt install -y libpam-google-authenticator

# Configure PAM for SSH
cat >> /etc/pam.d/sshd << 'EOF'
auth required pam_google_authenticator.so
EOF

# In /etc/ssh/sshd_config:
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive

systemctl reload sshd

# Each user sets up TOTP:
google-authenticator
# Scan the QR code with Google Authenticator or Authy
# Login now requires: SSH key + TOTP code

Layer 3: Hardened sshd_config

<code">cat > /etc/ssh/sshd_config.d/hardening.conf << 'EOF'
# Custom port (reduces automated scan noise)
Port 22222

# Protocol and cipher hardening
Protocol 2
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org
HostKeyAlgorithms ssh-ed25519,rsa-sha2-512,rsa-sha2-256
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com

# Authentication
PasswordAuthentication no
PermitRootLogin prohibit-password
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

# Limits
MaxAuthTries 3
MaxSessions 5
LoginGraceTime 20
MaxStartups 10:30:60   # 10 unauthenticated connections, then 30% chance of drop, hard limit 60

# Session hardening
X11Forwarding no
AllowAgentForwarding no
AllowTcpForwarding no
PermitTunnel no
PrintMotd no

# Idle disconnect
ClientAliveInterval 300
ClientAliveCountMax 2

# Restrict users
AllowUsers deploy alice bob
# or AllowGroups ssh-users
EOF

sshd -t && systemctl reload sshd

Layer 4: Kernel-Level Rate Limiting

<code"># Block IPs that attempt more than 5 connections in 60 seconds
iptables -A INPUT -p tcp --dport 22222 -m state --state NEW \
  -m recent --set --name SSH
iptables -A INPUT -p tcp --dport 22222 -m state --state NEW \
  -m recent --update --seconds 60 --hitcount 5 --name SSH -j DROP

# Make persistent
apt install -y iptables-persistent
iptables-save > /etc/iptables/rules.v4

Layer 5: Full Session Audit Logging

<code"># Log all SSH session commands to a tamper-evident audit file
apt install -y auditd audispd-plugins

# Audit SSH logins and commands
cat >> /etc/audit/rules.d/ssh.rules << 'EOF'
# Watch SSH authorised keys changes
-w /etc/ssh/authorized_keys -p wa -k ssh_keys
-w /root/.ssh/authorized_keys -p wa -k ssh_keys

# Log all executed commands
-a exit,always -F arch=b64 -S execve -k commands
EOF

systemctl enable --now auditd

# View SSH events
ausearch -k ssh_keys
ausearch -k commands --start today | aureport -x --summary

Layer 6: Port Knocking (Optional)

<code">apt install -y knockd

cat > /etc/knockd.conf << 'EOF'
[options]
    interface = eth0
    logfile = /var/log/knockd.log

[openSSH]
    sequence    = 7000,8000,9000
    seq_timeout = 10
    command     = /sbin/iptables -A INPUT -s %IP% -p tcp --dport 22222 -j ACCEPT
    tcpflags    = syn

[closeSSH]
    sequence    = 9000,8000,7000
    seq_timeout = 10
    command     = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 22222 -j ACCEPT
    tcpflags    = syn
EOF

systemctl enable --now knockd

# Connect: knock first, then SSH
knock YOUR_VPS_IP 7000 8000 9000
ssh -p 22222 deploy@YOUR_VPS_IP
knock YOUR_VPS_IP 9000 8000 7000  # Close when done

SSH Security Checklist

Control Status
Password auth disabled PasswordAuthentication no
Root login restricted PermitRootLogin prohibit-password
Custom port Port 22222
MaxAuthTries 3 Blocks brute force
Idle timeout ClientAliveInterval 300
X11/TCP forwarding off Prevents tunnelling abuse
Fail2Ban Bans repeat failures
Audit logging Every session recorded

Conclusion

Advanced SSH hardening on a Hong Kong VPS — certificate-based auth, TOTP two-factor, hardened cipher suites, kernel-level rate limiting, and audit logging — layers multiple independent controls so that no single bypass gives an attacker access. The combination makes automated attacks futile (rate limits + no passwords), credential theft less dangerous (2FA required), and any successful breach fully audited. These controls take under an hour to implement and require minimal ongoing maintenance.

Secure your server: Browse Server.HK Hong Kong VPS plans — apply this hardening guide immediately after provisioning any new VPS before exposing it to production traffic.

Related articles

More in VPS & ServersServer setup guides