用于切换当前用户身份到其他用户身份,变更时须输入所要变更的用户帐号与密码。
语法: su [-] username “-”可有可无,加上“-”的作用是在切换用户时初始化当前用户的各种环境变量。普通用户su不加username时就是直接切换到root用户。 eg:
[root@adai002 ~]# su - aming001 完全切换到用户aming001 上一次登录:三 6月 14 06:43:37 CST 2017pts/0 上 [aming001@adai002 ~]$ whoami 查看当前用户 aming001 [aming001@adai002 ~]$ pwd 查看当前目录绝对路径 /home/aming001 用户家目录 [aming001@adai002 ~]$ su 切换到root用户 密码: [root@adai002 aming001]# pwd /home/aming001 ----此时目录没变 [root@adai002 aming001]# exit 退出root到aming001 exit [aming001@adai002 ~]$ su - 完全切换到root用户 密码: 上一次登录:三 6月 14 06:45:11 CST 2017pts/0 上 [root@adai002 ~]# pwd /root ----家目录改变
应用环境: 在root用户登录时,直接以某个普通用户的身份去执行一个命令,适用于脚本中。
[root@adai002 ~]# su - aming001 -c "touch /tmp/1.txt" [root@adai002 ~]# ls -l /tmp/1.txt -rw-rw-r-- 1 aming001 aming001 0 6月 14 06:54 /tmp/1.txt
可以看到/tmp/1.txt的所有者和所属组都是aming001!
su的确为管理带来方便,通过切换到root下,能完成所有系统管理工具,只要把root的密码交给任何一个普通用户,他都能切换到root来完成所有的系统管理工作;但通过su切换到root后,也有不安全因素;比如系统有10个用户,而且都参与管理。如果这10个用户都涉及到超级权限的运用,做为管理员如果想让其它用户通过su来切换到超级权限的root,必须把root权限密码都告诉这10个用户;如果这10个用户都有root权限,通过root权限可以做任何事,这在一定程度上就对系统的安全造成了威协;想想Windows吧,简直就是恶梦;“没有不安全的系统,只有不安全的人”,我们绝对不能保证这 10个用户都能按正常操作流程来管理系统,其中任何一人对系统操作的重大失误,都可能导致系统崩溃或数据损失;所以su 工具在多人参与的系统管理中,并不是最好的选择,su只适用于一两个人参与管理的系统,毕竟su并不能让普通用户受限的使用;超级用户root密码应该掌握在少数用户手中,这绝对是真理!所以集权而治的存在还是有一定道理的;
用来以其他身份来执行命令,预设的身份为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命令编辑/etc/sudoers配置文件,操作方法同vi命令。当对多个命令设置速sudo权限时,需要用逗号加空格隔开。使用visudo有两个原因,一是它能够防止两个用户同时修改它;二是它也能进行有限的语法检查。所以,即使只有你一个超级用户,你也最好用visudo来检查一下语法。
[root@adai002 ~]# visudo 更改sudo配置文件 # This file MUST be edited with the 'visudo' command as root. 必须在root用户使用visudo命令! ## Allow root to run any commands anywhere root ALL=(ALL) ALL adai001 ALL=(ALL) /usr/bin/ls, /usr/bin/mv, /usr/bin/cat 对adai001用户进行授权(授权完毕后保存退出) [root@adai002 ~]# su - adai001 切换到普通用户 上一次登录:三 6月 14 10:23:01 CST 2017pts/1 上 [adai001@adai002 ~]$ ls /root/ ls: 无法打开目录/root/: 权限不够 (!!!即,普通用户没有访问root用户的权限) [adai001@adai002 ~]$ sudo /usr/bin/ls /root/ 使用sudo命下访问root用户 [sudo] password for adai001: anaconda-ks.cfg 访问成功!!! [adai001@adai002 ~]$ sudo /usr/bin/ls /root/ anaconda-ks.cfg 再次使用sudo命令时无需输入密码 [adai001@adai002 ~]$ cat /root/ cat: /root/: 权限不够 [adai001@adai002 ~]$ sudo /usr/bin/cat /root/ /usr/bin/cat: /root/: 是一个目录
注: 1)在增添用户的同时需要对用户设置密码(此处设置的是12345678),用户和登录密码要同时成对存在! 2)在编辑sudo配置文件时可以使用"NOPASSWD"前缀设置无密码使用权限,即在使用sudo命令时不用再输入用户密码!
sudo : 暂时切换到超级用户模式以执行超级用户权限,提示输入密码时该密码为当前用户的密码,而不是超级账户的密码。不过有时间限制,Ubuntu默认为一次时长15分钟。
su : 切换到某某用户模式,提示输入密码时该密码为切换后账户的密码,用法为“su账户名称”。如果后面不加账户时系统默认为root账户,密码也为超级账户的密码。没有时间限制。
sudo -i: 为了频繁的执行某些只有超级用户才能执行的权限,而不用每次输入密码,可以使用该命令。提示输入密码时该密码为当前账户的密码。没有时间限制。执行该命令后提示符变为“#”而不是“$”。想退回普通账户时可以执行“exit”或“logout” 。
其实,还有几个类似的用法: sudo /bin/bash: 这个命令也会切换到root的bash下,但不能完全拥有root的所有环境变量,比如PATH,可以拥有root用户的权限。这个命令和 sudo -s 是等同的。
sudo -s : 如上
sudo su : 这个命令,也是登录到了root,但是并没有切换root的环境变量,比如PATH。
sudo su - : 这个命令,纯粹的切换到root环境下,可以这样理解,先是切换到了root身份,然后又以root身份执行了 su -,此时跟使用root登录没有什么区别。此结果貌似跟sudo -i的效果是一样的,但是也有不同,sudo只是临时拥有了root的权限,而su则是使用root账号登录了linux系统。 所以,我们再来总结一下:
sudo su - 约等于 sudo -i
sudo -s 完全等于 sudo /bin/bash 约等于 sudo su sudo 终究被一个"临时权限的帽子"扣住,不能等价于纯粹的登录到系统里。
# # Sample /etc/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. # ## # User alias specification ## User_Alias FULLTIMERS = millert, mikef, dowdy User_Alias PARTTIMERS = bostley, jwfox, crawl User_Alias WEBMASTERS = will, wendy, wim ## # Runas alias specification ## Runas_Alias OP = root, operator Runas_Alias DB = oracle, sybase ## # Host alias specification ## Host_Alias SPARC = bigtime, eclipse, moet, anchor:\ SGI = grolsch, dandelion, black:\ ALPHA = widget, thalamus, foobar:\ HPPA = boa, nag, python Host_Alias CUNETS = 128.138.0.0/255.255.0.0 Host_Alias CSNETS = 128.138.243.0, 128.138.204.0/24, 128.138.242.0 Host_Alias SERVERS = master, mail, www, ns Host_Alias CDROM = orion, perseus, hercules ## # Cmnd alias specification ## Cmnd_Alias DUMPS = /usr/sbin/dump, /usr/sbin/rdump, /usr/sbin/restore, \ /usr/sbin/rrestore, /usr/bin/mt Cmnd_Alias KILL = /usr/bin/kill Cmnd_Alias PRINTING = /usr/sbin/lpc, /usr/bin/lprm Cmnd_Alias SHUTDOWN = /usr/sbin/shutdown Cmnd_Alias HALT = /usr/sbin/halt Cmnd_Alias REBOOT = /usr/sbin/reboot Cmnd_Alias SHELLS = /sbin/sh, /usr/bin/sh, /usr/bin/csh, /usr/bin/ksh, \ /usr/local/bin/tcsh, /usr/bin/rsh, \ /usr/local/bin/zsh Cmnd_Alias SU = /usr/bin/su Cmnd_Alias VIPW = /usr/sbin/vipw, /usr/bin/passwd, /usr/bin/chsh, \ /usr/bin/chfn ## # Override built-in defaults ## Defaults syslog=auth Defaults>root !set_logname Defaults:FULLTIMERS !lecture Defaults:millert !authenticate Defaults@SERVERS log_year, logfile=/var/log/sudo.log ## # User specification ## # root and users in group wheel can run anything on any machine as any user root ALL = (ALL) ALL %wheel ALL = (ALL) ALL # full time sysadmins can run anything on any machine without a password FULLTIMERS ALL = NOPASSWD: ALL # part time sysadmins may run anything but need a password PARTTIMERS ALL = ALL # jack may run anything on machines in CSNETS jack CSNETS = ALL # lisa may run any command on any host in CUNETS (a class B network) lisa CUNETS = ALL # operator may run maintenance commands and anything in /usr/oper/bin/ operator ALL = DUMPS, KILL, SHUTDOWN, HALT, REBOOT, PRINTING,\ sudoedit /etc/printcap, /usr/oper/bin/ # joe may su only to operator joe ALL = /usr/bin/su operator # pete may change passwords for anyone but root on the hp snakes pete HPPA = /usr/bin/passwd [A-z]*, !/usr/bin/passwd root # bob may run anything on the sparc and sgi machines as any user # listed in the Runas_Alias "OP" (ie: root and operator) bob SPARC = (OP) ALL : SGI = (OP) ALL # jim may run anything on machines in the biglab netgroup jim +biglab = ALL # users in the secretaries netgroup need to help manage the printers # as well as add and remove users +secretaries ALL = PRINTING, /usr/bin/adduser, /usr/bin/rmuser # fred can run commands as oracle or sybase without a password fred ALL = (DB) NOPASSWD: ALL # on the alphas, john may su to anyone but root and flags are not allowed john ALPHA = /usr/bin/su [!-]*, !/usr/bin/su *root* # jen can run anything on all machines except the ones # in the "SERVERS" Host_Alias jen ALL, !SERVERS = ALL # jill can run any commands in the directory /usr/bin/, except for # those in the SU and SHELLS aliases. jill SERVERS = /usr/bin/, !SU, !SHELLS # steve can run any command in the directory /usr/local/op_commands/ # as user operator. steve CSNETS = (operator) /usr/local/op_commands/ # matt needs to be able to kill things on his workstation when # they get hung. matt valkyrie = KILL # users in the WEBMASTERS User_Alias (will, wendy, and wim) # may run any command as user www (which owns the web pages) # or simply su to www. WEBMASTERS www = (www) ALL, (root) /usr/bin/su www # anyone can mount/unmount a cd-rom on the machines in the CDROM alias ALL CDROM = NOPASSWD: /sbin/umount /CDROM,\ /sbin/mount -o nosuid\,nodev /dev/cd0a /CDROM
文件编辑状态下可以用“/”进行关键词查找,输入“:set nu(=number)”显示行号。
注: 该方法只适用于通过ssh远程登录Linux的时候。修改配置文件vi /etc/ssh/sshd_config,修改“#PermitRootLogin yes”为“PermitRootLogin no”,即不允许root远程登录。 eg:
[root@adai002 ~]# vi /etc/ssh/sshd_config # Logging # obsoletes QuietMode and FascistLogging #SyslogFacility AUTH SyslogFacility AUTHPRIV #LogLevel INFO # Authentication: #LoginGraceTime 2m #PermitRootLogin yes -->no #StrictModes yes #MaxAuthTries 6 #MaxSessions 10 #RSAAuthentication yes #PubkeyAuthentication yes :wq 保存退出! [root@adai002 ~]# systemctl restart sshd.service 重启sshd服务,使其生效即可限制远程root远程登录!
(adsbygoogle = window.adsbygoogle || []).push({});
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句