sudo的使用

来源:互联网 发布:淘宝阿里健康是真的吗 编辑:程序博客网 时间:2024/05/22 10:49

sudo是linux下常用的允许普通用户使用超级用户权限的工具

它的主要配置文件是sudoers,linux下通常在/etc目录下,如果是solaris,缺省不装sudo的,编译安装后通常在安装目录的etc目录下,不过不管sudoers文件在哪儿,sudo都提供了一个编辑该文件的命令:visudo来对该文件进行修改。强烈推荐使用该命令修改sudoers,因为它会帮你校验文件配置是否正确,如果不正确,在保存退出时就会提示你哪段配置出错的。

言归正传,下面介绍如何配置sudoers

首先写sudoers的缺省配置:

#############################################################
# sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.
#

# Host alias specification

# User alias specification

# Cmnd alias specification

# Defaults specification

# User privilege specification
root    ALL=(ALL) ALL

# Uncomment to allow people in group wheel to run all commands
# %wheel        ALL=(ALL)       ALL

# Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL

# Samples
# %users ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users localhost=/sbin/shutdown -h now
##################################################################

1. 最简单的配置,让普通用户support具有root的所有权限
执行visudo之后,可以看见缺省只有一条配置:
root    ALL=(ALL) ALL
那么你就在下边再加一条配置:
support ALL=(ALL) ALL

这样,普通用户support就能够执行root权限的所有命令

以support用户登录之后,执行:
sudo su -
然后输入support用户自己的密码,就可以切换成root用户了

2. 让普通用户support只能在某几台服务器上,执行root能执行的某些命令
首先需要配置一些Alias,这样在下面配置权限时,会方便一些,不用写大段大段的配置。Alias主要分成4种
Host_Alias
Cmnd_Alias
User_Alias
Runas_Alias

1) 配置Host_Alias:就是主机的列表
Host_Alias      HOST_FLAG = hostname1, hostname2, hostname3
2) 配置Cmnd_Alias:就是允许执行的命令的列表
Cmnd_Alias      COMMAND_FLAG = command1, command2, command3
3) 配置User_Alias:就是具有sudo权限的用户的列表
User_Alias USER_FLAG = user1, user2, user3
4) 配置Runas_Alias:就是用户以什么身份执行(例如root,或者oracle)的列表
Runas_Alias RUNAS_FLAG = operator1, operator2, operator3
5) 配置权限
配置权限的格式如下:
USER_FLAG HOST_FLAG=(RUNAS_FLAG) COMMAND_FLAG
如果不需要密码验证的话,则按照这样的格式来配置
USER_FLAG HOST_FLAG=(RUNAS_FLAG) NOPASSWD: COMMAND_FLAG

配置示例:
############################################################################
# sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.
#

# Host alias specification
Host_Alias      EPG = 192.168.1.1, 192.168.1.2

# User alias specification

# Cmnd alias specification
Cmnd_Alias      SQUID = /opt/vtbin/squid_refresh, /sbin/service, /bin/rm

# Defaults specification

# User privilege specification
root    ALL=(ALL) ALL
support EPG=(ALL) NOPASSWD: SQUID

# Uncomment to allow people in group wheel to run all commands
# %wheel        ALL=(ALL)       ALL

# Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL

# Samples
# %users ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users localhost=/sbin/shutdown -h now
###############################################################


以系统管理者的身份执行指令,也就是说,经由 sudo 所执行的指令就好像是 root 亲自执行

参数:

-V 显示版本编号

-h 会显示版本编号及指令的使用方式说明

-l 显示出自己(执行 sudo 的使用者)的权限

-v 因为 sudo 在第一次执行时或是在 N 分钟内没有执行(N 预设为五)会问密码,这个参数是重新做一次确认,如果超过 N 分钟,也会问密码

-k 将会强迫使用者在下一次执行 sudo 时问密码(不论有没有超过 N 分钟)

-b 将要执行的指令放在背景执行

-p prompt 可以更改问密码的提示语,其中 %u 会代换为使用者的帐号名称, %h 会显示主机名称

-u username/#uid 不加此参数,代表要以 root 的身份执行指令,而加了此参数,可以以 username 的身份执行指令(#uid 为该 username 的使用者号码)

-s 执行环境变数中的 SHELL 所指定的 shell ,或是 /etc/passwd 里所指定的 shell

-H 将环境变数中的 HOME (家目录)指定为要变更身份的使用者家目录(如不加 -u 参数就是系统管理者 root )

command 要以系统管理者身份(或以 -u 更改为其他人)执行的指令

范例:

sudo -l 列出目前的权限

sudo -V 列出 sudo 的版本号

例如:
more /etc/sudoers
# sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.
#

# Host alias specification

# User alias specification

# Cmnd alias specification

# Defaults specification

# User privilege specification
root    ALL=(ALL) ALL

# Uncomment to allow people in group wheel to run all commands
# %wheel        ALL=(ALL)       ALL

# Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL

# Samples
# %users ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users localhost=/sbin/shutdown -h now
Cmnd_AliasCOMMENT=/data/comment/cmtwebstart.sh,/data/comment/cmtwebstop.sh,/usr/local/apache2.0.54-cmt/bin/apachectl,/usr/local/res
in3.0.14-cmt/bin/httpd.sh,/usr/java/bin/java
comment ALL=(root) NOPASSWD: COMMENT

Cmnd_AliasJIAOYAXIN=/home/jiaoyaxin/edustart.sh,/home/jiaoyaxin/edustop.sh,/usr/local/resin3.0.18-edu/bin/httpd.sh,/usr/java/bin/ja
va
jiaoyaxin ALL=(root) NOPASSWD: JIAOYAXIN

Cmnd_AliasHKFORUMS=/home/hkforums/hkstart.sh,/home/hkforums/hkstop.sh,/usr/local/apache2.0.59-hk/bin/apachectl,/usr/local/resin3.0.
14-hk/bin/httpd.sh,/usr/java/bin/java
hkforums ALL=(root) NOPASSWD: HKFORUMS

Cmnd_Alias SHOP4U=/data/shop4u/start.sh,/data/shop4u/stop.sh,/usr/local/resin3.0.18-shop4u/bin/httpd.sh,/usr/java/bin/java
shop4u ALL=(root) NOPASSWD:SHOP4U

Cmnd_AliasBBSTEST3=/home/bbstest3/resin-staticstart.sh,/home/bbstest3/resin-staticstop.sh,/usr/local/resin3.0.18-static/bin/httpd.
sh,/usr/local/apache2.0.54-club/bin/apachectl,/usr/local/resin3.0.18-club/bin/httpd.sh
bbstest3 ALL=(root) NOPASSWD:BBSTEST3

Cmnd_AliasAPACHE=/usr/local/apache2/bin/googlestop,/usr/local/apache2/bin/googlestart,/usr/local/apache2/bin/engsearchstart,/usr/lo
cal/apache2/bin/engsearchstop,/usr/bin/java
Cmnd_Alias PPAYMAIL=/usr/local/apache2/bin/httpd,/usr/local/resin-ppaymail-3.0.14/bin/httpd.sh,/usr/bin/java
Cmnd_Alias RESIN=/usr/local/resin-3.0.14/bin/httpd.sh
Cmnd_Alias ADCRM=/usr/local/resin-adcrm-3.0.14/bin/httpd.sh,/home/adcrm/resinstart,/home/adcrm/resinstop
houjixin ALL=(root) NOPASSWD:APACHE,RESIN
kongxiuli ALL=(root) NOPASSWD:RESIN,APACHE
adcrm ALL=(root) NOPASSWD:ADCRM,APACHE,RESIN
jiaoyaxin ALL=(root) NOPASSWD:PPAYMAIL


[comment@bbs comment]# more cmtwebstart.sh
#!/bin/sh
RESIN_HOME=/usr/local/resin3.0.14-cmt
APACHE_HOME=/usr/local/apache2.0.54-cmt
sudo ${RESIN_HOME}/bin/httpd.sh -pid srun5.pid -server e start
sudo ${RESIN_HOME}/bin/httpd.sh -pid srun6.pid -server f start
sudo ${APACHE_HOME}/bin/apachectl start
[comment@bbs comment]# more cmtwebstop.sh
#!/bin/sh
RESIN_HOME=/usr/local/resin3.0.14-cmt
APACHE_HOME=/usr/local/apache2.0.54-cmt
sudo ${RESIN_HOME}/bin/httpd.sh -pid srun5.pid -server e stop
sudo ${RESIN_HOME}/bin/httpd.sh -pid srun6.pid -server f stop
sudo ${APACHE_HOME}/bin/apachectl stop
这样,变通用户comment就能使用root的权限执行那sudo中定义的那些命令!!!

原创粉丝点击