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

sudo 不能用怎麼救:直接修 /etc/sudoers 的七個步驟

裝完系統發現 sudo 完全無法使用,只會看到一段似曾相識的長篇警告:

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.

查了資料後,解法如下:

  1. 用 su - 登入 root 模式
  2. 把 sudoers 的權限改成可寫:
    chmod 740 /etc/sudoers
  3. 用 vi 開啟:
    vi /etc/sudoers
  4. 在 root ALL=(ALL) ALL 那行下面另起一行,加上你的帳號:
    username ALL=(ALL) ALL
  5. 存檔退出
  6. 把權限改回預設的 440:
    chmod 440 /etc/sudoers
  7. 退回普通使用者,用 sudo 測試——通過

若想使用 sudo 時不必輸入密碼,把第 4 步改成:

username ALL=NOPASSWD:ALL

完整的 sudoers 範例:

# /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
Defaults    env_reset

# Host alias specification
# User alias specification
# Cmnd alias specification

# User privilege specification
root    ALL=(ALL) ALL
#tiger  ALL=(ALL) ALL
tiger   ALL=NOPASSWD:ALL

# Uncomment to allow members of group sudo to not need a password
# %sudo ALL=NOPASSWD: ALL

兩個提醒:一是權限必須還原成 440,否則 sudo 基於安全考量會拒絕運作;二是官方建議用 visudo 編輯這個檔案,因為它會在存檔前做語法檢查,能避免你把自己鎖在門外。NOPASSWD 雖然方便,但等於讓任何取得該帳號的人都能直接當 root,共用機器上最好別開。

Copyright © Linux教程網 All Rights Reserved