FisherHub Blog
← 返回列表 | 工具笔记

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

从 SSH 安全到 fail2ban,给新服务器上第一道防线——30 分钟搞定

用户与权限

# 创建新用户
adduser deploy

# 加入 sudo
usermod -aG sudo deploy

# 切换到新用户
su - deploy

SSH 安全加固

# 编辑 SSH 配置
sudo vim /etc/ssh/sshd_config

关键修改:

Port 2222              # 换端口
PermitRootLogin no     # 禁止 root 登录
PasswordAuthentication no  # 只用密钥
PubkeyAuthentication yes
AllowUsers deploy       # 白名单
sudo systemctl restart sshd

防火墙

sudo ufw allow 2222/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status

fail2ban

sudo apt install fail2ban -y
sudo systemctl enable fail2ban --now

系统更新

# 启用自动安全更新
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgrades

时区与语言

sudo timedatectl set-timezone Asia/Shanghai
sudo localectl set-locale LANG=en_US.UTF-8

基础工具

sudo apt install -y curl wget git vim htop net-tools \
  ca-certificates gnupg lsb-release

检查清单

  • 新增 deploy 用户
  • SSH 密钥登录
  • 禁止 root + 密码登录
  • UFW 防火墙开启
  • fail2ban 运行中
  • 自动安全更新启用
  • 时区正确

完成以上步骤,你的服务器就有了基本的安全防线。接下来才是装 Nginx、Docker 等应用层东西。