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
新 VPS 开通后,公网 IP 往往几分钟内就会被扫描、尝试爆破 SSH。下面是一套适用于 Debian 11/12、Ubuntu 22.04/24.04 的基础初始化流程,按顺序做完大约 10 分钟。
⚠️ 操作前请确认可以通过服务商的 VNC / 控制台登录,万一 SSH 配置出错还能救回来。
1. 更新系统
apt update && apt upgrade -y
2. 创建普通用户并授予 sudo
adduser deploy
usermod -aG sudo deploy
deploy 可以换成你自己的用户名。
3. 配置 SSH 密钥登录
在本地电脑生成密钥(已有可跳过):
ssh-keygen -t ed25519 -C "my-vps"
把公钥上传到 VPS:
ssh-copy-id deploy@你的IP
Windows 没有 ssh-copy-id 的话,可以手动把本地 ~/.ssh/id_ed25519.pub 的内容追加到 VPS 上的 /home/deploy/.ssh/authorized_keys。
👉 先新开一个终端,确认能用密钥登录,再进行下一步。
4. 关闭密码登录和 root 直接登录
sudo nano /etc/ssh/sshd_config
修改 / 确认以下几项:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
注意:部分系统在 /etc/ssh/sshd_config.d/ 下还有配置文件(例如云镜像的 50-cloud-init.conf),可能会覆盖上面的设置,记得一起检查:
sudo grep -r "PasswordAuthentication" /etc/ssh/sshd_config.d/
检查语法并重启 SSH(Debian/Ubuntu 的服务名是 ssh):
sudo sshd -t && sudo systemctl restart ssh
5. 启用防火墙 UFW
sudo apt install ufw -y
sudo ufw allow 22/tcp # 如果改过 SSH 端口,这里换成新端口
sudo ufw allow 80,443/tcp # 需要建站时再开
sudo ufw enable
sudo ufw status
6. 安装 Fail2ban 防爆破
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 是为了兼容 Debian 12 这类默认不生成 /var/log/auth.log 的系统。
7. 开启 BBR(可选,改善跨境 / 高延迟线路的 TCP 吞吐)
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
输出 bbr 即成功。需要内核 4.9+(Debian 10+ / Ubuntu 18.04+ 默认满足),并且是 KVM、VMware 等完整虚拟化,OpenVZ / LXC 容器通常无法自行修改。
8. 时区与自动安全更新(可选)
sudo timedatectl set-timezone Asia/Shanghai
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure -plow unattended-upgrades
✅ 检查清单
- 系统已更新
- 已创建普通用户并加入 sudo
- 密钥登录正常,已禁用密码登录与 root 登录
- UFW 已启用,SSH 端口已放行
- Fail2ban 正常运行
- BBR 已开启(可选)
以上步骤在 TotHost 越南 VPS(TOT K 为 KVM 架构、TOT M 为 VMware 架构)上都可以直接使用。想先测试线路可以用我们的 Looking Glass:https://lg.tothost.vn/ ,有问题欢迎在评论区或 Telegram 群 https://t.me/tothostvietnam 交流。
Translated by AI from 简体中文Original (简体中文)
可以共享、可以push的免费邮箱,更可使用自定义域名,完美契合您的各项需求。
6 replies
Sort order