服务器初始化SOP
本文介绍在OpenCloudOS系统上进行安全与稳定优化的步骤,包括更新系统、创建普通用户、配置SSH仅密钥登录、设置nftables防火墙规则、安装Docker及优化文件限制、启用BBR网络加速和设置Swap。
主要事项#
这里有一个背景,机器系统是OpenCloudOS,相当于CentOS,机器在海外,所以没有换源之类的操作。
| 项目 | 状态 | 说明 |
| --- | --- | --- |
| 系统更新 | - [ ] 已更新最新补丁 | 包含内核升级 |
| 普通用户 | - [ ] 创建wheel组普通用户 | root 禁止远程登录 |
| SSH | - [ ] 只允许密钥登录 | 端口 22 可访问,防火墙放行 |
| 防火墙 | - [ ] nftables 正确配置 | 默认拒绝非必要端口,SSH/ICMP/loopback放行 |
| 云安全组 | - [ ] 只放行必要端口 | SSH 仅允许可信 IP |
| Docker | - [ ] 安装并设置开机自启 | |
| 系统优化 | - [ ] swap/BBR/time sync/limits 等配置 | 保证长期 7×24 稳定运行 |
系统基础更新#
使用dnf更新,目的是:修复漏洞 + 更新内核 + 提升稳定性,命令如下:
sudo dnf clean all
sudo dnf makecache
sudo dnf update -y
bash重启后查看内核:
uname -r
bash安装常用运维工具:
sudo dnf install -y \
vim wget curl git htop unzip tar \
lsof net-tools bind-utils \
bash-completion chrony
bash启用时间同步:
sudo systemctl enable chronyd --now
timedatectl status
bash让系统自动安装安全补丁(自己做取舍):
sudo dnf install -y dnf-automatic
sudo systemctl enable --now dnf-automatic.timer
bash用户管理#
创建普通用户,添加到wheel用户组,让它能用sudo命令:
adduser oouser
passwd oouser
usermod -aG wheel oouser
bashSSH配置#
生成密钥:
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_cloud
# 上传到服务器
ssh-copy-id -i ~/.ssh/id_ed25519_cloud.pub 用户名@服务器IP
bash修改SSH:plaintext
sudo vim /etc/ssh/sshd_config
bash修改配置如下:plaintext
# 禁用密码认证
PasswordAuthentication no
# 禁用空密码登录
PermitEmptyPasswords no
# 确保公钥认证启用
PubkeyAuthentication yes
禁止 root 密码登录
PermitRootLogin no
bash重启SSH:
sudo systemctl reload sshd
bash防火墙配置#
先看系统中有哪些,iptables是旧方案,可以用但是没必要;firewalld非常常见但是相对nft占用资源略高。安装ntftbles:
sudo dnf install -y nftables
sudo systemctl enable nftables
bash创建规则文件:
sudo vim /etc/nftables.conf
bash写入如下规则放行SSH端口:plaintext
flush ruleset
table inet filter {
chain input {
type filter hook input priority 0;
policy drop;
ct state established,related accept
iif lo accept
# SSH
tcp dport 22 accept
# ICMP
ip protocol icmp accept
ip6 nexthdr icmpv6 accept
}
chain forward {
type filter hook forward priority 0;
policy drop;
}
chain output {
type filter hook output priority 0;
policy accept;
}
}
bash测试加载规则:
sudo nft -f /etc/nftables.conf
sudo nft list ruleset
bash启动nftables:plaintext
sudo systemctl start nftables
sudo systemctl status nftables
bash以后加端口则编辑/etc/nftables.conf文件:plaintext
sudo vim /etc/nftables.conf
bash子input链中新增一行:plaintext
tcp dport 8000 accept
bash然后应用:plaintext
sudo nft -f /etc/nftables.conf
bashDocker安装#
sudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
sudo dnf install -y docker-ce docker-ce-cli containerd.io
sudo systemctl enable --now docker
sudo usermod -aG docker aiadmin
sudo docker run hello-world # 测试
bash为长期运行优化系统#
首先是调整文件描述符限制(防止服务跑久了以后崩溃),修改文件:
sudo vim /etc/security/limits.conf
bash添加:
soft nofile 65535
hard nofile 65535
bash然后是开启BBR提升网络稳定性,内核4.9+才有:
echo "net.core.default_qdisc=fq" | sudo tee -a /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
sysctl net.ipv4.tcp_congestion_control # 验证命令
bash接着设置Swap,如果机器内存小的话可以设置:
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
bash开机自动挂载:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
bash降低swap触发频率:
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
bash