Ubuntu 24.04 LTS 服务器初始化配置指南
一、概述
Ubuntu 24.04 LTS(Noble Numbat)于 2024 年 4 月发布,提供 5 年标准支持和 12 年扩展安全维护。本文档提供生产环境服务器的标准化初始化配置流程。
二、初始登录与基础检查
2.1 首次登录
1 2 3 4 5
| ssh username@server_ip
ssh root@server_ip
|
2.2 系统信息检查
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| cat /etc/os-release lsb_release -a
uname -r
lscpu free -h df -h lsblk
ip addr show ip route show
|
2.3 创建普通用户(如使用 root 登录)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| adduser admin
usermod -aG sudo admin
mkdir -p /home/admin/.ssh cp -r /root/.ssh/* /home/admin/.ssh/ chown -R admin:admin /home/admin/.ssh chmod 700 /home/admin/.ssh chmod 600 /home/admin/.ssh/authorized_keys
su - admin
|
三、SSH 安全加固
3.1 配置 SSH 密钥认证
1 2 3 4 5 6 7 8
| ssh-keygen -t ed25519 -C "admin@ubuntu2404"
ssh-copy-id admin@server_ip
cat ~/.ssh/id_ed25519.pub | ssh admin@server_ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
|
3.2 修改 SSH 配置
1
| sudo vim /etc/ssh/sshd_config
|
关键配置项:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| # 禁用 root 登录 PermitRootLogin no
# 仅允许密钥认证 PasswordAuthentication no PubkeyAuthentication yes AuthenticationMethods publickey
# 修改默认端口(可选,避免自动化扫描) Port 2222
# 限制登录用户 AllowUsers admin
# 限制登录 IP(可选) # AllowUsers admin@192.168.1.*
# 禁用空密码 PermitEmptyPasswords no
# 设置超时 ClientAliveInterval 300 ClientAliveCountMax 2
# 禁用 X11 转发 X11Forwarding no
# 使用强加密算法 KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256 Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
|
3.3 重启 SSH 服务
1 2 3 4 5 6 7 8 9 10 11
| sudo sshd -t
sudo systemctl restart sshd
sudo systemctl status sshd
sudo systemctl enable sshd
|
3.4 配置防火墙允许 SSH
1 2 3 4 5 6 7 8
| sudo ufw allow 22/tcp
sudo ufw allow 2222/tcp
sudo ufw status verbose
|
四、系统更新与基础软件安装
4.1 更新系统
1 2 3 4 5 6 7 8 9 10 11 12
| sudo apt update
sudo apt upgrade -y
sudo apt dist-upgrade -y
sudo apt autoremove -y sudo apt autoclean
|
4.2 安装基础工具
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| sudo apt install -y \ vim \ curl \ wget \ git \ htop \ iotop \ iftop \ net-tools \ dnsutils \ telnet \ jq \ tree \ rsync \ unzip \ zip \ lsof \ strace \ tcpdump \ nmap \ ncdu \ sysstat \ iotop \ glances \ tmux \ screen \ bash-completion \ software-properties-common \ apt-transport-https \ ca-certificates \ gnupg \ lsb-release
|
4.3 配置系统 Locale
1 2 3 4 5 6 7 8 9 10 11
| locale
sudo locale-gen zh_CN.UTF-8 sudo update-locale LANG=zh_CN.UTF-8
sudo update-locale LANG=en_US.UTF-8
|
4.4 配置时区
1 2 3 4 5 6 7 8
| sudo timedatectl set-timezone Asia/Shanghai
timedatectl status
sudo timedatectl set-ntp true
|
五、防火墙配置(UFW)
5.1 启用 UFW
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| sudo ufw reset
sudo ufw default deny incoming sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose sudo ufw status numbered
|
5.2 常用 UFW 命令
1 2 3 4 5 6 7 8 9
| sudo ufw delete allow 80/tcp
sudo ufw disable
sudo ufw status verbose sudo tail -f /var/log/ufw.log
|
六、系统内核参数优化
6.1 创建优化配置
1
| sudo vim /etc/sysctl.d/99-optimize.conf
|
推荐配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| # ===== 网络优化 ===== # 启用 IP 转发(如需路由/NAT) net.ipv4.ip_forward = 0
# 禁用 IPv6(如不需要) # net.ipv6.conf.all.disable_ipv6 = 1 # net.ipv6.conf.default.disable_ipv6 = 1
# TCP 连接优化 net.core.somaxconn = 65535 net.core.netdev_max_backlog = 65535 net.ipv4.tcp_max_syn_backlog = 8192
# TCP 超时优化 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.tcp_keepalive_probes = 5 net.ipv4.tcp_keepalive_intvl = 15
# TCP 端口范围 net.ipv4.ip_local_port_range = 1024 65535
# TCP 内存优化 net.ipv4.tcp_mem = 16777216 33554432 67108864 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216
# 启用 TIME_WAIT 复用 net.ipv4.tcp_tw_reuse = 1
# 禁用 ICMP 重定向 net.ipv4.conf.all.accept_redirects = 0 net.ipv4.conf.default.accept_redirects = 0 net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0
# 启用 SYN Cookie 防护 net.ipv4.tcp_syncookies = 1
# 忽略 ICMP 广播 net.ipv4.icmp_echo_ignore_broadcasts = 1
# 记录可疑包 net.ipv4.conf.all.log_martians = 1
# ===== 文件句柄优化 ===== fs.file-max = 2097152 fs.nr_open = 2097152
# ===== 内存优化 ===== # 禁用 Swap 倾向(根据需求调整) vm.swappiness = 10
# 内存过量使用 vm.overcommit_memory = 1
# 脏页回写 vm.dirty_ratio = 20 vm.dirty_background_ratio = 10 vm.dirty_expire_centisecs = 3000 vm.dirty_writeback_centisecs = 500
# ===== 核心转储 ===== # 生产环境建议禁用 kernel.core_pattern = /dev/null
|
6.2 应用配置
1 2 3 4 5 6
| sudo sysctl --system
sysctl net.core.somaxconn sysctl vm.swappiness
|
七、用户限制配置
7.1 修改 limits.conf
1
| sudo vim /etc/security/limits.conf
|
添加配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| # 文件句柄数 * soft nofile 65535 * hard nofile 65535
# 进程数 * soft nproc 65535 * hard nproc 65535
# 核心转储大小(0 表示禁用) * soft core 0 * hard core 0
# 内存锁定 * soft memlock unlimited * hard memlock unlimited
|
7.2 配置 PAM
1 2 3 4
| sudo vim /etc/pam.d/common-session
session required pam_limits.so
|
7.3 Systemd 用户限制
1
| sudo vim /etc/systemd/system.conf
|
添加配置:
1 2
| DefaultLimitNOFILE=65535 DefaultLimitNPROC=65535
|
八、日志管理配置
8.1 配置 Journald
1
| sudo vim /etc/systemd/journald.conf
|
推荐配置:
1 2 3 4 5 6 7 8 9 10 11 12
| [Journal] Storage=persistent Compress=yes SystemMaxUse=1G SystemKeepFree=2G SystemMaxFiles=100 SystemMaxFileSize=100M RuntimeMaxUse=500M RuntimeKeepFree=1G RuntimeMaxFiles=50 RuntimeMaxFileSize=50M MaxRetentionSec=1month
|
8.2 配置 Logrotate
1
| sudo vim /etc/logrotate.conf
|
全局配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| weekly rotate 12 create dateext compress delaycompress missingok notifempty
# 系统日志 /var/log/syslog /var/log/kern.log /var/log/auth.log /var/log/mail.log { rotate 7 daily missingok notifempty delaycompress compress postrotate /usr/lib/rsyslog/rsyslog-rotate endscript }
# Nginx 日志(如安装) /var/log/nginx/*.log { daily missingok rotate 14 compress delaycompress notifempty create 0640 www-data adm sharedscripts prerotate if [ -d /etc/logrotate.d/httpd-prerotate ]; then \ run-parts /etc/logrotate.d/httpd-prerotate; \ fi \ endscript postrotate invoke-rc.d nginx rotate >/dev/null 2>&1 endscript }
|
8.3 日志查看命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| sudo journalctl -xe
sudo journalctl -u ssh -f sudo journalctl -u nginx -f
sudo journalctl --since "2026-03-12 00:00:00" --until "2026-03-12 23:59:59"
sudo journalctl -b
sudo journalctl --vacuum-time=7d sudo journalctl --vacuum-size=500M
|
九、安全加固
9.1 安装 Fail2Ban
1 2 3 4
| sudo apt install -y fail2ban
sudo systemctl enable fail2ban sudo systemctl start fail2ban
|
配置 Fail2Ban:
1 2
| sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local sudo vim /etc/fail2ban/jail.local
|
关键配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| [DEFAULT] bantime = 3600 findtime = 600 maxretry = 5 backend = auto
[sshd] enabled = true port = ssh filter = sshd logpath = /var/log/auth.log maxretry = 3 bantime = 86400
[nginx-http-auth] enabled = true port = http,https filter = nginx-http-auth logpath = /var/log/nginx/error.log maxretry = 3
|
9.2 配置自动安全更新
1 2 3
| sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
|
配置文件:
1
| sudo vim /etc/apt/apt.conf.d/50unattended-upgrades
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| Unattended-Upgrade::Allowed-Origins { "${distro_id}:${distro_codename}"; "${distro_id}:${distro_codename}-security"; "${distro_id}ESM:${distro_codename}-infra-security"; "${distro_id}ESM:${distro_codename}-apps-security"; };
Unattended-Upgrade::DevRelease "false"; Unattended-Upgrade::AutoFixInterruptedDpkg "true"; Unattended-Upgrade::MinimalSteps "true"; Unattended-Upgrade::Remove-Unused-Dependencies "true"; Unattended-Upgrade::Automatic-Reboot "false"; Unattended-Upgrade::Mail "admin@example.com";
|
9.3 禁用不需要的服务
1 2 3 4 5 6 7 8 9 10
| systemctl list-unit-files --state=enabled
sudo systemctl disable cups sudo systemctl disable bluetooth sudo systemctl disable avahi-daemon
systemctl list-unit-files --state=enabled | grep -E "cups|bluetooth|avahi"
|
十、监控与告警基础
10.1 安装基础监控工具
1 2 3 4 5
| sudo apt install -y htop iotop iftop nethogs glances
sudo apt install -nethogs nmap tcpdump
|
10.2 配置 Sysstat
1 2 3 4 5 6 7 8
| sudo apt install -y sysstat
sudo vim /etc/default/sysstat
sudo systemctl enable sysstat sudo systemctl start sysstat
|
查看历史数据:
1 2 3 4 5 6 7 8 9 10 11
| sar -u -f /var/log/sysstat/sa12
sar -r -f /var/log/sysstat/sa12
sar -d -f /var/log/sysstat/sa12
sar -n DEV -f /var/log/sysstat/sa12
|
十一、备份策略
11.1 配置关键目录备份
1 2
| sudo vim /usr/local/bin/backup-config.sh
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #!/bin/bash BACKUP_DIR="/backup/config" DATE=$(date +%Y%m%d_%H%M%S) HOSTNAME=$(hostname)
mkdir -p $BACKUP_DIR
tar -czf $BACKUP_DIR/${HOSTNAME}_config_$DATE.tar.gz \ /etc/ssh \ /etc/nginx \ /etc/mysql \ /etc/postgresql \ /etc/redis \ /etc/systemd/system \ /etc/crontab \ /var/spool/cron/crontabs
find $BACKUP_DIR -name "*.tar.gz" -mtime +7 -delete
echo "Backup completed: ${HOSTNAME}_config_$DATE.tar.gz"
|
1 2 3 4 5 6 7
| sudo chmod +x /usr/local/bin/backup-config.sh
sudo crontab -e
0 2 * * * /usr/local/bin/backup-config.sh >> /var/log/backup.log 2>&1
|
十二、初始化检查清单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #!/bin/bash
echo "=== Ubuntu 24.04 初始化检查 ==="
echo -e "\n[1] 系统版本" cat /etc/os-release | grep PRETTY_NAME
echo -e "\n[2] SSH 配置检查" grep -E "PermitRootLogin|PasswordAuthentication" /etc/ssh/sshd_config
echo -e "\n[3] 防火墙状态" sudo ufw status verbose
echo -e "\n[4] Sudo 用户" getent group sudo
echo -e "\n[5] 待更新包数量" apt list --upgradable 2>/dev/null | wc -l
echo -e "\n[6] 磁盘使用" df -h /
echo -e "\n[7] 内存使用" free -h
echo -e "\n[8] 关键服务状态" systemctl is-active ssh ufw fail2ban
echo -e "\n=== 检查完成 ==="
|
十三、总结
| 阶段 |
关键任务 |
优先级 |
| 初始登录 |
创建用户、配置 SSH 密钥 |
高 |
| 安全加固 |
SSH 配置、防火墙、Fail2Ban |
高 |
| 系统优化 |
内核参数、文件句柄、limits |
中 |
| 日志管理 |
Journald、Logrotate 配置 |
中 |
| 监控备份 |
基础监控、配置备份 |
中 |
| 自动更新 |
安全补丁自动更新 |
低 |
注意事项:
- 修改 SSH 配置前确保密钥登录可用
- 启用防火墙前确保必要端口已放行
- 内核参数调整需根据实际业务需求
- 定期审查日志和备份有效性
- 保持系统更新但避免频繁大版本升级