Ubuntu 24.04 LTS 服务器初始化配置指南

一、概述

Ubuntu 24.04 LTS(Noble Numbat)于 2024 年 4 月发布,提供 5 年标准支持和 12 年扩展安全维护。本文档提供生产环境服务器的标准化初始化配置流程。

二、初始登录与基础检查

2.1 首次登录

1
2
3
4
5
# 使用 SSH 密钥登录(推荐)
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

# 添加到 sudo 组
usermod -aG sudo admin

# 复制 root 的 SSH 密钥(可选)
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
# 如果使用默认端口 22
sudo ufw allow 22/tcp

# 如果使用自定义端口(如 2222)
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
locale

# 生成中文 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

# 启用 NTP 同步
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

# 允许 SSH
sudo ufw allow 22/tcp
# 或自定义端口
# sudo ufw allow 2222/tcp

# 允许 HTTP/HTTPS(Web 服务器)
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

# 允许特定 IP 访问(可选)
# sudo ufw allow from 192.168.1.0/24 to any port 22

# 启用防火墙
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
# 应用所有 sysctl 配置
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
# 确保 limits 生效
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

# 启用 sysstat
sudo vim /etc/default/sysstat
# 修改 ENABLED="true"

sudo systemctl enable sysstat
sudo systemctl start sysstat

查看历史数据:

1
2
3
4
5
6
7
8
9
10
11
# CPU 使用
sar -u -f /var/log/sysstat/sa12

# 内存使用
sar -r -f /var/log/sysstat/sa12

# 磁盘 IO
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

# 保留最近 7 天备份
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
# 每天凌晨 2 点备份
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 初始化检查 ==="

# 1. 系统版本
echo -e "\n[1] 系统版本"
cat /etc/os-release | grep PRETTY_NAME

# 2. SSH 配置
echo -e "\n[2] SSH 配置检查"
grep -E "PermitRootLogin|PasswordAuthentication" /etc/ssh/sshd_config

# 3. 防火墙状态
echo -e "\n[3] 防火墙状态"
sudo ufw status verbose

# 4. 用户检查
echo -e "\n[4] Sudo 用户"
getent group sudo

# 5. 系统更新
echo -e "\n[5] 待更新包数量"
apt list --upgradable 2>/dev/null | wc -l

# 6. 磁盘使用
echo -e "\n[6] 磁盘使用"
df -h /

# 7. 内存使用
echo -e "\n[7] 内存使用"
free -h

# 8. 服务状态
echo -e "\n[8] 关键服务状态"
systemctl is-active ssh ufw fail2ban

echo -e "\n=== 检查完成 ==="

十三、总结

阶段 关键任务 优先级
初始登录 创建用户、配置 SSH 密钥
安全加固 SSH 配置、防火墙、Fail2Ban
系统优化 内核参数、文件句柄、limits
日志管理 Journald、Logrotate 配置
监控备份 基础监控、配置备份
自动更新 安全补丁自动更新

注意事项:

  1. 修改 SSH 配置前确保密钥登录可用
  2. 启用防火墙前确保必要端口已放行
  3. 内核参数调整需根据实际业务需求
  4. 定期审查日志和备份有效性
  5. 保持系统更新但避免频繁大版本升级