正在加载...
423 字
2 分钟
安全防护
2026-04-29

以debian 12为例演示。低于2c2g的vps,用root用户,否则用普通用户;这里演示以以下用户名密码为例 用户名:woioeow 密码:4561834

更新系统#

Terminal window
apt update && apt upgrade -y

新建用户#

Terminal window
# 新建用户同时新建用户目录、指定默认shell
useradd -m -s /usr/bin/bash woioeow

设置密码#

Terminal window
passwd woioeow

设置sudo密码权限#

有的精简系统默认无sudo和vim,以下命令安装

Terminal window
apt install sudo vim -y
Terminal window
vim /etc/sudoers

找到root ALL=(ALL:ALL) ALL这行,粘贴至下一行,把root改为woioeow,如下:

Terminal window
root ALL=(ALL:ALL) ALL
woioeow ALL=(ALL:ALL) ALL

输入:x!保存退出

使用su woioeow可切换至新用户

实现ssh密钥登陆#

客户端#

Terminal window
ssh-keygen -t ed25519 -C "sshkey"

连续按三次回车

上传密钥#

Terminal window
# 如果没有改ssh端口可不指定-P(大写)参数
# 先在服务器创建.ssh目录
scp -P <port> C:\Users\<username>\.ssh\id_ed25519.pub <用户名>@<服务器IP>:~/.ssh/authorized_keys

服务端#

Terminal window
mkdir .ssh
# 上传密钥后
chmod 600 .ssh/authorized_keys

UFW防火墙配置#

Terminal window
sudo apt install ufw -y
# 启用防火墙
sudo ufw enable
# 开放的ssh端口,ipv4、limit、tcp
sudo ufw limit from 0.0.0.0/0 to any port 62768 proto tcp
# 载入规则
sudo ufw reload

禁用Ping#

Terminal window
sudo vim /etc/ufw/before.rules

echo-request,找到有input的那行,把ACCEPT改为DROP

Terminal window
sudo ufw reload

Fail2ban#

Terminal window
sudo apt install fail2ban -y
# 复制文件
cd /etc/fail2ban
sudo cp fail2ban.conf fail2ban.local
# 追加配置
sudo cat<<EOF >> fail2ban.local
[sshd]
enabled = true
port = 62768
filter = sshd
logpath = /var/log/auth.log
backend = auto
maxretry = 3
findtime = 10m
bantime = 7d
bantime.increment = true
bantime.factor = 2
bantime.maxtime = 30d
EOF
# 创建日志文件
sudo touch /var/log/auth.log
# 重启
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
sudo systemctl status fail2ban

修改日志体积上限#

Terminal window
sudo vim /etc/systemd/journald.conf
修改以下参数
SystemMaxUse=200M
SystemKeepFree=500M
MaxRetentionSec=7day
重启服务
sudo systemctl restart systemd-journald
sudo systemctl status systemd-journald
安全防护
https://blog.vian.im/安全防护/
作者
woioeow
发布于
2026-04-29
许可协议
CC BY-NC-SA 4.0