Skip to content

系统安装与配置指南

操作系统选择

1. Ubuntu Server

推荐使用Ubuntu Server 22.04 LTS版本作为服务器操作系统。

优势

  • 🔧 稳定性好,长期支持
  • 📚 社区活跃,资料丰富
  • 🛠️ 软件包丰富,更新及时
  • 💻 对新手友好

下载方式

  1. 官网下载:Ubuntu Server 22.04 LTS
  2. 国内镜像:

2. 其他选择

Debian

  • 更稳定,更轻量
  • 适合经验丰富的用户
  • 软件包可能较旧

CentOS/Rocky Linux

  • 企业级稳定性
  • 学习曲线较陡
  • 适合专业用户

系统安装步骤

1. 制作启动盘

  1. 下载RufusbalenaEtcher
  2. 准备8GB以上U盘
  3. 写入系统镜像

2. BIOS设置

  1. 进入BIOS(常用按键:F2/Del)
  2. 调整启动顺序
  3. 禁用Secure Boot
  4. 保存设置并重启

3. 安装系统

  1. 选择语言:English
  2. 键盘布局:默认
  3. 网络配置:
    bash
    # 静态IP配置示例
    IP: 192.168.1.100
    掩码: 255.255.255.0
    网关: 192.168.1.1
    DNS: 8.8.8.8, 114.114.114.114
  4. 存储配置:
    系统分区:
    /boot - 512MB
    swap - 内存大小
    / - 剩余空间
  5. 创建用户和密码
  6. 安装SSH服务器
  7. 等待安装完成

基础系统配置

1. 更新系统

bash
sudo apt update
sudo apt upgrade -y

2. 配置软件源

  1. 备份源文件:
bash
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
  1. 修改为国内源:
bash
# 清华源
sudo nano /etc/apt/sources.list

# 添加以下内容
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy 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.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

3. 安装基础软件

bash
sudo apt install -y \
    curl \
    wget \
    git \
    htop \
    net-tools \
    ufw \
    fail2ban \
    vim

4. 配置SSH

  1. 编辑SSH配置文件:
bash
sudo nano /etc/ssh/sshd_config
  1. 修改以下设置:
Port 22222           # 修改默认端口
PermitRootLogin no   # 禁止root登录
PasswordAuthentication yes  # 允许密码认证
  1. 重启SSH服务:
bash
sudo systemctl restart sshd

5. 配置防火墙

bash
# 启用UFW
sudo ufw enable

# 允许SSH端口
sudo ufw allow 22222/tcp

# 允许常用端口
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

6. 配置时区

bash
# 设置时区
sudo timedatectl set-timezone Asia/Shanghai

# 安装NTP服务
sudo apt install -y ntp
sudo systemctl enable ntp
sudo systemctl start ntp

网络配置

1. 配置静态IP

  1. 编辑netplan配置:
bash
sudo nano /etc/netplan/00-installer-config.yaml
  1. 添加配置:
yaml
network:
  version: 2
  ethernets:
    ens33:  # 网卡名称
      dhcp4: no
      addresses:
        - 192.168.1.100/24
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 114.114.114.114]
  1. 应用配置:
bash
sudo netplan apply

2. 配置主机名

bash
# 设置主机名
sudo hostnamectl set-hostname mediaserver

# 编辑hosts文件
sudo nano /etc/hosts

# 添加记录
127.0.0.1 mediaserver

3. 网络优化

  1. 修改系统参数:
bash
sudo nano /etc/sysctl.conf

# 添加以下内容
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_congestion_control = bbr
  1. 应用更改:
bash
sudo sysctl -p

性能优化

1. 调整系统限制

  1. 编辑limits配置:
bash
sudo nano /etc/security/limits.conf

# 添加以下内容
* soft nofile 65535
* hard nofile 65535

2. 配置Swap

bash
# 创建swap文件
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# 永久挂载
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

3. 优化文件系统

bash
# 调整mount选项
sudo nano /etc/fstab

# 添加noatime选项
UUID=xxx / ext4 defaults,noatime 0 1

监控与维护

1. 安装监控工具

bash
# 安装Netdata
bash <(curl -Ss https://my-netdata.io/kickstart.sh)

2. 配置日志轮转

bash
sudo nano /etc/logrotate.d/mediaserver

# 添加配置
/var/log/mediaserver/*.log {
    weekly
    rotate 4
    compress
    delaycompress
    missingok
    notifempty
    create 644 root root
}

3. 设置自动更新

bash
# 安装自动更新工具
sudo apt install -y unattended-upgrades

# 配置自动更新
sudo dpkg-reconfigure -plow unattended-upgrades

安全加固

1. 配置Fail2ban

bash
# 创建jail配置
sudo nano /etc/fail2ban/jail.local

# 添加配置
[sshd]
enabled = true
port = 22222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600

2. 禁用不需要的服务

bash
# 列出所有服务
systemctl list-unit-files

# 禁用不需要的服务
sudo systemctl disable SERVICE_NAME
sudo systemctl mask SERVICE_NAME

3. 配置系统审计

bash
# 安装审计工具
sudo apt install -y auditd

# 启用审计服务
sudo systemctl enable auditd
sudo systemctl start auditd

故障排除

1. 常见问题解决

  1. 系统无法启动

    • 检查GRUB配置
    • 进入恢复模式
    • 检查系统日志
  2. 网络连接问题

    • 检查网卡配置
    • 测试网络连通性
    • 查看防火墙规则
  3. 磁盘空间问题

    • 清理日志文件
    • 删除临时文件
    • 检查大文件占用

2. 日志查看

bash
# 系统日志
sudo journalctl -xe

# 认证日志
sudo tail -f /var/log/auth.log

# 应用日志
sudo tail -f /var/log/syslog

3. 性能诊断

bash
# CPU使用率
top

# 内存使用
free -h

# 磁盘使用
df -h

备份策略

1. 系统备份

bash
# 安装备份工具
sudo apt install -y rsync

# 创建备份脚本
sudo nano /usr/local/bin/backup.sh

# 添加备份命令
#!/bin/bash
rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /path/to/backup/

2. 配置备份

bash
# 创建定时任务
sudo crontab -e

# 添加每周备份
0 2 * * 0 /usr/local/bin/backup.sh

3. 数据恢复

bash
# 恢复系统文件
sudo rsync -aAXv /path/to/backup/ /

# 恢复GRUB
sudo grub-install /dev/sda
sudo update-grub