Linux 服务器初始化与安全加固指南
作者: CaoZH
日期: 2026-04-20
本文为原创教程
拿到一台全新的 Linux 服务器后,直接部署应用是不安全的。本文整理了一套标准化的初始化流程,覆盖用户管理、SSH 加固、防火墙、Fail2Ban、自动更新等关键环节。
一、初始登录
bash
# 使用 root 登录
ssh root@your-server-ip
# 立即更新系统
apt update && apt upgrade -y
# 或
yum update -y
# 查看系统信息
uname -a
cat /etc/os-release
free -h
df -h
二、创建普通用户
bash
# 创建部署用户
adduser deploy
# 按提示设置密码和其他信息
# 授予 sudo 权限
usermod -aG sudo deploy
# CentOS: usermod -aG wheel deploy
# 验证
su - deploy
sudo whoami
# 输出: root
三、SSH 安全加固
bash
# 编辑 SSH 配置
sudo vim /etc/ssh/sshd_config
conf
# 修改以下配置
Port 2222 # 修改默认端口(22 → 自定义)
PermitRootLogin no # 禁止 root 登录
PasswordAuthentication no # 禁止密码登录(使用密钥)
PubkeyAuthentication yes # 启用密钥认证
AllowUsers deploy # 只允许指定用户登录
MaxAuthTries 3 # 最大认证尝试次数
ClientAliveInterval 300 # 客户端超时(秒)
ClientAliveCountMax 2 # 超时重试次数
bash
# 复制本地公钥到服务器
ssh-copy-id -p 2222 deploy@your-server-ip
# 重启 SSH
sudo systemctl restart sshd
# ⚠️ 重启前务必在另一个终端验证能登录!
ssh -p 2222 deploy@your-server-ip
四、配置防火墙
bash
# 安装 UFW(Ubuntu)
sudo apt install -y ufw
# 设置默认策略
sudo ufw default deny incoming
sudo ufw default allow outgoing
# 开放 SSH(使用修改后的端口)
sudo ufw allow 2222/tcp comment 'SSH'
# 开放 Web 服务
sudo ufw allow 80/tcp comment 'HTTP'
sudo ufw allow 443/tcp comment 'HTTPS'
# 其他服务按需开放
sudo ufw allow 8080/tcp comment 'API'
# 启用防火墙
sudo ufw enable
# 查看状态
sudo ufw status verbose
bash
# CentOS 使用 firewalld
sudo firewall-cmd --permanent --add-port=2222/tcp
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
五、安装 Fail2Ban
bash
# 安装
sudo apt install -y fail2ban
# 复制配置
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
ini
# /etc/fail2ban/jail.local
[DEFAULT]
bantime = 3600 # 封禁时间(秒)
findtime = 600 # 检测窗口(秒)
maxretry = 5 # 最大重试次数
[sshd]
enabled = true
port = 2222 # 改为你的 SSH 端口
logpath = %(sshd_log)s
maxretry = 3
bantime = 86400 # SSH 暴力破解封禁 1 天
[nginx-http-auth]
enabled = true
port = http,https
logpath = /var/log/nginx/error.log
bash
# 启动
sudo systemctl start fail2ban
sudo systemctl enable fail2ban
# 查看状态
sudo fail2ban-client status
sudo fail2ban-client status sshd
六、自动安全更新
bash
# Ubuntu
sudo apt install -y unattended-upgrades
# 配置自动更新
sudo dpkg-reconfigure --priority=low unattended-upgrades
# 选择 Yes
# 查看配置
sudo cat /etc/apt/apt.conf.d/20auto-upgrades
bash
# CentOS
sudo yum install -y yum-cron
sudo systemctl start yum-cron
sudo systemctl enable yum-cron
七、安装常用工具
bash
# 监控
sudo apt install -y htop iotop net-tools sysstat
# 网络
sudo apt install -y curl wget netcat
# 编辑器
sudo apt install -y vim git
# Docker(如需要)
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker deploy
八、SWAP 配置
bash
# 检查是否有 SWAP
swapon --show
# 创建 2GB SWAP
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# 永久生效
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# 配置 SWAP 使用策略
# /etc/sysctl.conf
vm.swappiness=10
九、日志与审计
bash
# 查看登录日志
sudo last
sudo lastb # 失败的登录尝试
# 查看系统日志
sudo journalctl -xe
sudo journalctl -u sshd -n 50
# 查看认证日志
sudo tail -f /var/log/auth.log
十、一键初始化脚本
bash
#!/bin/bash
# server-init.sh --- 一键初始化脚本
set -e
SSH_PORT=${1:-2222}
DEPLOY_USER=${2:-deploy}
echo "🚀 开始服务器初始化..."
# 1. 更新系统
apt update && apt upgrade -y
# 2. 创建用户
adduser --gecos "" $DEPLOY_USER
usermod -aG sudo $DEPLOY_USER
# 3. SSH 加固
sed -i "s/#Port 22/Port $SSH_PORT/" /etc/ssh/sshd_config
sed -i "s/PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config
sed -i "s/#PasswordAuthentication yes/PasswordAuthentication no/" /etc/ssh/sshd_config
systemctl restart sshd
# 4. 防火墙
apt install -y ufw
ufw default deny incoming
ufw default allow outgoing
ufw allow $SSH_PORT/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw --force enable
# 5. Fail2Ban
apt install -y fail2ban
systemctl enable --now fail2ban
# 6. 常用工具
apt install -y htop curl wget git vim unattended-upgrades
# 7. SWAP
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' >> /etc/fstab
echo "✅ 初始化完成!"
echo "请使用新端口登录:ssh -p $SSH_PORT $DEPLOY_USER@<服务器IP>"
十一、总结
markdown
## 安全加固清单
□ 创建普通用户,禁止 root 登录
□ 修改 SSH 端口(22 → 自定义)
□ 仅允许密钥登录
□ 配置防火墙(UFW / firewalld)
□ 安装 Fail2Ban 防暴力破解
□ 启用自动安全更新
□ 配置 SWAP
□ 安装监控工具
□ 定期查看日志
首发于 CaoZH 的笔记