用于切换当前用户身份到其余用户身份,变动时须输入所要变动的用户账号与密码。node
语法: su [-] username
“-”无关紧要,加上“-”的做用是在切换用户时初始化当前用户的各类环境变量。普通用户su不加username时就是直接切换到root用户。
eg:python
[root@3 ~]# su - aming001 彻底切换到用户aming001 上一次登陆:三 6月 14 06:43:37 CST 2017pts/0 上 [aming001@3 ~]$ whoami 查看当前用户 aming001 [aming001@3 ~]$ pwd 查看当前目录绝对路径 /home/aming001 用户家目录 [aming001@3 ~]$ su 切换到root用户 密码: [root@3 aming001]# pwd /home/aming001 ----此时目录没变 [root@3 aming001]# exit 退出root到aming001 exit [aming001@3 ~]$ su - 彻底切换到root用户 密码: 上一次登陆:三 6月 14 06:45:11 CST 2017pts/0 上 [root@3 ~]# pwd /root ----家目录改变
应用环境:
在root用户登陆时,直接以某个普通用户的身份去执行一个命令,适用于脚本中。linux
[root@3 ~]# su - aming001 -c "touch /tmp/1.txt" [root@3 ~]# ls -l /tmp/1.txt -rw-rw-r-- 1 aming001 aming001 0 6月 14 06:54 /tmp/1.txt
能够看到/tmp/1.txt的全部者和所属组都是aming001!web
su的确为管理带来方便,经过切换到root下,能完成全部系统管理工具,只要把root的密码交给任何一个普通用户,他都能切换到root来完成全部的系统管理工做;但经过su切换到root后,也有不安全因素;好比系统有10个用户,并且都参与管理。若是这10个用户都涉及到超级权限的运用,作为管理员若是想让其它用户经过su来切换到超级权限的root,必须把root权限密码都告诉这10个用户;若是这10个用户都有root权限,经过root权限能够作任何事,这在必定程度上就对系统的安全形成了威协;想一想Windows吧,简直就是恶梦;“没有不安全的系统,只有不安全的人”,咱们绝对不能保证这 10个用户都能按正常操做流程来管理系统,其中任何一人对系统操做的重大失误,均可能致使系统崩溃或数据损失;因此su 工具在多人参与的系统管理中,并非最好的选择,su只适用于一两我的参与管理的系统,毕竟su并不能让普通用户受限的使用;超级用户root密码应该掌握在少数用户手中,这绝对是真理!因此集权而治的存在仍是有必定道理的;shell
用来以其余身份来执行命令,预设的身份为root。在/etc/sudoers中设置了可执行sudo指令的用户。若其未经受权的用户企图使用sudo,则会发出警告的邮件给管理员。用户使用sudo时,必须先输入密码,以后有5分钟的有效期限,超过时限则必须从新输入密码。 安全
语法: sudo (选项) (参数)
选项: (该部分只作了解)
-b:在后台执行指令;
-h:显示帮助;
-H:将HOME环境变量设为新身份的HOME环境变量;
-k:结束密码的有效期限,也就是下次再执行sudo时便须要输入密码;
-l:列出目前用户可执行与没法执行的指令;
-p:改变询问密码的提示符号;
-s:执行指定的shell;
-u<用户>:以指定的用户做为新的身份。若不加上此参数,则预设以root做为新的身份;
-v:延长密码有效期限5分钟;
-V :显示版本信息。bash
配置sudo必须经过编辑/etc/sudoers文件,并且只有超级用户才能够修改它。使用visudo命令编辑/etc/sudoers配置文件,操做方法同vi命令。当对多个命令设置速sudo权限时,须要用逗号加空格隔开。使用visudo有两个缘由,一是它可以防止两个用户同时修改它;二是它也能进行有限的语法检查。因此,即便只有你一个超级用户,你也最好用visudo来检查一下语法。 oracle
[root@3 ~]# 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 2 ALL=(ALL) /usr/bin/ls, /usr/bin/mv, /usr/bin/cat 对2用户进行受权(受权完毕后保存退出) [root@3 ~]# su - 2 切换到普通用户 上一次登陆:三 6月 14 10:23:01 CST 2017pts/1 上 [2@3 ~]$ ls /root/ ls: 没法打开目录/root/: 权限不够 (!!!即,普通用户没有访问root用户的权限) [2@3 ~]$ sudo /usr/bin/ls /root/ 使用sudo命下访问root用户 [sudo] password for adai001: anaconda-ks.cfg 访问成功!!! [2@3 ~]$ sudo /usr/bin/ls /root/ anaconda-ks.cfg 再次使用sudo命令时无需输入密码 [2@3 ~]$ cat /root/ cat: /root/: 权限不够 [2@3 ~]$ sudo /usr/bin/cat /root/ /usr/bin/cat: /root/: 是一个目录
注:
1)在增添用户的同时须要对用户设置密码(此处设置的是12345678),用户和登陆密码要同时成对存在!
2)在编辑sudo配置文件时可使用"NOPASSWD"前缀设置无密码使用权限,即在使用sudo命令时不用再输入用户密码!eclipse
sudo : 暂时切换到超级用户模式以执行超级用户权限,提示输入密码时该密码为当前用户的密码,而不是超级帐户的密码。不过有时间限制,Ubuntu默认为一次时长15分钟。ssh
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@3 ~]# 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@3 ~]# systemctl restart sshd.service 重启sshd服务,使其生效便可限制远程root远程登陆!