介紹 本篇文章主要介紹sudo配置和用法,為了給某個用戶控制許可權比如執行某個命令或者關機操作等,伺服器管理員通常會給這個用戶配置sudo,接下來就來詳細介紹具體的配置方法。 環境:centos6.7 結構說明 可以通過編輯文件/etc/sudoers來配置,通常使用visudo命令來進行修改,因為如 ...
介紹
本篇文章主要介紹sudo配置和用法,為了給某個用戶控制許可權比如執行某個命令或者關機操作等,伺服器管理員通常會給這個用戶配置sudo,接下來就來詳細介紹具體的配置方法。
環境:centos6.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命令,如果配置錯誤了保存的時候它會有提示。
備註: 作者:pursuer.chen 博客:http://www.cnblogs.com/chenmh 本站點所有隨筆都是原創,歡迎大家轉載;但轉載時必須註明文章來源,且在文章開頭明顯處給明鏈接。 《歡迎交流討論》 |