Linux sudo用法與配置

Linux環境:CentOS 6.7linux

結構說明

能夠經過編輯文件/etc/sudoers來配置,一般使用visudo命令來進行修改,由於若是你修改的格式不符合它會進行提示。接下來就經過一個格式來了解它app

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

<user>:指的是具體的用戶或者用戶別名,若是使用%user指的是用戶組。dom

<host>:指的是具體的host(能夠是機器名也能夠是ip)或者是host別名,ALL表明全部的host。ssh

<operator user>:可選,指定能夠用調用哪一個用戶的來執行,ALL表明調用root用戶來執行。這裏要說明一下root用戶的權限表明什麼意思,好比一個目錄的全部者是root,那麼必須具有root用戶的權限才能執行相關操做,好比上面的chenmh用戶若是它配置的是其它用戶好比它本身,那麼它在root全部者的目錄下面是沒權限執行操做的。一樣若是這裏配置的是哪一個用戶那麼好比mkdir建立的文件夾就的全部者就是哪一個用戶。默認不指定表明使用ALLide

<par>:可選,指定參數,一般使用NOPASSWD(表明該用戶在執行sudo的時候不須要再輸入本身的密碼)。ui

<command list>:指定的具體命令或者是命令別名或者是ALL,ALL表明全部權限。this

說明: [<operator user> [<par>]]這兩個選項是能夠選的能夠不指定,若是不指定默認是調用root用戶執行,可是使用sudo必須輸入用戶本身的密碼rest

配置文件

接下來詳細來看看它的配置文件,它的配置文件以及很詳細的告訴了咱們該怎樣使用,在前面一部分是示例怎樣將一組權限建立組別名,注意別名須要大寫,code

## 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命令組的權限,多個命令組用逗號分隔ip

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

5.chenmh的用戶sudo免密登陸使用(經常使用)

chenmh      ALL=(ALL)       NOPASSWD:ALL


快捷操做 

 1.查看當前用戶具有的sudo權限

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

總結

配置sudo記得使用visudo命令,若是配置錯誤了保存的時候它會有提示。

相關文章
相關標籤/搜索