1. 程式人生 > 實用技巧 >Linux環境下為普通使用者新增sudo許可權

Linux環境下為普通使用者新增sudo許可權

sudo的配置檔案

sudo預設配置檔案是**/etc/sudoers** ,一般使用Linux指定編輯工具visudo ,此工具的好處是可以進行錯誤檢查。在新增規則不符合語法規則時,儲存退出時會提示給我們錯誤資訊;配置好後,可以用切換到您授權的普通使用者下,通過sudo -l來檢視哪些命令是可以執行的或禁止的;

/etc/sudoers 檔案中每行是一個規則,前面帶有#號可以當作是註釋的內容,並不執行;如果規則很長,可以寫在多列上,可以用\號來續行。

/etc/sudoers 的規則可分為兩類;一類是授權規則,另一類是別名定義;別名定義並不是必須的,但授權規則是必須的;

進入root使用者,開啟sudoers檔案

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults    env_reset
Defaults    mail_badpass
Defaults    secure_path
="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" # Host alias specification # User alias specification # Cmnd alias specification # User privilege specification root ALL=(ALL:ALL) ALL # Allow members of group sudo to execute any command %sudo ALL=(ALL:ALL) ALL # See sudoers(5) for more
information on "#include" directives: #includedir /etc/sudoers.d

例項1:普通使用者lin新增sudo許可權,在“root ALL=(ALL)ALL”這一行下面,加入如下圖所示的一行(使用者名稱 ALL=(ALL) ALL),並儲存。

lin    ALL=(ALL:ALL) ALL

例項2:如何想讓普通使用者lin具有/etc/init.d/nagios指令碼重啟的許可權

lin ALL=NOPASSWD:/etc/init.d/nagios restart

例項3:讓普通使用者lin具有所有超級使用者的許可權而又不用輸入密碼

lin ALL=(ALL)NOPASSWD:ALL