新しい 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 は好きなユーザー名に変えてOKです。
3. SSH 鍵ログインを設定
ローカル PC で鍵を生成(すでにあるならスキップ):
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 に手動で追記すればOKです。
👉 先に新しいターミナルを開いて、鍵でログインできることを確認してから次のステップへ。
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 でどうぞ。
新 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 交流。
AI が简体中文から翻訳原文(简体中文)です
返信 6 件
並び順