Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

解決 xxx is not in the sudoers file 的兩種做法

普通使用者執行 sudo 時被拒,提示 xx is not in the sudoers file。以下兩種做法原理相同,只是操作方式不同。

方法一:直接編輯 /etc/sudoers

先用 whereis 找到設定檔位置(預設在 /etc/sudoers):
[root@localhost xiaofei]# whereis sudoers
sudoers: /etc/sudoers /etc/sudoers.bak /usr/share/man/man5/sudoers.5.gz

切換到 root 後,由於該檔預設唯讀,需先加上寫入權限:
[root@localhost xiaofei]# chmod u+w /etc/sudoers

接著編輯,找到 root ALL=(ALL) ALL 那一行,在下面追加自己的帳號:
root   ALL=(ALL)   ALL
xiaofei ALL=(ALL)   ALL   # 這行是新增的,xiaofei 為使用者名稱

存檔後務必把寫入權限收回:
[root@localhost xiaofei]# chmod u-w /etc/sudoers

若不收回,執行 sudo 會出現 /etc/sudoers is mode 0640, should be 0440 的警告甚至拒絕運作。完成後登出 root,該帳號即可使用 sudo。

方法二:用 visudo

先切換到 root(注意 su - 與 su 不同:前者會一併載入 root 的環境變數):
$ su -

然後執行 visudo(注意指令中間沒有空格):
1. 移游標到檔案最後一行
2. 按 a 進入 append 模式
3. 輸入 your_user_name ALL=(ALL) ALL
4. 按 Esc
5. 輸入 :wq 存檔

visudo 的好處是存檔時會自動做語法檢查,發現寫錯會拒絕寫入,比手動編輯安全得多,是官方推薦的方式。

Copyright © Linux教程網 All Rights Reserved