首頁 > 軟體

Linux下sudo設定和用法

2020-06-16 17:31:37

介紹

本篇文章主要介紹sudo設定和用法,為了給某個使用者控制許可權比如執行某個命令或者關機操作等,伺服器管理員通常會給這個使用者設定sudo,接下來就來詳細介紹具體的設定方法。

Linux環境:CentOS 6.7

結構說明

可以通過編輯檔案/etc/sudoers來設定,通常使用visudo命令來進行修改,因為如果你修改的格式不符合它會進行提示。接下來就通過一個格式來了解它

<user> <host> = [<operator user> [<par>]] <command list>
chenmh localhost = (root)    NOPASSWD: /bin/mkdir test

<user>:指的是具體的使用者或者使用者別名,如果使用%user指的是使用者組。

<host>:指的是具體的host(可以是機器名也可以是ip)或者是host別名,ALL代表所有的host。

<operator user>:可選,指定可以用呼叫哪個使用者的來執行,ALL代表呼叫root使用者來執行。這裡要說明一下root使用者的許可權代表什麼意思,比如一個目錄的所有者是root,那麼必須具備root使用者的許可權才能執行相關操作,比如上面的chenmh使用者如果它設定的是其它使用者比如它自己,那麼它在root所有者的目錄下面是沒許可權執行操作的。同樣如果這裡設定的是哪個使用者那麼比如mkdir建立的資料夾就的所有者就是哪個使用者。預設不指定代表使用ALL

<par>:可選,指定引數,通常使用NOPASSWD(代表該使用者在執行sudo的時候不需要再輸入自己的密碼)。

<command list>:指定的具體命令或者是命令別名或者是ALL,ALL代表所有許可權。

說明: [<operator user> [<par>]]這兩個選項是可以選的可以不指定,如果不指定預設是呼叫root使用者執行,但是使用sudo必須輸入使用者自己的密碼

組態檔

接下來詳細來看看它的組態檔,它的組態檔以及很詳細的告訴了我們該怎樣使用,在前面一部分是範例怎樣將一組許可權建立組別名,注意別名需要大寫,

## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
##
## Examples are provided at the bottom of the file for collections
## of related commands, which can then be delegated out to particular
## users or groups.
## 
## This file must be edited with the 'visudo' command.
##可以將多個host設定成一個host別名 ## Host Aliases ## Groups of machines. You may prefer to use hostnames (perhaps
using ## wildcards for entire domains) or IP addresses instead. Host_Alias FILESERVERS = 192.168.137.40,192.168.137.30 # Host_Alias MAILSERVERS = smtp, smtp2
##將多個user設定成一個user別名 ## User Aliases ## These aren
't often necessary, as you can use regular groups ## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname ## rather than USERALIAS User_Alias ADMINS = chenmh ###接下來是命令別名,就是將一組命令放在一起,這樣可以簡便設定 ## Command Aliases ## These are groups of related commands... ## Networking # Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool ## Installation and management of software # Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum ## Services # Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig ## Updating the locate database # Cmnd_Alias LOCATE = /usr/bin/updatedb ## Storage # Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount ## Delegating permissions # Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp ## Processes # Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall ## Drivers # Cmnd_Alias DRIVERS = /sbin/modprobe
###這個是我自己設定的命令別名,將mkdir和rm命令設定在一起分配給某個使用者 ##userdefin Cmnd_Alias OTHERS
= /bin/mkdir, /bin/rm # Defaults specification # # Disable "ssh hostname sudo <cmd>", because it will show the password in clear. # You have to run "ssh -t hostname sudo <cmd>". # Defaults requiretty # # Refuse to run if unable to disable echo on the tty. This setting should also be # changed in order to be able to use sudo without a tty. See requiretty above. # Defaults !visiblepw # # Preserving HOME has security implications since many programs # use it when searching for configuration files. Note that HOME # is already set when the the env_reset option is enabled, so # this option is only effective for configurations where either # env_reset is disabled or HOME is present in the env_keep list. # Defaults always_set_home Defaults env_reset Defaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS" Defaults env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE" Defaults env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES" Defaults env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE" Defaults env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY" # # Adding HOME to env_keep may enable a user to run unrestricted # commands via sudo. # # Defaults env_keep += "HOME" Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin ## Next comes the main part: which users can run what software on ## which machines (the sudoers file can be shared between multiple ## systems). ## Syntax: ## ## user MACHINE=COMMANDS ## ## The COMMANDS section may have other options added to it. ## ## Allow root to run any commands anywhere root ALL=(ALL) ALL ## Allows members of the 'sys' group to run networking, software, ## service management apps and more. # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS ###sys這個組中的使用者可以執行相關的命令組的許可權,多個命令組用逗號分隔 ## Allows people in group wheel to run all commands # %wheel ALL=(ALL) ALL ## Same thing without a password # %wheel ALL=(ALL) NOPASSWD: ALL ## Allows members of the users group to mount and unmount the ## cdrom as root # %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom ###users組的使用者可以執行掛載和解除安裝/mnt/cdrom目錄的許可權 ## Allows members of the users group to shutdown this system # %users localhost=/sbin/shutdown -h now ###users組的使用者可以執行關機命令 ## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment) #includedir /etc/sudoers.d 

案例

1.使用者別名組中的使用者可以在FILESERVERS這組host裡面呼叫root使用者執行OTHERS命令組的許可權,多個命令組用逗號分隔

ADMINS  FILESERVERS=(ALL)    NOPASSWD:OTHERS

2.使用者chenmh這個使用者可以在本機以呼叫root使用者來執行所有的命令

chenmh localhost=(root) NOPASSWORD:ALL

3.使用者chenmh可以呼叫root使用者建立目錄test,也只能建立test目錄

chenmh ALL=(ALL)    NOPASSWD: /bin/mkdir test

4.dev組的使用者可以執行關機shutdown命令

%dev ALL=(ALL)    NOPASSWD:/sbin/shutdown

快捷操作 

 1.檢視當前使用者具備的sudo許可權

sudo -l
User chenmh may run the following commands on this host:
    (root) NOPASSWD: /bin/mkdir, /bin/rm

總結

設定sudo記得使用visudo命令,如果設定錯誤了儲存的時候它會有提示。 

本文永久更新連結地址http://www.linuxidc.com/Linux/2016-09/135617.htm


IT145.com E-mail:sddin#qq.com