su命令是用来切换用户的,其格式为:su - username 经常使用的选项以下:less
[lichao@test-02 ~]$ ls /root ls: 没法打开目录/root: 权限不够 [lichao@test-02 ~]$ su - root 密码: 上一次登陆:五 12月 29 02:37:30 CST 2017从 192.168.101.1pts/0 上 [root@test-02 ~]# ls /root anaconda-ks.cfg [root@test-02 ~]# 登出 [lichao@test-02 ~]$ su - root -c ls /root 密码: anaconda-ks.cfg [lichao@test-02 ~]$
当使用普通用户切换root的时候,须要输入root的密码,使用root切换到普通用户则不须要密码。ssh
受权普通用户拥有别的用户的权限,大部分时候是受权普通用户拥有root用户的权限,要使用这个命令,须要编辑配置文件,etc/sudoers,可是直接编辑这个文件出错后系统不会报错,因此咱们用visudo命令来编辑,若是编辑有问题,系统会提示错误ui
## user MACHINE=COMMANDS ## ## The COMMANDS section may have other options added to it. ## ## Allow root to run any commands anywhere root ALL=(ALL) ALL lichao ALL=(ALL) /usr/bin/ls, /usr/bin/cat, /usr/bin/more
这样编辑以后,lichao 就能够拥有root的ls,cat ,more 的权限了,使用方式以下rest
[lichao@test-02 ~]$ ls /root ls: 没法打开目录/root: 权限不够 [lichao@test-02 ~]$ sudo ls /root 1.txt anaconda-ks.cfg [lichao@test-02 ~]$ cat /root/1.txt cat: /root/1.txt: 权限不够 [lichao@test-02 ~]$ sudo cat /root/1.txt aaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccdddddddddddddddddddeeeeeeeeeeeeeeeeeeefffffffffffffffffffffggggggggggggg hhhhhhhhhhhhhhhhhhiiiiiiiiiiiiiiiijjjjjjjjjjjjjjjjjjjjjjjkkkkkkkkkkkkkkkkkkkkkllllllllllllllllll [lichao@test-02 ~]$ less /root/1.txt /root/1.txt: 权限不够 [lichao@test-02 ~]$ sudo less /root/1.txt 对不起,用户 lichao 无权以 root 的身份在 test-02 上执行 /bin/less /root/1.txt。 [lichao@test-02 ~]$ sudo more /root/1.txt aaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccdddddddddddddddddddeeeeeeeeeeeeeeeeeeefffffffffffffffffffffggggggggggggg hhhhhhhhhhhhhhhhhhiiiiiiiiiiiiiiiijjjjjjjjjjjjjjjjjjjjjjjkkkkkkkkkkkkkkkkkkkkkllllllllllllllllll [lichao@test-02 ~]$
编辑/etc/ssh/ssh_config文件,将#PermitRootLogin yes ,注释符号删掉,后面的yes改成no ,再重启服务,就限制了远程登陆rootcode
# Logging # obsoletes QuietMode and FascistLogging #SyslogFacility AUTH SyslogFacility AUTHPRIV #LogLevel INFO # Authentication: #LoginGraceTime 2m PermitRootLogin no #StrictModes yes #MaxAuthTries 6 #MaxSessions 10
[root@test-02 ~]# systemctl restart sshd.service重启服务,就好了ci
限制了远程登陆root,而有些普通用户又须要root权限怎么弄呢? 这时候能够visudo,给须要权限的用户受权su ,就能够了it
## Allow root to run any commands anywhere root ALL=(ALL) ALL AD ALL=(ALL) NOPASSWD: /usr/bin/su