sudo 是 Linux 中讓非 root 使用者暫時取得高權限的機制。使用者不必知道超級使用者的密碼,只要在特殊指令(例如 yum)前加上 sudo 並輸入自己的密碼,就能以 root 身分執行;此後約五分鐘內再次使用 sudo 不必重複輸入。sudo 同時提供完善的日誌,詳細記錄每個使用者做過什麼。
這些授權規則存放於 /etc/sudoers,修改它需要 root 權限。
當使用者 dba 執行 sudo 時看到這段訊息,代表他還沒有被授權:
[dba@oracle ~]$ sudo yum -y update We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things: #1) Respect the privacy of others. #2) Think before you type. #3) With great power comes great responsibility. [sudo] password for dba: dba is not in the sudoers file. This incident will be reported.
步驟如下:
其中 NOPASSWD: ALL 就是免密碼提權的關鍵:該使用者執行任何 sudo 指令都不再需要輸入密碼。
免密碼提權方便,但風險也等比放大——帳號一旦被盜,等於直接交出 root。實務上更建議的做法是:只針對特定指令免密(例如 dba ALL=(ALL) NOPASSWD: /usr/bin/systemctl, /usr/bin/yum),或把使用者加入 wheel 群組再以 %wheel ALL=(ALL) ALL 統一管理,並搭配 sudo 日誌定期稽核。