
正文
linux常用命令:sudo 命令
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
sudo命令用来以其他身份来执行命令,预设的身份为root。
1.命令格式:
sudo [参数] [命令]
2.命令功能:
功能: sudo可以针对单个命令授予临时权限。用户也可以通过su切换到root用户运行命令,su启动一个root shell允许用户运行之后的所有的命令。
1)sudo与su的不同之处:sudo仅在需要时授予用户权限,减少了用户因为错误执行命令损坏系统的可能性;sudo也可以用来以其他用户身份执行命令。此外,sudo可以记录用户执行的命令,以及失败的特权获取。
2)提醒:在/etc/sudoers中设置了可执行sudo指令的用户。若其未经授权的用户企图使用sudo,则会发出警告的邮件给管理员。用户使用sudo时,必须先输入密码,之后有5分钟的有效期限,超过期限则必须重新输入密码。
3)[root@localhost etc]# cat sudoers 查看/etc下的只有root才能打开的文件配置文件sudoers
[xiong@localhost ~]$ yum update mysql//yum的执行需要root权限已加载插件:fastestmirror, langpacks您需要 root 权限执行此命令。[xiong@localhost ~]$ sudo yum update mysql //默认情况下sudo获得root用户的权限We trust you have received the usual lecture from the local SystemAdministrator. It usually boils down to these three things: #) Respect the privacy of others. #) Think before you type. #) With great power comes great responsibility.[sudo] password for xiong:xiong 不在 sudoers 文件中。此事将被报告。//此时sunmeng还没有使用sudo的权限
4)给普通用使用sudo命令的授权:
## 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 (perhaps 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/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# Defaults specification## Disable "ssh hostname sudo <cmd>", because it will show the password in clear.# You have to run "ssh -t hostname sudo <cmd>".# 一些环境变量的相关配置,具体情况可见man soduersDefaults requiretty//设为默认的目标用户,69行是系统自带的,是对所有用户的默认,如果设sunmeng为默认目标用户,则语法为:Defaults:foobar runas_default=rene (冒号必不可少)Defaults env_resetDefaults 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"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).## 下面是规则配置:什么用户在哪台服务器上可以执行哪些命令(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//第一个ALL是指网络中的主机,我们后面把它改成了主机名。第二个括号里的ALL是指目标用户,也就是以谁的身份去执行命令。最后一个ALL当然就是指命令名了。## Allows members of the 'sys' group to run networking, software,## service management apps and more.## 允许sys中户组中的用户使用NETWORKING等所有别名中配置的命令# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS## Allows people in group wheel to run all commands## 允许wheel用户组中的用户执行所有命令%wheel ALL=(ALL) ALL## 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这台服务器# %users localhost=/sbin/shutdown -h now## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)## 读取放置在/etc/sudoers.d/文件夹中的文件(此处的#不意味着这是一个声明)#includedir /etc/sudoers.d
5)简单的测试:
visudo后得到的格式为: 账户名 主机名称=(可切换的身份) 可用的指令 //比如root账户 root ALL=(ALL) ALL 对于新增的账户就在下面加上 test ALL=(root) ALL //允许test用sudo命令执行root的所有命令 同理对于组使用者也是一个,但是要加上% %testgroup ALL=(root) ALL 需要注意的是:每次切换,你都需要密码才可以,使用如下命令可以避免输入密码: test ALL=(root) NOPASSWD: ALL ------------------------------- [root@localhost xiong]# visudo//这里的visudo不需要任何参数,因为它是打开sudoers的专属命令//改过的地方## user MACHINE=COMMANDS#### The COMMANDS section may have other options added to it.#### Allow root to run any commands anywhereroot ALL=(ALL) ALLxiong ALL=(ALL) ALL
[xiong@localhost ~]$ ls//当前身份执行命令Desktop Documents Downloads Music Pictures Public Templates Videos[xiong@localhost ~]$ sudo ls //利用root身份执行命令Desktop Documents Downloads Music Pictures Public Templates Videos[xiong@localhost ~]$ yum update mysql //当前身份执行命令,提醒需要root身份已加载插件:fastestmirror, langpacks您需要 root 权限执行此命令。[xiong@localhost ~]$ sudo yum update mysql//以root身份执行命令(在终端中第一次用这个命令时会提醒输入密码)已加载插件:fastestmirror, langpacksbase | :extras | :updates | :Loading mirror speeds from cached hostfile * base: mirrors.cqu.edu.cn * extras: mirrors.cqu.edu.cn * updates: mirrors.cqu.edu.cnNo packages marked for update
3.命令参数:
1) -b 在后台运行命令
2) -u user 以指定用户身份运行命令(或编辑文件)
3) -l 查看当前用户可以执行的命令
4) -v 更新用户的时间戳而不执行命令(注意:在输入密码之后,5分钟内执行sudo不需要再次输入密码。)
5) -V 显示sudo的版本信息
6) -i 以目标用户登录一个shell
4.使用实例:
示例1:指定用户名
命令: sudo -u root yum install mysql-devel
[xiong@localhost Desktop]$ sudo -u root yum install mysql-devel-u参数比较有用的地方是在多用户时,可以临时获得权限打开用户独占的文件已加载插件:fastestmirror, langpacksLoading mirror speeds from cached hostfile * base: mirrors.cqu.edu.cn * extras: mirrors.cqu.edu.cn * updates: mirrors.cqu.edu.cn正在解决依赖关系--> 正在检查事务......--> 解决依赖关系完成依赖关系解决============================================================================================================= Package 架构 版本 源 大小=============================================================================================================正在安装: mariadb-devel x86_64 :-.el7_2......作为依赖被升级: krb5-libs.x86_64 :-.el7_2 krb5-workstation.x86_64 :-.el7_2 openssl.x86_64 :.el7_2. openssl-libs.x86_64 :.el7_2. pcre.x86_64 :.el7_2. zlib.x86_64 :-.el7 完毕!
示例2:sudo命令与rpm命令结合使用的几个例子
[xiong@localhost Desktop]$ -.linux2..i386.rpm[sudo] password for xiong:未安装软件包 MySQL-embedded--.linux2..i386.rpm[xiong@localhost Desktop]$ -.linux2..i386.rpmMySQL-devellibaio.so.libaio.so.(LIBAIO_0.)libaio.so.(LIBAIO_0.)libc.so.libc.so.(GLIBC_2.)libc.so.(GLIBC_2.)libc.so.(GLIBC_2.1.3)libc.so.(GLIBC_2.)libc.so.(GLIBC_2.)libc.so.(GLIBC_2.3.3)libcrypt.so.libcrypt.so.(GLIBC_2.)libdl.so.libdl.so.(GLIBC_2.)libdl.so.(GLIBC_2.)libm.so.libm.so.(GLIBC_2.)libm.so.(GLIBC_2.)libncurses.so.libpthread.so.libpthread.so.(GLIBC_2.)libpthread.so.(GLIBC_2.)libpthread.so.(GLIBC_2.)libpthread.so.(GLIBC_2.3.2)librt.so.librt.so.(GLIBC_2.)rpmlib(CompressedFileNames) <= -rpmlib(PayloadFilesHavePrefix) <=
示例3:列出用户可以执行的命令
命令:sudo -l
[xiong@localhost Desktop]$ sudo -l匹配此主机上 xiong 的默认条目: requiretty, !visiblepw, always_set_home, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY", secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin用户 xiong 可以在该主机上运行以下命令: (ALL) ALL
示例4:以目标身份登录一个shell
命令:sudo -i
[root@localhost Desktop]# sudo -i[root@localhost ~]# sudo -i
示例5:显示详细的sudo的版本信息
命令: sudo -V
[root@localhost ~]# sudo -V Sudo 版本 1.8.6p7 当前选项:--build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu --program-prefix= --disable-dependency-tracking --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib64 --libexecdir=/usr/libexec --localstatedir=/var --sharedstatedir=/var/lib --mandir=/usr/share/man --infodir=/usr/share/info --prefix=/usr --sbindir=/usr/sbin --libdir=/usr/lib64 --docdir=/usr/share/doc/sudo-1.8.6p7 --with-logging=syslog --with-logfac=authpriv --with-pam --with-pam-login --with-editor=/bin/vi --with-env-editor --with-ignore-dot --with-tty-tickets --with-ldap --with-ldap-conf-file=/etc/sudo-ldap.conf --with-selinux --with-passprompt=[sudo] password for %p: --with-linux-audit --with-sssd Sudoers 策略插件版本 1.8.6p7 Sudoers 文件语法版本 Sudoers 路径:/etc/sudoers nsswitch 路径:/etc/nsswitch.conf ldap.conf 路径:/etc/sudo-ldap.conf ldap.secret 路径:/etc/ldap.secret 认证方法: 'pam' 若使用了 syslog,用于记录日志的 syslog 设施:authpriv 用户认证成功时使用的 syslog 优先级:notice 用户认证不成功时使用的 syslog 优先级:alert 忽略 $PATH 中的“.” 在用户不在 sudoers 列表中时发送邮件 对每个用户/终端组合使用独立的时间戳 在用户第一次运行 sudo 时向他致辞 默认要求用户认证 root 可以运行 sudo 总是将 $HOME 设为目标用户的主目录 允许收集一些信息,以提供有用的错误消息 只允许拥有终端的用户执行 sudo Visudo 将优先考虑 EDITOR 环境变量 设置 LOGNAME 和 USER 环境变量 日志文件折行的长度( 则不折行): 认证时间戳延时:5.0 分钟 密码提示延时:5.0 分钟 输入密码的尝试次数: 要使用的 umask,或 使用用户的: 邮件程序路径:/usr/sbin/sendmail 邮件程序标志:-t 发送邮件的地址:root 邮件消息的主题行:*** SECURITY information for %h *** 密码错误消息:Sorry, try again. 认证时间戳文件夹的路径:/var/db/sudo 默认密码提示:[sudo] password for %p: 运行命令的默认用户:root 覆盖用户的 $PATH 变量的值:/sbin:/bin:/usr/sbin:/usr/bin visudo 所使用的编辑器的路径:/bin/vi 何时为“list”伪命令请求密码:any 何时为“verify”伪命令请求密码:all >= 的文件描述符将会在执行命令前关闭 将环境重设为默认的变量集 要检查完整性的环境变量: TERM LINGUAS LC_* LANGUAGE LANG COLORTERM 要移除的环境变量: RUBYOPT RUBYLIB PYTHONUSERBASE PYTHONINSPECT PYTHONPATH PYTHONHOME TMPPREFIX ZDOTDIR READNULLCMD NULLCMD FPATH PERL5DB PERL5OPT PERL5LIB PERLLIB PERLIO_DEBUG JAVA_TOOL_OPTIONS SHELLOPTS GLOBIGNORE PS4 BASH_ENV ENV TERMCAP TERMPATH TERMINFO_DIRS TERMINFO _RLD* LD_* PATH_LOCALE NLSPATH HOSTALIASES RES_OPTIONS LOCALDOMAIN CDPATH IFS 要保留的环境变量: XAUTHORITY _XKB_CHARSET LINGUAS LANGUAGE LC_ALL LC_TIME LC_TELEPHONE LC_PAPER LC_NUMERIC LC_NAME LC_MONETARY LC_MESSAGES LC_MEASUREMENT LC_IDENTIFICATION LC_COLLATE LC_CTYPE LC_ADDRESS LANG USERNAME QTDIR PS2 PS1 MAIL LS_COLORS KDEDIR INPUTRC HISTSIZE HOSTNAME DISPLAY COLORS 解析 sudoers 时使用的区域设置:C 使用 zlib 压缩 I/O 日志 用于保存输入/输出日志的目录:/var/log/sudo-io 用于保存输入/输出日志的文件:%{seq} 在分配伪终端时向 utmp/utmpx 文件中添加一条记录 本地 IP 地址和网络掩码对: 192.168.0.15/255.255.255.0 fe80::20c:29ff:fe9f:bf8b/ffff:ffff:ffff:ffff:: Sudoers I/O plugin version 1.8.6p7






