Linux sudo命令详解

sudo命令用来以其他身份来执行命令,预设的身份为root。在/etc/sudoers中设置了可执行sudo指令的用户。若其未经授权的用户企图使用sudo,则会发出警告的邮件给管理员。用户使用sudo时,必须先输入密码,之后有5分钟的有效期限,超过期限则必须重新输入密码。

语法 sudo(选项)(参数) 选项
-b:在后台执行指令;
-h:显示帮助;
-H:将HOME环境变量设为新身份的HOME环境变量;
-k:结束密码的有效期限,也就是下次再执行sudo时便需要输入密码;。
-l:列出目前用户可执行与无法执行的指令;
-p:改变询问密码的提示符号;
-s:执行指定的shell;
-u<用户>:以指定的用户作为新的身份。若不加上此参数,则预设以root作为新的身份;
-v:延长密码有效期限5分钟;
-V :显示版本信息。 参数 指令:需要运行的指令和对应的参数。

实例
配置sudo必须通过编辑/etc/sudoers文件,而且只有超级用户才可以修改它,还必须使用visudo编辑。之所以使用visudo有两个原因,一是它能够防止两个用户同时修改它;二是它也能进行有限的语法检查。所以,即使只有你一个超级用户,你也最好用visudo来检查一下语法。
visudo默认的是在vi里打开配置文件,用vi来修改文件。我们可以在编译时修改这个默认项。visudo不会擅自保存带有语法错误的配置文件,它会提示你出现的问题,并询问该如何处理,就像:

sudoers file: syntax error, line 22 <
此时我们有三种选择:键入“e”是重新编辑,键入“x”是不保存退出,键入“Q”是退出并保存。如果真选择Q,那么sudo将不会再运行,直到错误被纠正。
现在,我们一起来看一下神秘的配置文件,学一下如何编写它。让我们从一个简单的例子开始:让用户Foobar可以通过sudo执行所有root可执行的命令。以root身份用visudo打开配置文件,可以看到类似下面几行:

Runas alias specification

User privilege specificationroot ALL=(ALL)ALL

我们一看就明白个差不多了,root有所有权限,只要仿照现有root的例子就行,我们在下面加一行(最好用tab作为空白):
foobar ALL=(ALL) ALL
保存退出后,切换到foobar用户,我们用它的身份执行命令:

[foobar@localhost ~]$ ls /root ls: /root: 权限不够 [foobar@localhost ~]$
sudo ls /root PassWord: anaconda-ks.cfg Desktop install.log
install.log.syslog

好了,我们限制一下foobar的权利,不让他为所欲为。比如我们只想让他像root那样使用ls和ifconfig,把那一行改为:

foobar localhost= /sbin/ifconfig, /bin/ls

再来执行命令:

[foobar@localhost ~]$ sudo head -5 /etc/shadow Password: Sorry, user
foobar is not allowed to execute ‘/usr/bin/head -5 /etc/shadow’ as
root on localhost.localdomain. [foobar@localhost ~]$ sudo
/sbin/ifconfigeth0 Linkencap:Ethernet HWaddr 00:14:85:EC:E9:9B…

现在让我们来看一下那三个ALL到底是什么意思。第一个ALL是指网络中的主机,我们后面把它改成了主机名,它指明foobar可以在此主机上执行后面的命令。第二个括号里的ALL是指目标用户,也就是以谁的身份去执行命令。最后一个ALL当然就是指命令名了。例如,我们想让foobar用户在linux主机上以jimmy或rene的身份执行kill命令,这样编写配置文件:

foobar linux=(jimmy,rene) /bin/kill

但这还有个问题,foobar到底以jimmy还是rene的身份执行?这时我们应该想到了sudo -u了,它正是用在这种时候。 foobar可以使用sudo -u jimmy kill PID或者sudo -u rene kill PID,但这样挺麻烦,其实我们可以不必每次加-u,把rene或jimmy设为默认的目标用户即可。再在上面加一行:

Defaults:foobar runas_default=rene

Defaults后面如果有冒号,是对后面用户的默认,如果没有,则是对所有用户的默认。就像配置文件中自带的一行:

Defaults env_reset

另一个问题是,很多时候,我们本来就登录了,每次使用sudo还要输入密码就显得烦琐了。我们可不可以不再输入密码呢?当然可以,我们这样修改配置文件:

foobar localhost=NOPASSWD: /bin/cat, /bin/ls

再来sudo一下:

[foobar@localhost ~]$ sudo ls /rootanaconda-ks.cfg Desktop install.log
install.log.syslog

当然,你也可以说“某些命令用户foobar不可以运行”,通过使用!操作符,但这不是一个好主意。因为,用!操作符来从ALL中“剔出”一些命令一般是没什么效果的,一个用户完全可以把那个命令拷贝到别的地方,换一个名字后再来运行。
日志与安全
sudo为安全考虑得很周到,不仅可以记录日志,还能在有必要时向系统管理员报告。但是,sudo的日志功能不是自动的,必须由管理员开启。这样来做:

touch /var/log/sudo
vi /etc/syslog.conf

在syslog.conf最后面加一行(必须用tab分割开)并保存:
local2.debug /var/log/sudo
重启日志守候进程,

ps aux grep syslogd

把得到的syslogd进程的PID(输出的第二列是PID)填入下面:

kill –HUP PID

这样,sudo就可以写日志了:

[foobar@localhost ~]$ sudo ls /rootanaconda-ks.cfg
Desktop install.log

install.log.syslog

$cat /var/log/sudoJul 28 22:52:54 localhost sudo: foobar :

TTY=pts/1 ; pwd=/home/foobar ; USER=root ; command=/bin/ls /root

不过,有一个小小的“缺陷”,sudo记录日志并不是很忠实:

[foobar@localhost ~]$ sudo cat /etc/shadow > /dev/null

cat /var/log/sudo…Jul 28 23:10:24 localhost sudo: foobar :
TTY=pts/1 ;

PWD=/home/foobar ; USER=root ; COMMAND=/bin/cat /etc/shadow

重定向没有被记录在案!为什么?因为在命令运行之前,shell把重定向的工作做完了,sudo根本就没看到重定向。这也有个好处,下面的手段不会得逞:

[foobar@localhost ~]$ sudo ls /root > /etc/shadowbash: /etc/shadow:
权限不够

sudo 有自己的方式来保护安全。以root的身份执行sudo-V,查看一下sudo的设置。因为考虑到安全问题,一部分环境变量并没有传递给sudo后面的命令,或者被检查后再传递的,比如:PATH,HOME,SHELL等。当然,你也可以通过sudoers来配置这些环境变量。
sudoers文件解释
Sudoers allows particular users to run various commands as the root user, without needing the root password.
该文件允许特定用户像root用户一样使用各种各样的命令,而不需要root用户的密码
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.
该文件必须使用"visudo"命令编辑

Host Aliases

主机别名

Groups of machines. You may prefer to use hostnames (perhap using wildcards for entire domains) or IP addresses instead. 对于一组服务器,你可能会更喜欢使用主机名(可能是全域名的通配符)或IP地址代替,这时可以配置主机别名 # Host_Alias FILESERVERS = fs1, fs2 Host_Alias MAILSERVERS = smtp, smtp2 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 = jsmith, mikem

Command Aliases

#These are groups of related commands… ## 指定一系列相互关联的命令(当然可以是一个)的别名,通过赋予该别名sudo权限, ## 可以通过sudo调用所有别名包含的命令,下面是一些示例

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/sbin/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
#环境变量的相关配置

Defaults specification # # Disable "ssh hostname sudo ", because it will show the password in clear. # You have to run "ssh -t hostname sudo ".

Defaults requiretty
Defaults env_reset
Defaults env_keep = “COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR
LS_COLORS MAIL PS1 PS2 QTDIR USERNAME
LANG LC_ADDRESS LC_CTYPE LC_COLLATE LC_IDENTIFICATION
LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC
LC_PAPER LC_TELEPHONE LC_TIME LC_ALL LANGUAGE LINGUAS
_XKB_CHARSET XAUTHORITY”

Next comes the main part: which users can run what software on ## which machines (the sudoers file can be shared between multiple ## systems).

下面是规则配置:什么用户在哪台服务器上可以执行哪些命令(sudoers文件可以在多个系统上共享)

Syntax: #语法

user MACHINE=COMMANDS ## 用户 登录的主机=(可以变换的身份) 可以执行的命令
The COMMANDS section may have other options added to it.
命令部分可以附带一些其它的选项
Allow root to run any commands anywhere ## 允许root用户执行任意路径下的任意命令

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中户组中的用户使用NETWORKING等所有别名中配置的命令
Allows people in group wheel to run all commands

%wheel ALL=(ALL) ALL

允许wheel用户组中的用户执行所有命令
Same thing without a password
允许wheel用户组中的用户在不输入该用户的密码的情况下使用所有命令

%wheel ALL=(ALL) NOPASSWD: ALL

Allows members of the users group to mount and unmount the
cdrom as root
允许users用户组中的用户像root用户一样使用mount、unmount、chrom命令

%users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

Allows members of the users group to shutdown this system

%users localhost=/sbin/shutdown -h now

允许users用户组中的用户像root用户一样使用shutdown命令

实际案例演示 实例1:让普通用户fieldyang具有/etc/init.d/nagios脚本重启的权限,可以在/etc/sudoers添加如下设置:

[root@test ~]# visudo fieldyang ALL=NOPASSWD:/etc/init.d/nagios restart

实例2:让普通用户fieldyang具有所有超级用户的权限而又不用输入密码 [root@test ~]# visudo fieldyang ALL=(ALL)NOPASSWD:ALL [fieldyang@test ~]#sudo su - [fieldyang@test ~]#pwd /root

实例3:针对MySQL数据库的设置,让test组中的test用户具备/etc/init.d/mysqld的权限
######################## mysql ################

  1. [root@test ~]# groupadd test [root@test ~]# useradd -g test -m -d /home/test -s /bin/bash test [root@test ~]# passwd test
  2. [root@test ~]# visudo

test ALL=(ALL) NOPASSWD: /etc/init.d/mysqld

test ALL=(ALL) /etc/init.d/mysqld
3. start/stop mysql
3.1) start mysql
login test
[root@test ~]# su test
[test@test ~]$ sudo /etc/init.d/mysqld start
3.2) stop mysql
login test
[root@test ~]# su test
[test@test ~]$ sudo /etc/init.d/mysqld stop

实例4:针对tomcat的设置,让test组中的test用户具备tomcat操作的权限
######################## tomcat ################
1.
[root@test ~]# groupadd test
[root@test ~]# useradd -g test -m -d /home/test -s /bin/bash test
[root@test ~]# passwd test
2.
[root@test ~]# visudo
# test ALL=(ALL) /usr/local/tomcat/bin/shutdown.sh,/usr/local/tomcat/bin/startup.sh
test ALL=(ALL) NOPASSWD: /usr/local/tomcat/bin/shutdown.sh,/usr/local/tomcat/bin/startup.sh
3.
[root@test ~]# vim /usr/local/tomcat/bin/catalina.sh

JDK

export JAVA_HOME=/usr/local/jdk
export JRE_HOME=$JAVA_HOME/jre
  1. start/stop tomcat
    4.1) start tomcat
    login test
    [root@test ~]# su test
    [test@test ~]$ sudo /usr/local/tomcat/bin/startup.sh
    [test@test ~]$ ss -ntlup | grep java
    [test@test ~]$ curl -I http://localhost:8080
    4.2) stop tomcat
    login test
    [root@test ~]# su test
    [test@test ~]$ sudo /usr/local/tomcat/bin/shutdown.sh
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值