423 字
2 分钟
安全防护
2026-04-29
以debian 12为例演示。低于2c2g的vps,用root用户,否则用普通用户;这里演示以以下用户名密码为例 用户名:woioeow 密码:4561834
更新系统
apt update && apt upgrade -y新建用户
# 新建用户同时新建用户目录、指定默认shelluseradd -m -s /usr/bin/bash woioeow设置密码
passwd woioeow设置sudo密码权限
有的精简系统默认无sudo和vim,以下命令安装
Terminal window apt install sudo vim -y
vim /etc/sudoers找到root ALL=(ALL:ALL) ALL这行,粘贴至下一行,把root改为woioeow,如下:
root ALL=(ALL:ALL) ALLwoioeow ALL=(ALL:ALL) ALL输入:x!保存退出
使用su woioeow可切换至新用户
实现ssh密钥登陆
客户端
ssh-keygen -t ed25519 -C "sshkey"连续按三次回车
上传密钥
# 如果没有改ssh端口可不指定-P(大写)参数# 先在服务器创建.ssh目录scp -P <port> C:\Users\<username>\.ssh\id_ed25519.pub <用户名>@<服务器IP>:~/.ssh/authorized_keys服务端
mkdir .ssh# 上传密钥后chmod 600 .ssh/authorized_keysUFW防火墙配置
sudo apt install ufw -y# 启用防火墙sudo ufw enable# 开放的ssh端口,ipv4、limit、tcpsudo ufw limit from 0.0.0.0/0 to any port 62768 proto tcp# 载入规则sudo ufw reload禁用Ping
sudo vim /etc/ufw/before.rules搜echo-request,找到有input的那行,把ACCEPT改为DROP
sudo ufw reloadFail2ban
sudo apt install fail2ban -y# 复制文件cd /etc/fail2bansudo cp fail2ban.conf fail2ban.local# 追加配置sudo cat<<EOF >> fail2ban.local[sshd]enabled = trueport = 62768filter = sshdlogpath = /var/log/auth.logbackend = auto
maxretry = 3findtime = 10mbantime = 7d
bantime.increment = truebantime.factor = 2bantime.maxtime = 30dEOF# 创建日志文件sudo touch /var/log/auth.log# 重启sudo systemctl enable fail2bansudo systemctl start fail2bansudo systemctl status fail2ban修改日志体积上限
sudo vim /etc/systemd/journald.conf修改以下参数SystemMaxUse=200MSystemKeepFree=500MMaxRetentionSec=7day重启服务sudo systemctl restart systemd-journaldsudo systemctl status systemd-journald