您现在的位置:首页 » 知识库 » 系统集成 » LINUX系统 LINUX系统
Linux下su命令、sudo命令、限制root远程登录讲解
发布日期:2018-04-16

su命令介绍

sum命令,它是用来切换用户的 快捷键ctrl+d,退出当前用户

su命令用法

su - username 完全切换当前用户

[root@localhost ~]# whoami

root

[root@localhost ~]# su - hanfeng

[hanfeng@localhost ~]$ whoami

hanfeng

[hanfeng@localhost ~]$ pwd

/home/hanfeng

[hanfeng@localhost ~]$ ls -la

总用量 16

drwx------. 2 hanfeng ha1 83 11月 1 15:07 .

drwxr-xr-x. 4 root root 34 10月 31 15:38 ..

-rw-------. 1 hanfeng ha1 21 11月 1 15:18 .bash_history

-rw-r--r--. 1 hanfeng ha1 18 8月 3 2016 .bash_logout

-rw-r--r--. 1 hanfeng ha1 193 8月 3 2016 .bash_profile

-rw-r--r--. 1 hanfeng ha1 231 8月 3 2016 .bashrc

[hanfeng@localhost ~]$ 登出

[root@localhost ~]#

其中这个 - ,目的就是彻底的切换用户,使用自己的配置、环境变量等

.bash开头的文件都是用户的配置文件

- su username 看到用户已经切换,但不是彻底的切换过来,因为还在/root用户下

[root@localhost ~]# su hanfeng

[hanfeng@localhost root]$ whoami

hanfeng

[hanfeng@localhost root]$ pwd

/root

[hanfeng@localhost root]$ exit

[root@localhost ~]#

su - -c用法

su - -c “命令” 用户名:是不进入这个账户,但是以这个账户的身份执行命令

[root@localhost ~]# su - -c "touch /tmp/han.111" hanfeng

[root@localhost ~]# ls -lt /tmp/ |head

总用量 0

-rw-r--r--. 1 hanfeng ha1 0 11月 1 15:45 han.111

drwx------. 3 root root 17 11月 1 10:21 systemd-private-5909416742444897abc72857986169b4-vmtoolsd.service-klBF7K

[root@localhost ~]# id hanfeng

uid=1000(hanfeng) gid=1001(ha1) 组=1001(ha1),1002(user3)

[root@localhost ~]#

这里会看到文件的属组是ha1,因为hanfeng用户的属组就是ha1

su命令切换用户后,再切换到另一个用户下

只要输入再次切换用户的密码即可登录

[root@localhost ~]# su - hanfeng

上一次登录:三 11月 1 16:09:37 CST 2017pts/0 上

[hanfeng@localhost ~]$ su - user5

密码:

最后一次失败的登录:三 11月 1 16:10:09 CST 2017pts/0 上

最有一次成功登录后有 1 次失败的登录尝试。

[user5@localhost ~]$ ls -la

总用量 12

drwx------. 2 user5 user5 62 11月 1 16:07 .

drwxr-xr-x. 5 root root 47 11月 1 16:07 ..

-rw-r--r--. 1 user5 user5 18 8月 3 2016 .bash_logout

-rw-r--r--. 1 user5 user5 193 8月 3 2016 .bash_profile

-rw-r--r--. 1 user5 user5 231 8月 3 2016 .bashrc

[user5@localhost ~]$ 登出

[hanfeng@localhost ~]$ 登出

[root@localhost ~]#

/etc/skel文件

useradd -M 不要自动建立用户的登入目录,就是说创建的用户没有配置文件 /etc/skel是存放模板配置文件

[root@localhost ~]# useradd -M ll

[root@localhost ~]# su - ll

su: 警告:无法更改到 /home/ll 目录: 没有那个文件或目录

-bash-4.2$

-bash-4.2$ 登出

[root@localhost ~]# mkdir /home/ll

[root@localhost ~]# chown ll:ll /home/ll/

[root@localhost ~]# su - ll

上一次登录:三 11月 1 16:20:29 CST 2017pts/0 上

-bash-4.2$ pwd

/home/ll

-bash-4.2$ ls -la

总用量 0

drwxr-xr-x. 2 ll ll 6 11月 1 16:28 .

drwxr-xr-x. 6 root root 57 11月 1 16:28 ..

-bash-4.2$ 登出

[root@localhost ~]# cp /etc/skel/.bash* /home/ll/

[root@localhost ~]# chown -R ll:ll !$

chown -R ll:ll /home/ll/

[root@localhost ~]# su - ll

上一次登录:三 11月 1 16:55:28 CST 2017pts/0 上

[ll@localhost ~]$ 登出

[root@localhost ~]#

普通用户也可以 su - 到root用户下,只需要root用户的密码

[root@hf-01 ~]# su - hanfeng

上一次登录:五 10月 27 05:14:42 CST 2017pts/1 上

[hanfeng@hf-01 ~]$ su -

密码:

上一次登录:四 11月 2 01:26:08 CST 2017从 192.168.74.1pts/0 上

[root@hf-01 ~]# whoami

root

[root@hf-01 ~]# 登出

[hanfeng@hf-01 ~]$ 登出

[root@hf-01 ~]# 登出

[root@hf-01 ~]#

3.8 sudo命令

sudo命令介绍

sudo命令,可以不切换用户就可以获取其他用户的权限来执行相关命令。(通常情况就是,给普通用户授权root用户的身份)

visudo命令,可以打开sudo命令的配置文件(会看到其实代开的是/etc/sudoers.tmp这个文件)

[root@hf-01 ~]# visudo 会进入/etc/sudoers.tmp的配置文件中

97 ## Allow root to run any commands anywhere

98 root ALL=(ALL) ALL

99 hanfeng ALL=(ALL) /usr/bin/ls, /usr/bin/mv, /usr/bin/ls

[root@hf-01 ~]#

输入 :set nu 来显示行号 我们在root用户下面一行,在添加一个用户,并可以运/usr/bin/ls, usr/bin/mv, /usr/bin/cat命令,可写多个, 也可写ALL(表示所有) 然后 :wq 保存退出 默认root支持sudo,因为文件中默认有root ALL=(ALL) ALL 。 在这一行下面加入 hanfeng ALL=(ALL) /usr/bin/ls, /usr/bin/mv, /usr/bin/ls ,意思是hanfeng这个用户在执行sudo这个命令时,可以获取部分root用户的权限。 从左到右依次为,第一个ALL就可以理解为主机的意思,第二个ALL是可以获取哪个用户的权限,All就是所有包括root,第三个ALL是指使用sudo执行所有命令。

sudo命令的用法

sudo命令用法一

su命令可以切换用户身份 在 su 在切换成普通用户后,是无法查看/root/目录的,这时用sudo命令,则可以让该用户临时拥有root用户的权限 使用在visudo命令中,编辑的命令要使用绝对路径

[root@hf-01 ~]# su - hanfeng

上一次登录:四 11月 2 03:52:44 CST 2017pts/0 上

[hanfeng@hf-01 ~]$ ls /root/

ls: 无法打开目录/root/: 权限不够

[hanfeng@hf-01 ~]$ sudo /usr/bin/ls /root/ 在执行命令后,会提示输入hanfeng用户的密码

We trust you have received the usual lecture from the local System

Administrator. It usually boils down to these three things:

#1) Respect the privacy of others.

#2) Think before you type.

#3) With great power comes great responsibility.

[sudo] password for hanfeng:

11.txt 234 33.txt ha.txt

[hanfeng@hf-01 ~]$ ls /root/ 在hanfeng用户下直接去执行会发现无法打开/root/目录

ls: 无法打开目录/root/: 权限不够

[hanfeng@hf-01 ~]$ sudo /usr/bin/ls /root/

11.txt 234 33.txt ha.txt

[hanfeng@hf-01 ~]$ mv /root/ha.txt /root/haha.txt

mv: 无法打开目录/root/: 权限不够

[hanfeng@hf-01 ~]$ sudo /usr/bin/mv /root/ha.txt /root/haha.txt

[hanfeng@hf-01 ~]$ 登出

[root@hf-01 ~]#

sudo命令用法二

在visudo命令中, 编辑/etc/sudoers.tmp配置文件,设置NOPASSWD: ALL,则之后再也不需要输入密码。 在sudo命令下,可以使用绝对路径命令,也可以直接使用命令去执行,得到的结果相同

[root@hf-01 ~]# visudo

[root@hf-01 ~]# su - user2

上一次登录:四 11月 2 07:17:04 CST 2017pts/0 上

[user2@hf-01 ~]$ ls /root/

ls: 无法打开目录/root/: 权限不够

[user2@hf-01 ~]$ sudo ls /root/

11.txt 234 33.txt haha.txt

[user2@hf-01 ~]$ sudo /usr/bin/ls /root/

11.txt 234 33.txt haha.txt

[user2@hf-01 ~]$ 登出

[root@hf-01 ~]#

sudo命令用法三

在visudo命令中,给一些用户设置一些别名,这里的别名相当于一个虚拟的用户

如:User Aliases 给用户做一个别名 其中的ADMINS是虚拟用户,jsmith, mikem是两个真实用户,所以说虚拟用户里面存在两个真实用户

## 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

在visudo命令中,给命令设置一些别名

## 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

例子:

[root@hf-01 ~]# visudo 进入到配置环境中

然后到

## Networking 那一段落最后加上

HANFENG_CMD = /usr/bin/ls, /usr/bin/mv, /usr/bin/cat

并将用户名hanfeng后面,去除那些绝对路径命令,修改上HANFENG_CMD,然后保存退出

root ALL=(ALL) ALL

hanfeng ALL=(ALL) HANFENG_CMD

[root@hf-01 ~]# su - hanfeng

上一次登录:四 11月 2 05:46:40 CST 2017pts/0 上

[hanfeng@hf-01 ~]$ sudo ls /root/ 这里会发现可以查看/root/目录下的文件

[sudo] password for hanfeng:

11.txt 234 33.txt haha.txt

[hanfeng@hf-01 ~]$ sudo ls /root/

11.txt 234 33.txt haha.txt

[hanfeng@hf-01 ~]$ sudo cat /root/haha.txt

[hanfeng@hf-01 ~]$ 登出

[root@hf-01 ~]#

对用户组做出一些限制

## Allows people in group wheel to run all commands

109 %wheel ALL=(ALL) ALL

sudo命令总结:

在visudo命令中的配置文件下,输入 :set nu 则每行会显示出行号。 在第一次使用sudo命令,去执行某条命令,会要求输入当前用户的密码,但在第二次执行该条命令时,直接输入即可执行(或者,在visudo的配置文件中,在该用户的写上无需密码,如hanfeng ALL=(ALL) NOPASSWD:ALL 就可直接登录,无需密码了),再添加命令需要使用绝对路径 在visudo的配置文件中写错了,保存退出后,会报错,这时选择 e 然后回车继续进去编辑即可。 在visudo的配置文件中,添加命令,需要使用绝对路径(使用命令的时候可以使用绝对路径命令或命令去执行) sudo命令,就是用普通用户临时拥有root用户的身份,去执行某一条命令。(这样就可以避免将root用户给普通用户了) 给用户、命令做一些别名,对用户组做出一些限制

3.9 限制root远程登录

sudo su - root 命令(root可省略)

设置不需要密码直接切换到root用户下

在visudo中 在User_Alias HANS = hanfeng, user2, user4 HANS ALL=(ALL) NOPASSWD: /usr/bin/su 设置完成后就可直接切换到root用户下了

[root@hf-01 ~]# visudo 在配置环境中设置

在## User Aliases一段末尾处的下一行设置

User_Alias HANS = hanfeng, user2, user4

在 ## Allow root to run any commands anywhere

root ALL=(ALL) ALL

hanfeng ALL=(ALL) HANFENG_CMD

user10 ALL=(ALL) NOPASSWD: /usr/bin/ls, /usr/bin/mv, /usr/bin/cat

user2 ALL=(ALL) NOPASSWD: /usr/bin/ls, /usr/bin/mv, /usr/bin/cat

在这里加上一下点,然后保存退出

HANS ALL=(ALL) NOPASSWD: /usr/bin/su

然后执行命令

[root@hf-01 ~]# su - hanfeng

上一次登录:四 11月 2 07:51:34 CST 2017pts/0 上

[hanfeng@hf-01 ~]$ sudo su -

上一次登录:四 11月 2 05:38:13 CST 2017从 192.168.74.1pts/1 上

[root@hf-01 ~]# whoami

root

[root@hf-01 ~]# 登出

[hanfeng@hf-01 ~]$ 登出

[root@hf-01 ~]#

限制root用户远程登录

在vi /etc/ssh/sshd_config文件中设置

在/etc/ssh/sshd_config里面搜索关键词用法和less一样,是 /关键词 就会出现

[root@hf-01 ~]# vi /etc/ssh/sshd_config 在这里搜索/Root

#LoginGraceTime 2m

PermitRootLogin no 将前面的 #号 去除并将yes改为no,表示不允许远程登录了

#StrictModes yes

然后保存退出,再重启配置服务

[root@hf-01 ~]# systemctl restart sshd.service

[root@hf-01 ~]#

然后再去xshell连接,会发现密码和秘钥都无法登录 使用putty去登陆普通用户,就会看到普通用户无法去访问/root/目录,但是使用sudo su - root就可切换到root用户下使用

  • 1.公司登记注册于2003年1月27日,清远市桑达电子网络媒体有限公司
    2.公司2006年起成为清远市政府定点协议供货商,电子采购供货商
    3.公司2007年被清远市相关政府部门评为安防行业状元
    4.公司2007年起成为长城电脑清远如意服务站(SP368)
    5.公司2007年承建清远市横河路口电子警察工程,开创清远电子警察先河。
  • 6.公司2007年起成为IBM合作伙伴、公司2010年底成为金蝶软件清远金牌代理(伙伴编号:30030013)
    7.公司组团队参加南方都市报组织的创富评选,获广东80强。公司申请多项软件著作权、专利权
    8.2016年起公司成为粤东西北地区为数不多的双软企业,确立“让软件驱动世界,让智能改变生活!"企业理想
    9.2016-01-29更名为广东互动电子网络媒体有限公司
    10.2021-01-13更名为广东互动电子有限公司
  • 投资合作咨询热线电话:0763-3391888 3323588
  • 做一个负责任的百年企业! 天行健,君子以自强不息;地势坤,君子以厚德载物;
    为用户创造价值! 让软件驱动世界; 让智能改变生活; 超越顾客期望,帮助顾客成功;
    对客户负责,对员工负责,对企业命运负责!帮助支持公司的客户成功;帮助忠诚于公司的员工成功!
  • 联系电话:0763-3391888 3323588 3318977
    服务热线:18023314222 QQ:529623964
  • 工作QQ:2501204690 商务QQ: 602045550
    投资及业务投诉QQ: 529623964
    微信:小米哥 微信号:qysed3391888
    腾讯微博:桑达网络-基石与起点
  • E-MAIL:222#QYSED.CN ok3391888#163.com (请用@替换#)
在线客服
  • 系统集成咨询
    点击这里给我发消息
  • 网站\微信\软件咨询
    点击这里给我发消息
  • 售后服务
    点击这里给我发消息
  • 投资合作
    点击这里给我发消息