Appearance
Linux服务器初始化与安全加固运维笔记
🔄 第一阶段:系统初始化
1.1 系统更新
bash
# 刷新软件源列表
sudo apt update
# 升级所有可升级的软件包
sudo apt upgrade -y
# (可选) 深度升级,处理依赖关系变更
sudo apt full-upgrade -y
# 清理无用依赖包
sudo apt autoremove -y1.2 安装基础运维工具
bash
# 一键安装所有核心工具
sudo apt update && sudo apt install -y htop nmon curl wget ufw fail2ban| 工具 | 用途 | 关键命令 |
|---|---|---|
| htop | 交互式进程监控 | htop → 按F10退出 |
| nmon | 系统性能监控 | nmon → 按q退出 |
| curl | 网络传输测试 | curl -I http://example.com |
| wget | 文件下载 | wget https://example.com/file.zip |
🛡️ 第二阶段:安全加固
2.1 防火墙 (UFW) 配置
⚠️ 操作顺序至关重要:先放行SSH,再启用防火墙
bash
# 1. 放行当前SSH端口(防止被锁)
sudo ufw allow 22/tcp
# 2. 启用防火墙
sudo ufw enable
# 输入 y 确认
# 3. 验证状态
sudo ufw status verbose
# 应显示 Status: active 和 22/tcp ALLOW IN2.2 防暴力破解 (Fail2ban) 配置
bash
# 1. 启动并设为开机自启
sudo systemctl enable --now fail2ban
# 2. 检查运行状态
sudo systemctl status fail2ban
# 应显示 Active: active (running)
# 3. 查看SSH保护状态
sudo fail2ban-client status sshd
# 查看被封IP列表和失败尝试次数🔑 第三阶段:SSH密钥认证强化
3.1 本地生成密钥对(在个人电脑操作)
bash
# 生成Ed25519算法密钥(更安全快速)
ssh-keygen -t ed25519 -C "your_email@example.com"
# 按提示操作:
# 1. 保存路径:直接回车用默认位置
# 2. 设置密钥密码:强烈建议设置强密码生成文件说明:
~/.ssh/id_ed25519→ 私钥(绝密!绝不分享)~/.ssh/id_ed25519.pub→ 公钥(上传到服务器)
3.2 上传公钥到服务器
bash
# 方法1:使用ssh-copy-id(Linux/macOS)
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@服务器IP
# 方法2:Windows手动上传(PowerShell)
# 复制公钥文件到服务器
scp "C:\Users\用户名\.ssh\aliyun_server.pub" root@服务器IP:~/.ssh/
# 登录服务器后执行:
mkdir -p ~/.ssh
cat ~/.ssh/aliyun_server.pub >> ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys3.3 测试密钥登录(关键验证步骤)
bash
# 1. 新开终端窗口测试密钥登录
ssh -i "私钥路径" root@服务器IP
# 应直接登录或提示输入密钥密码
# 2. 测试密码登录应被拒绝
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no root@服务器IP
# 应显示 Permission denied (publickey)3.4 禁用SSH密码登录(最终加固)
⚠️ 确保密钥登录测试成功后再执行!
bash
# 1. 备份原始配置
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup
# 2. 编辑配置文件
sudo nano /etc/ssh/sshd_config
# 3. 修改以下关键参数:
PermitRootLogin prohibit-password # 禁止root密码登录
PasswordAuthentication no # 禁用所有密码认证
PubkeyAuthentication yes # 启用公钥认证
# 4. 保存并重载SSH服务(不断开当前连接)
sudo systemctl reload ssh
# 5. 验证配置生效
sudo sshd -T | grep -E "passwordauthentication|permitrootlogin"
# 应输出:
# passwordauthentication no
# permitrootlogin prohibit-password🎯 第四阶段:验证与维护
4.1 最终状态检查清单
sudo ufw status verbose→ 防火墙活跃,22端口放行sudo systemctl status fail2ban→ 服务运行正常ssh root@服务器IP→ 密钥登录成功ssh -o PreferredAuthentications=password ...→ 密码登录被拒sudo sshd -T | grep passwordauthentication→ 返回no
4.2 日常监控命令
bash
# 查看系统资源
htop
nmon
# 检查安全状态
sudo ufw status
sudo fail2ban-client status sshd
sudo tail -f /var/log/auth.log # 查看认证日志
# 查看系统关键信息
uname -r # 内核版本
df -h # 磁盘使用
free -h # 内存使用4.3 故障恢复指南
| 问题 | 解决方案 |
|---|---|
| 密钥丢失 | 使用备份的公钥重新添加,或通过云控制台VNC恢复 |
| 误封自己IP | sudo fail2ban-client set sshd unbanip 你的IP |
| SSH配置错误 | sudo cp /etc/ssh/sshd_config.backup /etc/ssh/sshd_config 还原备份 |
| 防火墙锁死 | 通过云控制台VNC登录,执行 sudo ufw disable |
📝 核心运维原则
- 最小权限原则:如非必要,不使用root用户
- 配置前先备份:修改重要文件前务必备份
- 测试再应用:服务配置修改后,先测试再切换
- 密钥管理:私钥如同保险箱密码,必须妥善保管
- 日志审查:定期检查
/var/log/auth.log、/var/log/ufw.log
✅ 完成状态
至此,你的Linux服务器已具备:
- ✅ 最新的系统环境
- ✅ 实时资源监控能力
- ✅ 网络防火墙保护
- ✅ 自动防暴力破解
- ✅ 强制的SSH密钥认证
- ✅ 完整的配置备份
安全警告:私钥文件是访问服务器的唯一凭证,务必:
- 设置强密码保护私钥
- 在多设备间安全传输
- 定期备份到加密存储
- 不在不安全网络中使用