Rocky Linux 9 是與 RHEL 相容的免費選擇:相同的套件、相同的 dnf、相同的 firewalld 與 SELinux。如果你設定過 Debian 或 Ubuntu,步驟順序是一樣的,只是工具不同。以下是一台全新 Rocky 9 伺服器的前十分鐘。
以下每個指令都在發布前於我們的 Rocky Linux 9 映像檔上執行過。在乾淨的伺服器上,整個流程只要幾分鐘。
1. 更新系統
用控制台中的密碼以 root 登入,然後更新:
dnf -y upgrade
在我們的映像檔上,測試時只花了幾秒,因為映像檔本身已是最新。
2. 建立 sudo 使用者並加入 SSH 金鑰
在 Rocky 上,管理員群組叫 wheel,不是 sudo:
useradd -m -G wheel deploy
passwd deploy # sudo will ask for this password
把你的公開金鑰放到伺服器上(如何建立金鑰)。如果你已將它複製到 /root/key.pub:
install -d -m 700 -o deploy -g deploy /home/deploy/.ssh
install -m 600 -o deploy -g deploy /root/key.pub /home/deploy/.ssh/authorized_keys
restorecon -R /home/deploy/.ssh
restorecon 會設定 sshd 預期的 SELinux 標籤。標籤錯誤時,金鑰登入會失敗,而且沒有明顯的錯誤訊息。
開啟第二個終端機,確認 ssh deploy@YOUR.SERVER.IP 可以登入、sudo -v 接受密碼。在那之前請保持 root 工作階段開著。
3. 關閉 root 登入與密碼登入
這裡 Rocky 和教科書式修改 sshd_config 不同。設定放在 /etc/ssh/sshd_config.d/ 下的個別檔案中,sshd 採用它找到的第一個值。在我們的映像檔中,第一個檔案是 00-eqvps.conf,它允許 root 與密碼登入,好讓你第一天就能進入伺服器。請在那裡修改:
sed -i -e 's/^PermitRootLogin .*/PermitRootLogin no/' \
-e 's/^PasswordAuthentication .*/PasswordAuthentication no/' \
/etc/ssh/sshd_config.d/00-eqvps.conf
sshd -t && systemctl reload sshd
sshd -T | grep -E '^(permitrootlogin|passwordauthentication) '
最後一個指令會顯示 sshd 實際執行的設定。你應該看到:
permitrootlogin no
passwordauthentication no
之後 ssh root@… 即使用金鑰也會被拒絕,deploy 只能用金鑰登入。
4. 防火牆:firewalld
雲端映像檔沒有內建 firewalld。安裝、啟動,並只開放需要的服務:
dnf -y install firewalld
systemctl enable --now firewalld
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --remove-service=cockpit
firewall-cmd --reload
firewall-cmd --list-services
預設區域開放了 cockpit(連接埠 9090)和 dhcpv6-client。Cockpit 並未安裝,但沒有理由讓它的連接埠開著。加入應用程式時,用同樣方法開放連接埠,例如 firewall-cmd --permanent --add-service=https。
5. 自動安全性更新
dnf-automatic 依排程下載更新,但預設不會安裝,所以要把安裝打開:
dnf -y install dnf-automatic
sed -i 's/^apply_updates = .*/apply_updates = yes/' /etc/dnf/automatic.conf
systemctl enable --now dnf-automatic.timer
systemctl list-timers dnf-automatic.timer
如果只想要安全性修補,在同一個檔案裡再設定 upgrade_type = security。
6. 讓 SELinux 保持開啟
全新伺服器上 getenforce 會輸出 Enforcing,以上所有步驟都能在這個狀態下運作。當應用程式需要非標準的連接埠或目錄時,解法是設定標籤(semanage port、restorecon),而不是 setenforce 0。
選哪個方案
Rocky 9 閒置時約使用 400 MB 記憶體。Nano 方案(1 GB,每月 $3)足以執行一個小型服務;Micro(2 GB,每月 $5)還能放一個資料庫。在 NAT 方案上,SSH 透過控制台中顯示的專屬連接埠進入。如果需要對所有人開放 80/443 連接埠,請選擇有獨立 IPv4 的方案,例如 Micro-IP,每月 $10。
Rocky Linux 9 是 RHEL 系作業系統頁面上的映像檔之一,所有 VPS 方案都可以選用。付款方式為 Base、Ethereum 或 Polygon 網路上的 USDT、USDC 或 ETH,不需證件,也不需信用卡。如果你偏好用指令碼管理伺服器,API 文件說明了下單、重新安裝與續約。
評論
目前還沒有評論。歡迎成為第一位。