Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

CentOS 啟用 sudo 權限並禁止 root 遠端登入安全加固

編輯:Linux 教學

CentOS 預設允許 root 直接 SSH 登入且未配置 sudo,存在暴力破解與誤操作風險。本文說明建立 sudo 管理員、鎖定 root SSH 登入的標準流程。

1. 建立普通用戶並加入 sudo

# 建立用戶
useradd centos
passwd centos

# 配置 sudo 權限
visudo
# 找到
root    ALL=(ALL)       ALL
# 下方新增
centos  ALL=(ALL)       ALL
# 或允許免密 (生產慎用)
# centos  ALL=(ALL)       NOPASSWD: ALL

visudo 會語法檢查,防止配置錯誤導致無法 sudo。

2. 禁止 root SSH 登入

vim /etc/ssh/sshd_config
# 修改
PermitRootLogin no
# 建議同時啟用
PubkeyAuthentication yes
PasswordAuthentication no   # 僅允許金鑰登入,更安全

3. 重載 SSH 服務生效

systemctl reload sshd
# 或
service sshd reload

4. 驗證配置

  1. 新開終端,用 centos 用戶 SSH 登入。
  2. 測試 sudo -l 顯示權限。
  3. 嘗試 sudo systemctl status sshd 執行管理指令。
  4. 另開終端嘗試 ssh root@server 應被拒絕。

緊急情況:root 仍需登入

  • 本地主控台 (Console/IDRAC/iLO) 不受 PermitRootLogin 限制。
  • 單用戶模式 (Runlevel 1) 可重設密碼、修復配置。
  • 若必須遠端 root,可暫時改為 PermitRootLogin prohibit-password 僅允許金鑰登入。

進階:sudo 細粒度控制

# 僅允許特定指令
centos  ALL=(ALL) /usr/bin/systemctl restart httpd, /usr/bin/systemctl status httpd

# 群組授權 (wheel 群組)
%wheel  ALL=(ALL)       ALL
# 將用戶加入 wheel
usermod -aG wheel centos

安全審計建議

  • 定期審計 /var/log/secure 中的 sudo 使用記錄。
  • 配置 Defaults logfile=/var/log/sudo.log 集中記錄。
  • 關鍵系統建議部署堡壘機/跳板機,所有運維操作經審計錄影。
Copyright © Windows教程網 All Rights Reserved