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

Linux sudo 提示 "is not in the sudoers file" 完整修復指南

編輯:Linux 教學

問題原因

當前用戶未被授權使用 sudo。/etc/sudoers 文件定義了哪些用戶/組可執行 sudo 及具體權限,默認僅 root 及 wheel/sudo 組成員擁有權限。

修復方法一:直接編輯 /etc/sudoers(需 root 權限)

  1. 切換到 root:su - 輸入 root 密碼。
  2. 編輯文件:gedit /etc/sudoers(或 vi /etc/sudoers)。
  3. 找到 root ALL=(ALL) ALL 行,在其下添加:
    your_username ALL=(ALL) ALL
    (將 your_username 替換為實際用戶名)。
  4. 保存退出。
步驟二

編輯 sudoers 文件添加用戶授權

修復方法二:使用 visudo(推薦,語法檢查+防並發衝突)

  1. 切換 root:su -。
  2. 執行 visudo(注意無空格)。
  3. 移動光標到 root ALL=(ALL) ALL 下方,按 i 進入插入模式。
    添加:your_username ALL=(ALL) ALL
  4. 按 Esc 退出插入模式,輸入 :x 保存並退出。

visudo 會在保存前自動檢查語法,防止寫入錯誤導致 sudo 癱瘓。

驗證

su - your_username
sudo ls /root   # 應提示輸入密碼並顯示內容

權限格式解析

用戶名  主機=(目標用戶)  允許的命令
ld      ALL=(ALL)      ALL
  • ALL (第1個) = 所有主機
  • (ALL) = 可切換至任意用戶身份執行
  • ALL (第2個) = 允許執行所有命令

如需免密碼:ld ALL=(ALL) NOPASSWD: ALL(生產環境慎用)。

常見誤區

  • 直接用 vi /etc/sudoers 未語法檢查,寫入錯誤導致無法 sudo 甚至無法 su(單用戶模式修復)。
  • 修改後未切換用戶重新登錄,導致權限未生效。
  • 編輯時誤刪 root ALL=(ALL) ALL 導致 root 自身失權。
Copyright © Windows教程網 All Rights Reserved