引言
- 背景:新购买的 Linux 服务器需要大量基础配置,新手容易遗漏关键步骤
- 核心价值:系统化配置清单,确保服务器安全、稳定、高效运行
- 适用场景:新服务器部署、系统重装后配置、云服务器初始化
前置条件
- 系统要求
- Ubuntu/Debian/CentOS 等主流发行版
- root 或 sudo 权限
- 工具准备
- 终端访问权限(SSH)
- 文本编辑器(vim/nano)
- 网络连接正常
配置清单
1️⃣ 系统基础配置
1.1 设置主机名
# 查看当前主机名
hostname
# 修改主机名
sudo hostnamectl set-hostname server01
# 更新 /etc/hosts 文件
sudo sed -i 's/127.0.1.1 .* /127.0.1.1 server01/' /etc/hosts
1.2 更新系统
# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y
# CentOS/RHEL
sudo dnf update -y
# 或
sudo yum update -y
1.3 时区和日期
# 设置时区为上海
sudo timedatectl set-timezone Asia/Shanghai
# 查看时间状态
timedatectl status
# 设置 NTP 时间同步(可选)
sudo timedatectl set-ntp true
1.4 禁用密码 root 登录(安全)
# 编辑 sshd 配置
sudo vim /etc/ssh/sshd_config
# 修改或添加:
# PermitRootLogin prohibit-password
# PasswordAuthentication no
重启 SSH 服务生效:
sudo systemctl restart sshd
2️⃣ 防火墙配置
2.1 UFW(Ubuntu)
# 查看当前防火墙状态
sudo ufw status
# 启用防火墙
sudo ufw enable
# 允许常用端口
sudo ufw allow 22/tcp # SSH
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
sudo ufw allow 8080/tcp # 应用端口
# 允许所有 IPv4/IPv6 流量(可选)
sudo ufw allow ipv4
sudo ufw allow ipv6
2.2 firewalld(CentOS)
# 查看状态
sudo firewall-cmd --state
# 启用防火墙
sudo systemctl enable --now firewalld
# 开放端口
sudo firewall-cmd --permanent --add-port=22/tcp
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=443/tcp
sudo firewall-cmd --reload
3️⃣ SSH 安全加固
3.1 修改 SSH 端口(可选)
# 编辑配置
sudo vim /etc/ssh/sshd_config
# 修改:
# Port 2222 # 改为自定义端口
# PermitRootLogin no
# PasswordAuthentication no
# PubkeyAuthentication yes
# 重启服务
sudo systemctl restart sshd
3.2 配置密钥登录
# 生成 SSH 密钥对(如果还没有)
ssh-keygen -t ed25519 -C "your_email@example.com"
# 复制公钥到服务器
ssh-copy-id user@server_ip
# 查看已登录的密钥
cat ~/.ssh/id_ed25519.pub
3.3 安装 Fail2Ban
# Ubuntu/Debian
sudo apt install fail2ban -y
# CentOS/RHEL
sudo dnf install fail2ban -y
# 启用并启动
sudo systemctl enable --now fail2ban
# 查看状态
sudo systemctl status fail2ban
4️⃣ 用户和权限管理
4.1 创建普通用户
# 创建新用户
sudo adduser newuser
# 设置密码
sudo passwd newuser
# 添加到 sudo 组
sudo usermod -aG sudo newuser # Ubuntu/Debian
# 或
sudo usermod -aG wheel newuser # CentOS
4.2 配置 sudo 权限
# 编辑 sudoers 文件(安全方式)
sudo visudo
# 添加:
# %sudo ALL=(ALL:ALL) ALL
# newuser ALL=(ALL) ALL # 允许特定用户 sudo
4.3 文件权限检查
# 查看关键文件权限
ls -la /etc/
ls -la /root/
ls -la /var/log/
# 修复权限(如需要)
sudo chmod 755 /path/to/directory
sudo chmod 644 /path/to/file
5️⃣ 软件源优化
5.1 Ubuntu/Debian 更换国内源
# 使用一键脚本(推荐)
sudo bash <(curl -sSL https://linuxmirrors.cn/main.sh)
# 或手动编辑
sudo vim /etc/apt/sources.list
5.2 CentOS 更换阿里云源
# 备份原配置
sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# 使用阿里云脚本
sudo yum install -y yum-utils
sudo yum-config-manager --add-mirror https://mirrors.aliyun.com/centos/$basearch/os/
# 或手动编辑
sudo vim /etc/yum.repos.d/CentOS-Base.repo
6️⃣ 日志系统配置
6.1 配置日志轮转
# Ubuntu/Debian
sudo vim /etc/logrotate.conf
# CentOS/RHEL
sudo vim /etc/logrotate.conf
# 常见配置:
# /var/log/*.log {
# daily
# rotate 7
# compress
# delaycompress
# missingok
# notifempty
# copytruncate
# }
6.2 安装日志分析工具
# 安装多行日志查看
sudo apt install mtr -y
sudo apt install htop -y
sudo apt install ncdu -y
# 或 CentOS
sudo dnf install mtr htop ncdu -y
7️⃣ 系统监控工具
7.1 安装监控工具
# 安装常用工具
sudo apt install vim git curl wget htop net-tools -y
# 或
sudo dnf install vim git curl wget htop net-tools -y
7.2 配置自动更新(可选)
# Ubuntu/Debian:禁用自动升级(更安全)
sudo vim /etc/apt/apt.conf.d/01autoremove
# 添加:
# APT::AutoRemove::RecommendsImportant false
8️⃣ 备份配置
8.1 配置自动备份脚本
# 创建备份目录
sudo mkdir -p /backup
# 创建定时备份脚本
sudo vim /usr/local/bin/daily_backup.sh
# 添加内容:
#!/bin/bash
DATE=$(date +%Y%m%d_%H%M%S)
tar -czf /backup/system_backup_$DATE.tar.gz /etc /home /var/log
8.2 配置 cron 任务
# 编辑 cron
sudo crontab -e
# 添加每日备份任务:
# 0 2 * * * /usr/local/bin/daily_backup.sh
验证清单
✅ 基础功能验证
# 1. 主机名
hostname
# 2. 时间同步
timedatectl status
# 3. 网络连通性
ping -c 4 www.baidu.com
# 4. DNS 解析
nslookup www.google.com
# 5. 防火墙状态
sudo ufw status # Ubuntu
sudo firewall-cmd --state # CentOS
# 6. SSH 服务
sudo systemctl status sshd
# 7. 磁盘空间
df -h
# 8. 内存使用
free -h
# 9. 系统负载
uptime
✅ 安全配置验证
# 1. SSH 配置
grep "Port " /etc/ssh/sshd_config
grep "PermitRootLogin" /etc/ssh/sshd_config
# 2. Fail2Ban 状态
sudo systemctl status fail2ban
# 3. 登录日志
tail -20 /var/log/auth.log # Ubuntu
tail -20 /var/log/secure # CentOS
常见问题
Q1:SSH 连接失败怎么办?
✔ 检查 SSH 服务:
sudo systemctl status sshd
✔ 检查防火墙:
sudo ufw status # Ubuntu
sudo firewall-cmd --list-ports # CentOS
✔ 查看日志:
sudo journalctl -xe # Ubuntu/Debian
sudo tail -f /var/log/secure # CentOS
Q2:无法更新系统?
✔ 检查软件源:
# Ubuntu/Debian
ls /etc/apt/sources.list.d/
# CentOS
ls /etc/yum.repos.d/
✔ 清理缓存:
sudo apt clean # Ubuntu
sudo dnf clean all # CentOS
Q3:忘记 root 密码?
✔ 重启进入 recovery mode:
# 按 Shift 进入 GRUB,选择 recovery mode
# 选择 "root" 选项
# 在 root shell 中:
passwd root
Q4:防火墙阻止了连接?
✔ 检查端口开放:
sudo ufw allow 22/tcp # Ubuntu
sudo firewall-cmd --add-port=22/tcp --permanent # CentOS
✔ 临时关闭防火墙测试:
sudo ufw disable # Ubuntu(谨慎使用)
sudo systemctl stop firewalld # CentOS
推荐资源
- Ubuntu 官方文档 :ubuntu.com/docs
- CentOS 官方文档 :docs.centos.org
- Linux 安全指南 :LinuxSecurity.org
- Fail2Ban 配置示例 :github.com/fail2ban/fail2ban
- 服务器初始化脚本 :github.com/bin456789
结语
- 配置清单建议保存为模板,每次部署时参考
- 重要操作前务必备份当前配置
- 定期更新系统和安全补丁
- 如果有什么问题,欢迎评论留言讨论,看到会回复
- 如果你觉得这个回答对你有帮助,欢迎关注我【一行梦境】,我会在那里分享更多深度内容和实用技巧。