Sau khi mở VPS mới, IP công khai thường bị quét cổng và dò brute-force SSH chỉ trong vài phút. Dưới đây là quy trình khởi tạo cơ bản áp dụng cho Debian 11/12, Ubuntu 22.04/24.04, làm lần lượt khoảng 10 phút là xong.
⚠️ Trước khi thao tác, nhớ xác nhận là bạn có thể đăng nhập qua VNC / console của nhà cung cấp, phòng trường hợp cấu hình SSH bị sai vẫn còn đường cứu.
1. Cập nhật hệ thống
apt update && apt upgrade -y
2. Tạo user thường và cấp quyền sudo
adduser deploy
usermod -aG sudo deploy
deploy có thể đổi thành username bạn muốn.
3. Cấu hình đăng nhập SSH bằng key
Tạo key trên máy local (nếu có rồi thì bỏ qua):
ssh-keygen -t ed25519 -C "my-vps"
Upload public key lên VPS:
ssh-copy-id deploy@你的IP
Nếu Windows không có ssh-copy-id, có thể mở file ~/.ssh/id_ed25519.pub trên máy local rồi nối nội dung vào /home/deploy/.ssh/authorized_keys trên VPS.
👉 Mở một terminal mới, xác nhận đăng nhập bằng key được rồi hãy làm bước tiếp theo.
4. Tắt đăng nhập bằng mật khẩu và đăng nhập root trực tiếp
sudo nano /etc/ssh/sshd_config
Sửa / xác nhận các mục sau:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
Lưu ý: một số hệ thống còn có file cấu hình trong /etc/ssh/sshd_config.d/ (ví dụ 50-cloud-init.conf của cloud image), có thể ghi đè các thiết lập trên, nhớ kiểm tra luôn:
sudo grep -r "PasswordAuthentication" /etc/ssh/sshd_config.d/
Kiểm tra cú pháp rồi restart SSH (tên service trên Debian/Ubuntu là ssh):
sudo sshd -t && sudo systemctl restart ssh
5. Bật firewall 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. Cài Fail2ban chống 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 là để tương thích với các hệ thống như Debian 12, mặc định không tạo /var/log/auth.log.
7. Bật BBR (tùy chọn, cải thiện TCP throughput cho tuyến xuyên biên giới / độ trễ cao)
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
Output ra bbr là thành công. Cần kernel 4.9+ (Debian 10+ / Ubuntu 18.04+ mặc định đáp ứng), và phải là ảo hóa đầy đủ như KVM, VMware; container OpenVZ / LXC thường không tự sửa được.
8. Timezone và tự động cập nhật bảo mật (tùy chọn)
sudo timedatectl set-timezone Asia/Shanghai
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure -plow unattended-upgrades
✅ Checklist
- Hệ thống đã được cập nhật
- Đã tạo user thường và thêm vào sudo
- Đăng nhập bằng key ổn, đã tắt đăng nhập bằng mật khẩu và đăng nhập root
- UFW đã bật, cổng SSH đã được mở
- Fail2ban chạy bình thường
- BBR đã bật (tùy chọn)
Các bước trên dùng được trực tiếp trên VPS Việt Nam của TotHost (TOT K dùng kiến trúc KVM, TOT M dùng kiến trúc VMware). Muốn test tuyến trước thì có thể dùng Looking Glass của bên mình: https://lg.tothost.vn/ , có vấn đề gì cứ trao đổi ở phần comment hoặc group Telegram 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 交流。
Dịch bằng AI từ 简体中文Bản gốc (简体中文)
快银 VPN,一键连接全球节点,线路稳定、延迟低。支持 Windows、macOS 等多平台,TUN 全局模式覆盖全部应用流量。无需复杂配置,下载即用。
6 phản hồi
Sắp xếp