sudo命令详解,linux sudo命令参数选项、基本用法、高级应用,在/etc/sudo
sudo命令详解,linux sudo命令参数选项、基本用法、高级应用,在/etc/sudo
sudo命令用来以其他身份来执行命令,预设的身份为root。在/etc/sudoers
中设置了可执行sudo指令的用户。若其未经授权的用户企图使用sudo,则会发出警告的邮件给管理员。用户使用sudo时,必须先输入密码,之后有5分钟的有效期限,超过期限则必须重新输入密码。
语法
sudo(选项)(参数)
选项
-b:在后台执行指令; -h:显示帮助; -H:将HOME环境变量设为新身份的HOME环境变量; -k:结束密码的有效期限,也就是下次再执行sudo时便需要输入密码;。 -l:列出目前用户可执行与无法执行的指令; -p:改变询问密码的提示符号; -s<shell>:执行指定的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来配置这些环境变量。
实例基本用法
sudo命令使用
$ sudo ls [sudo] password for hnlinux: hnlinux is not in the sudoers file. This incident will be reported.
指定用户执行命令
# sudo -u userb ls -l
显示sudo设置
$ sudo -L //显示sudo设置 Available options in a sudoers ``Defaults'' line: syslog: Syslog facility if syslog is being used for logging syslog_goodpri: Syslog priority to use when user authenticates successfully syslog_badpri: Syslog priority to use when user authenticates unsuccessfully long_otp_prompt: Put OTP prompt on its own line ignore_dot: Ignore '.' in $PATH mail_always: Always send mail when sudo is run mail_badpass: Send mail if user authentication fails mail_no_user: Send mail if the user is not in sudoers mail_no_host: Send mail if the user is not in sudoers for this host mail_no_perms: Send mail if the user is not allowed to run a command tty_tickets: Use a separate timestamp for each user/tty combo lecture: Lecture user the first time they run sudo lecture_file: File containing the sudo lecture authenticate: Require users to authenticate by default root_sudo: Root may run sudo log_host: Log the hostname in the (non-syslog) log file log_year: Log the year in the (non-syslog) log file shell_noargs: If sudo is invoked with no arguments, start a shell set_home: Set $HOME to the target user when starting a shell with -s always_set_home: Always set $HOME to the target user's home directory path_info: Allow some information gathering to give useful error messages fqdn: Require fully-qualified hostnames in the sudoers file insults: Insult the user when they enter an incorrect password requiretty: Only allow the user to run sudo if they have a tty env_editor: Visudo will honor the EDITOR environment variable rootpw: Prompt for root's password, not the users's runaspw: Prompt for the runas_default user's password, not the users's targetpw: Prompt for the target user's password, not the users's use_loginclass: Apply defaults in the target user's login class if there is one set_logname: Set the LOGNAME and USER environment variables stay_setuid: Only set the effective uid to the target user, not the real uid preserve_groups: Don't initialize the group vector to that of the target user loglinelen: Length at which to wrap log file lines (0 for no wrap) timestamp_timeout: Authentication timestamp timeout passwd_timeout: Password prompt timeout passwd_tries: Number of tries to enter a password umask: Umask to use or 0777 to use user's logfile: Path to log file mailerpath: Path to mail program mailerflags: Flags for mail program mailto: Address to send mail to mailfrom: Address to send mail from mailsub: Subject line for mail messages badpass_message: Incorrect password message timestampdir: Path to authentication timestamp dir timestampowner: Owner of the authentication timestamp dir exempt_group: Users in this group are exempt from password and PATH requirements passprompt: Default password prompt passprompt_override: If set, passprompt will override system prompt in all cases. runas_default: Default user to run commands as secure_path: Value to override user's $PATH with editor: Path to the editor for use by visudo listpw: When to require a password for 'list' pseudocommand verifypw: When to require a password for 'verify' pseudocommand noexec: Preload the dummy exec functions contained in 'noexec_file' noexec_file: File containing dummy exec functions ignore_local_sudoers: If LDAP directory is up, do we ignore local sudoers file closefrom: File descriptors >= %d will be closed before executing a command closefrom_override: If set, users may override the value of `closefrom' with the -C option setenv: Allow users to set arbitrary environment variables env_reset: Reset the environment to a default set of variables env_check: Environment variables to check for sanity env_delete: Environment variables to remove env_keep: Environment variables to preserve role: SELinux role to use in the new security context type: SELinux type to use in the new security context askpass: Path to the askpass helper program env_file: Path to the sudo-specific environment file sudoers_locale: Locale to use while parsing sudoers visiblepw: Allow sudo to prompt for a password even if it would be visisble pwfeedback: Provide visual feedback at the password prompt when there is user input fast_glob: Use faster globbing that is less accurate but does not access the filesystem umask_override: The umask specified in sudoers will override the user's, even if it is more permissive
以root权限执行上一条命令
$ sudo !!
以特定用户身份进行编辑文本
$ sudo -u uggc vi ~www/index.html //以 uggc 用户身份编辑 home 目录下www目录中的 index.html 文件
列出目前的权限
sudo -l
列出 sudo 的版本资讯
sudo -V
评论暂时关闭