Ubuntu 服务器开荒必做事项
Ubuntu 服务器开荒必做事项
一、系统更新与基础配置
1. 切换软件源镜像(重要:优先执行)
Ubuntu 版本代号对照表
在配置镜像源之前,请先确认您的 Ubuntu 版本代号:
| Ubuntu 版本 | 版本代号 | 适用场景 |
|---|---|---|
| 24.04 LTS | noble | 最新长期支持版(推荐) |
| 22.04 LTS | jammy | 主流长期支持版(稳定) |
| 20.04 LTS | focal | 旧长期支持版(仍有维护) |
| 18.04 LTS | bionic | 老旧版本(接近停止维护) |
查看当前系统版本:
lsb_release -a
# 或
lsb_release -c # 查看版本代号
国内服务器推荐镜像源
阿里云镜像源(推荐)
# 备份原始源配置
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
# 编辑源配置
sudo nano /etc/apt/sources.list
# 清空并替换为以下内容(以 Ubuntu 22.04 jammy 为例)
# nano 编辑器操作说明:
# - 按 Ctrl+K 删除当前行(可重复操作清空文件)
# - 复制镜像源配置内容后,右键粘贴到编辑器
# - 按 Ctrl+O 保存文件(字母 O,不是数字 0),然后按回车确认
# - 按 Ctrl+X 退出 nano 编辑器
Ubuntu 22.04 (Jammy) 阿里云源:
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
Ubuntu 24.04 (Noble) 阿里云源:
deb http://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse
其他国内镜像源
# 清华大学镜像源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# 华为云镜像源
deb https://mirrors.huaweicloud.com/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.huaweicloud.com/ubuntu/ jammy-security main restricted universe multiverse
deb https://mirrors.huaweicloud.com/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.huaweicloud.com/ubuntu/ jammy-backports main restricted universe multiverse
# 腾讯云镜像源
deb http://mirrors.tencent.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.tencent.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.tencent.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.tencent.com/ubuntu/ jammy-backports main restricted universe multiverse
一键切换阿里云镜像源脚本
# 查看当前 Ubuntu 版本代号
lsb_release -cs
# 自动替换为阿里云源(Ubuntu 22.04/24.04 通用)
sudo sed -i 's|http://.*archive.ubuntu.com|http://mirrors.aliyun.com|g' /etc/apt/sources.list
sudo sed -i 's|http://.*security.ubuntu.com|http://mirrors.aliyun.com|g' /etc/apt/sources.list
国外服务器推荐镜像源
官方默认源(海外服务器使用)
# 海外服务器通常不需要更换,使用默认源即可
# 如果需要优化,可以选择地理位置较近的镜像
美国/欧洲常用镜像
# 美国 - MIT 镜像
deb http://mirrors.mit.edu/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.mit.edu/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.mit.edu/ubuntu/ jammy-updates main restricted universe multiverse
# 英国 - UKFast 镜像
deb http://mirror.bytemark.co.uk/ubuntu/ jammy main restricted universe multiverse
deb http://mirror.bytemark.co.uk/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirror.bytemark.co.uk/ubuntu/ jammy-updates main restricted universe multiverse
# 日本 - JAIST 镜像(适合亚洲海外服务器)
deb http://ftp.jaist.ac.jp/pub/Linux/ubuntu/ jammy main restricted universe multiverse
deb http://ftp.jaist.ac.jp/pub/Linux/ubuntu/ jammy-security main restricted universe multiverse
deb http://ftp.jaist.ac.jp/pub/Linux/ubuntu/ jammy-updates main restricted universe multiverse
自动选择最快镜像
# 使用 apt-select 工具测试并选择最快镜像
sudo apt install -y apt-select
apt-select --country CN --top 3 --choose # 中国
apt-select --country US --top 3 --choose # 美国
更换源后必做
# 更新软件包列表
sudo apt update
# 升级已安装的软件包
sudo apt upgrade -y
# 完整升级(包括内核)
sudo apt full-upgrade -y
# 清理不再需要的依赖
sudo apt autoremove -y
Desktop 版图形界面操作方法(适合新手)
如果您使用的是 Ubuntu Desktop(带图形界面),可通过 "软件和更新" 工具可视化修改镜像源,操作更简单:
- 打开 "应用程序",搜索并打开 "软件和更新" (Software & Updates)
- 在弹出的窗口中,找到 "下载自"(Download from)选项,点击右侧的下拉菜单,选择 "其他站点" (Other)
- 在新窗口中,点击 "选择最佳服务器" (Select Best Server),系统会自动测试并推荐延迟最低的国内镜像源
- 等待测试完成后,点击 "选择服务器"(Choose Server),然后输入管理员密码确认
- 关闭窗口时,系统会提示 "需要更新缓存",点击 "重新载入"(Reload),等待索引更新完成即可
常见问题与解决方法
1. 执行 apt update 时报 "404 Not Found"
- 原因:版本代号错误(如将 24.04 的
noble写成了 22.04 的jammy) - 解决:重新执行
lsb_release -c命令查看当前系统的版本代号,然后修正sources.list中的代号
2. 镜像源生效后速度仍慢
- 原因:选择的镜像源可能暂时负载过高,或您的网络与该源兼容性差
- 解决:更换其他国内镜像源(如从阿里云换成清华大学源),重复配置步骤
3. 修改后无法恢复原配置
- 解决:执行备份恢复命令:
sudo cp /etc/apt/sources.list.bak /etc/apt/sources.list
sudo apt update
4. 执行 apt update 时提示证书信任问题
- 解决:输入
y并回车确认信任证书
2. 设置时区和时间同步
# 设置时区为 Asia/Shanghai
sudo timedatectl set-timezone Asia/Shanghai
# 安装 NTP 时间同步服务
sudo apt install -y chrony
sudo systemctl enable chrony
sudo systemctl start chrony
# 验证时间同步状态
timedatectl status
3. 设置主机名
# 设置主机名
sudo hostnamectl set-hostname your-server-name
# 编辑 hosts 文件
sudo nano /etc/hosts
# 添加:127.0.0.1 your-server-name
二、用户与权限管理
1. 创建新用户(避免使用 root)
# 创建新用户
sudo adduser username
# 将用户添加到 sudo 组
sudo usermod -aG sudo username
# 验证用户权限
su - username
sudo whoami
2. 配置 SSH 密钥登录
# 在本地机器生成密钥对(如果还没有)
ssh-keygen -t ed25519 -C "your_email@example.com"
# 复制公钥到服务器
ssh-copy-id username@server_ip
# 或者手动添加
mkdir -p ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
3. 禁用 root 用户 SSH 登录
sudo nano /etc/ssh/sshd_config
修改以下配置:
PermitRootLogin no
PasswordAuthentication no # 如果已配置密钥登录
PubkeyAuthentication yes
# 重启 SSH 服务
sudo systemctl restart sshd
三、防火墙配置
1. 安装并配置 UFW 防火墙
# 安装 UFW
sudo apt install -y ufw
# 设置默认策略
sudo ufw default deny incoming
sudo ufw default allow outgoing
# 允许 SSH 连接(重要!先设置再启用)
sudo ufw allow ssh
# 或者指定端口
sudo ufw allow 22/tcp
# 允许其他常用端口
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
# 启用防火墙
sudo ufw enable
# 查看防火墙状态
sudo ufw status verbose
2. 配置 Fail2Ban(防暴力破解)
# 安装 Fail2Ban
sudo apt install -y fail2ban
# 创建本地配置文件
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
# 编辑配置
sudo nano /etc/fail2ban/jail.local
推荐配置:
[DEFAULT]
bantime = 3600 # 封禁时间(秒)
findtime = 600 # 检测时间窗口
maxretry = 5 # 最大重试次数
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
# 启动并启用 Fail2Ban
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
# 查看状态
sudo fail2ban-client status
sudo fail2ban-client status sshd
四、网络优化
1. 配置静态 IP(如需要)
# 编辑 Netplan 配置
sudo nano /etc/netplan/00-installer-config.yaml
示例配置:
network:
ethernets:
enp1s0f0: //这里要核对一下自己网卡的名称,可用 ip a 查看
addresses:
- 192.168.1.100/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
version: 2
# 应用配置
sudo netplan apply
常见问题1:文件权限问题
# 异常提示
** (generate:19938): WARNING **: 09:08:10.458: Permissions for /etc/netplan/00-installer-config.yaml are too open. Netplan configuration should NOT be accessible by others.
** (process:19936): WARNING **: 09:08:10.805: Permissions for /etc/netplan/00-installer-config.yaml are too open. Netplan configuration should NOT be accessible by others.
** (process:19936): WARNING **: 09:08:10.882: Permissions for /etc/netplan/00-installer-config.yaml are too open. Netplan configuration should NOT be accessible by others.
# 解决方法
sudo chmod 600 /etc/netplan/00-installer-config.yaml
常见问题2:配置文件格式问题(如果批量执行以下命令遇到问题,可以逐个执行)
# 遇到yaml文件格式怎么配置都不对时,采用以下处理方案
cat > /tmp/fixip.yaml << 'EOF'
network:
ethernets:
enp1s0f0:
addresses:
- 192.168.1.100/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
version: 2
EOF
sudo cp /tmp/fixip.yaml /etc/netplan/00-installer-config.yaml
sudo chmod 600 /etc/netplan/00-installer-config.yaml
sudo netplan apply
rm /tmp/fixip.yaml
2. 优化网络参数
sudo nano /etc/sysctl.conf
添加以下优化参数:
# 防止 SYN 洪水攻击
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_synack_retries = 2
# 优化 TCP 连接
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_intvl = 60
net.ipv4.tcp_keepalive_probes = 5
# 增加文件描述符限制
fs.file-max = 65535
# 禁用 IP 转发(如果不是路由器)
net.ipv4.ip_forward = 0
# 应用配置
sudo sysctl -p
五、安全加固
1. 安装自动安全更新
# 安装 unattended-upgrades
sudo apt install -y unattended-upgrades
# 配置自动更新
sudo dpkg-reconfigure -plow unattended-upgrades
# 编辑配置
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades
2. 配置日志审计
# 安装 auditd
sudo apt install -y auditd
# 启用服务
sudo systemctl enable auditd
sudo systemctl start auditd
# 查看日志
sudo ausearch --start today
3. 限制 su 命令访问
# 只允许 wheel 组用户使用 su
sudo nano /etc/pam.d/su
# 取消注释:auth required pam_wheel.so
4. 设置密码策略
# 安装 libpam-pwquality
sudo apt install -y libpam-pwquality
# 编辑配置
sudo nano /etc/security/pwquality.conf
推荐配置:
minlen = 8
minclass = 3
maxrepeat = 3
六、系统监控
1. 安装基础监控工具
# 安装常用工具
sudo apt install -y htop iotop nethogs iftop tmux tree curl wget git
# 安装系统监控
sudo apt install -y sysstat
sudo systemctl enable sysstat
sudo systemctl start sysstat
2. 配置日志管理
# 查看日志配置
sudo nano /etc/systemd/journald.conf
推荐配置:
SystemMaxUse=500M
SystemMaxFileSize=50M
MaxRetentionSec=1month
# 重启 journald
sudo systemctl restart systemd-journald
七、磁盘与存储管理
1. 检查磁盘使用情况
# 查看磁盘分区
lsblk
df -h
# 查看磁盘使用情况
sudo du -sh /*
2. 配置 Logrotate
# 检查 logrotate 配置
sudo nano /etc/logrotate.conf
# 查看具体服务配置
ls /etc/logrotate.d/
3. 设置 Swap 分区(如需要)
# 创建 swap 文件(例如 4GB)
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# 添加到 fstab 实现开机挂载
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# 验证
free -h
swapon --show
八、常用软件安装
1. 开发工具
# 安装编译工具
sudo apt install -y build-essential
# 安装常用开发工具
sudo apt install -y python3 python3-pip nodejs npm docker.io
2. 数据库(按需安装)
# MySQL
sudo apt install -y mysql-server
# PostgreSQL
sudo apt install -y postgresql postgresql-contrib
# Redis
sudo apt install -y redis-server
3. Web 服务器(按需安装)
# Nginx
sudo apt install -y nginx
# Apache
sudo apt install -y apache2
九、备份策略
1. 配置自动备份
# 安装 rsync
sudo apt install -y rsync
# 创建备份脚本
sudo nano /usr/local/bin/backup.sh
示例备份脚本:
#!/bin/bash
BACKUP_DIR="/backup/$(date +%Y%m%d)"
mkdir -p $BACKUP_DIR
# 备份重要配置
rsync -av /etc/ $BACKUP_DIR/etc/
rsync -av /var/www/ $BACKUP_DIR/www/
# 删除 7 天前的备份
find /backup -type d -mtime +7 -exec rm -rf {} \;
# 添加执行权限
sudo chmod +x /usr/local/bin/backup.sh
# 添加到 crontab
sudo crontab -e
# 添加:0 2 * * * /usr/local/bin/backup.sh
十、性能调优
1. 调整文件描述符限制
# 编辑 limits.conf
sudo nano /etc/security/limits.conf
添加:
* soft nofile 65535
* hard nofile 65535
root soft nofile 65535
root hard nofile 65535
2. 优化 SSH 连接
sudo nano /etc/ssh/sshd_config
添加/修改:
ClientAliveInterval 300
ClientAliveCountMax 2
UseDNS no
十一、验证清单
完成以上配置后,建议进行以下验证:
- 系统已更新到最新版本
- 时区设置正确
- 新用户已创建并可正常使用 sudo
- SSH 密钥登录正常,root 登录已禁用
- 防火墙已启用,规则正确
- Fail2Ban 运行正常
- 自动安全更新已配置
- 监控工具已安装
- 备份脚本已配置并测试
- 系统性能参数已优化
附录:常用命令速查
# 查看系统信息
hostnamectl
uname -a
lsb_release -a
# 查看服务状态
systemctl status <service-name>
# 查看端口占用
sudo ss -tulpn
# 查看登录日志
last
lastb
# 查看磁盘 IO
iostat -x 1
# 查看内存使用
free -h
# 查看进程
ps aux | grep <process>
top
htop
注意:
- 执行任何配置前,建议先创建系统快照或备份
- 修改 SSH 配置前,确保已配置好密钥登录,避免被锁在服务器外
- 启用防火墙前,务必先允许 SSH 端口
- 根据实际业务需求调整配置参数
评论