Devhui's Blog

返回

主要事项#

这里有一个背景,机器系统是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
bash

SSH配置#

生成密钥:


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
bash

Docker安装#


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
服务器初始化SOP
https://astro-pure.js.org/blog/%E6%B5%8B%E8%AF%95%E6%96%87%E7%AB%A0%E7%9A%84%E7%9B%AE%E5%BD%95/%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%88%9D%E5%A7%8B%E5%8C%96sop
作者 devhui
发布时间 2026年2月1日