AlmaLinux VPS 配置指南
通过系统更新、EPEL、SELinux 检查、firewalld 和自动补丁,让 AlmaLinux 服务器做好投入生产的准备。
AlmaLinux 的行为与 Red Hat Enterprise Linux 一致,因此本指南遵循企业级 Linux 的习惯:保持 SELinux 开启,使用 firewalld,并由 dnf 负责打补丁。
- 适用版本:AlmaLinux 10、9 和 8
- 推荐配置:2 GB 或以上内存
- 您需要准备:SSH 客户端和通过电子邮件收到的 root 密码
选择版本
在结账时选择 AlmaLinux。版本 9 目前的厂商支持最广泛;如果您的软件已支持版本 10,新项目选择 10 更合适。选择 8 之前,请先查看厂商的支持列表。
登录并确认系统信息
连接后确认系统版本和 SELinux 状态。
ssh root@YOUR_SERVER_IP
cat /etc/almalinux-release
getenforce更新系统
安装所有可用更新,如果更新包含新内核,请重启。
needs-restarting 命令来自 dnf-utils 或 yum-utils 软件包,如果缺失请先安装。
dnf upgrade -y
dnf needs-restarting -r || systemctl reboot添加 EPEL 软件仓库
EPEL 提供数千个额外的软件包。在版本 9 及更高版本上,其中许多软件包还需要 CRB 软件仓库。
在 AlmaLinux 8 上,此仓库名为 powertools,请改用 --set-enabled powertools。
dnf install epel-release dnf-plugins-core -y
dnf config-manager --set-enabled crb创建管理员用户
加入 wheel 组即可获得 sudo 权限。
useradd -m -G wheel deploy
passwd deploy
# 在您自己的电脑上执行
ssh-copy-id deploy@YOUR_SERVER_IP加固 SSH
在这一系列系统上,该服务名为 sshd。
关闭当前终端之前,请在第二个终端中确认密钥登录可用。 版本 8 不会读取此目录,请改为在 /etc/ssh/sshd_config 中设置 PermitRootLogin no 和 PasswordAuthentication no。
sudo tee /etc/ssh/sshd_config.d/10-hardening.conf > /dev/null <<'EOF'
PermitRootLogin no
PasswordAuthentication no
EOF
sudo sshd -t && sudo systemctl restart sshd启用 firewalld
firewalld 按服务名称管理规则,因此您很少需要记住端口号。
sudo dnf install firewalld -y
sudo systemctl enable --now firewalld
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload配合 SELinux,而不是对抗它
SELinux 是大多数人最想关闭的功能。其实只需查看它拦截了什么,然后修复那一处即可。例如,Web 服务器需要连接后端时,只需改动一项设置。
sudo ausearch -m avc -ts recent
sudo setsebool -P httpd_can_network_connect 1自动安装安全更新
除非您明确指定应用更新,否则 dnf-automatic 只会下载更新。
sudo dnf install dnf-automatic -y
sudo sed -i 's/^apply_updates.*/apply_updates = yes/' /etc/dnf/automatic.conf
sudo systemctl enable --now dnf-automatic.timerAlmaLinux VPS 配置指南:常见问题
常见问题的快速解答。
为什么 AlmaLinux 8 上 sshd_config.d 中的设置不生效?
在 AlmaLinux 8 上,SSH 服务器不会读取 /etc/ssh/sshd_config.d/ 中的文件,因此放在那里的加固配置不会生效。请直接在 /etc/ssh/sshd_config 中设置 PermitRootLogin no 和 PasswordAuthentication no,用 sshd -t 检查文件,然后重启 sshd。AlmaLinux 9 和 10 会读取该目录。关闭会话前,请先在另一个终端确认密钥登录可用。
如何解决 AlmaLinux 上的 SELinux 拒绝问题?
用 sudo ausearch -m avc -ts recent 查看 SELinux 拦截了什么,然后只修复这一项,而不是直接关闭 SELinux。例如,需要连接后端的 Web 服务器只需运行 setsebool -P httpd_can_network_connect 1。保持 SELinux 处于 enforcing 状态,是本 AlmaLinux 指南遵循的企业级 Linux 习惯之一。
AlmaLinux 的 wheel 组是什么,如何授予 sudo 权限?
wheel 组是 AlmaLinux 及其他企业级 Linux 系统中的管理员组:默认的 sudoers 文件允许该组成员使用 sudo 执行命令。运行 useradd -m -G wheel deploy 即可一步创建管理员用户并加入该组,再用 passwd deploy 设置密码,然后在您自己的电脑上用 ssh-copy-id 复制 SSH 密钥。对于已有用户,运行 usermod -aG wheel 加上用户名即可。
如何在 AlmaLinux 上开启自动更新?
安装 dnf-automatic,在 /etc/dnf/automatic.conf 中设置 apply_updates = yes,并用 systemctl 启用 dnf-automatic.timer。如果不设置这一项,AlmaLinux 上的 dnf-automatic 只会下载更新而不会安装。定时器开启后,安全修复会持续推送到您的服务器,无需您登录操作。
如何在 AlmaLinux 上禁用 root SSH 登录?
先在 wheel 组中创建一个管理员用户,并把您的 SSH 密钥复制给它。然后在 /etc/ssh/sshd_config.d/ 下的一个文件中写入 PermitRootLogin no 和 PasswordAuthentication no,用 sshd -t 测试后重启 sshd。AlmaLinux 8 不读取该目录,因此请把这两行加到 /etc/ssh/sshd_config 中,并在另一个终端确认密钥登录可用。
如何检查 AlmaLinux 更新后是否需要重启?
在 dnf upgrade -y 之后运行 dnf needs-restarting -r。它会报告是否有新内核或核心库需要重启,指南中的命令仅在需要时才重启 AlmaLinux 服务器。该命令来自 dnf-utils 或 yum-utils 软件包,如果缺失请先安装。
如何查看我的 VPS 运行的是哪个 AlmaLinux 版本?
通过 SSH 登录,运行 cat /etc/almalinux-release 查看确切的 AlmaLinux 版本,再运行 getenforce 确认 SELinux 处于 enforcing 状态。请在按指南继续操作前先检查版本,因为部分步骤在 8 版本上有所不同,比如 powertools 软件源的名称以及 SSH 设置的存放位置。
AlmaLinux VPS 建议配多少内存?
本 AlmaLinux 部署指南建议使用 2 GB 或以上内存的服务器,其步骤适用于 AlmaLinux 10、9 和 8。除服务器外,您只需一个 SSH 客户端,以及服务器就绪时我们通过邮件发送的 root 密码。
探索更多 VPS 主机方案
为您的项目找到合适的服务器。