sudo su - root 介绍

sudo命令和su命令介绍

介绍

在使用云主机时,某个用户登录后,可以执行如下命令直接切换到 root 用户

1
$ sudo su - root

sudo 命令需要输入当前用户的密码,su 命令需要输入 root 用户的密码。另外一个区别是其默认行为,sudo 命令只允许使用提升的权限运行单个命令,而 su 命令会启动一个新的 shell,同时允许使用 root 权限运行尽可能多的命令,直到明确退出登录。

sudo 命令

sudo 命令,全称为 super user do,允许非root用户执行root用户才可以执行的命令。

要想使一个用户具有使用sudo的能力,需要让root用户将其名字、可以执行的特定命令、按照哪种用户或用户组的身份执行等信息注册到/etc/sudoers文件中,即完成对该用户的授权(此时该用户称为“sudoer”)才可以。

当一般用户执行特殊权限时,在命令前加上 sudo,此时系统会让你输入密码以确认终端机前操作的是你本人,确认后系统会将该命令的进程以超级用户的权限运行。

在一定的时间段内,再次执行sudo的命令时不再询问密码,超出此时间段(一般为5分钟)后需要再次输入密码。

可以配置用户执行sudo时不需输入密码

sudo配置无密码后,用户就可以使用 sudo su - root 直接免密切换到root 用户了

无密码 sudo 配置

centos7 配置用户无密码 sudo

修改/etc/sudoers文件,从而让普通用户username支持无密码sudo

方式一:

sudo 相关的配置位于 /etc/sudoers 文件内,这个文件不建议直接编辑,而是使用以下命令

1
sudo visudo

该命令会打开默认的编辑器编辑 /etc/sudoers 文件,并在保存时自动检查文件格式并设置到正确的文件权限。

进入编辑状态后,在文件的最后面 添加以下内容

1
username   ALL=(ALL)       NOPASSWD:ALL

username 改成自己的用户名

NOPASSWD 表示不需要输入密码
ALL 表示所有命令
也就是,用户在执行所有的 sudo 命令时军不需要输入密码,如果要设置指定命令无需输入密码,只需要把后面的 ALL 替换为具体命令

方式二:

手动修改 /etc/sudoers 方式如下

1
2
3
4
5
6
7
8
$ chmod u+w /etc/sudoers
$ vim /etc/sudoers
$ diff /etc/sudoers /etc/sudoers.bak
108c108
< username   ALL=(ALL)       NOPASSWD:ALL
---
> 
$ chmod u-w /etc/sudoers

注意:username ALL=(ALL) NOPASSWD:ALL 需要添加到 # %wheel ALL=(ALL) NOPASSWD: ALL 下面(即文件最后面),否则sudo还是会需要密码的。

然后,username用户就可以无需密码执行sudo命令了。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@centos7-desktop ~]# ll /etc/sudoers
-r--r-----. 1 root root 4328 9月  30 2020 /etc/sudoers
[root@centos7-desktop ~]# chmod u+w /etc/sudoers
[root@centos7-desktop ~]# ll /etc/sudoers
-rw-r-----. 1 root root 4328 9月  30 2020 /etc/sudoers
[root@centos7-desktop ~]# vim /etc/sudoers
[root@centos7-desktop ~]# diff /etc/sudoers /etc/sudoers.bak
108c108
< username   ALL=(ALL)       NOPASSWD:ALL
---
> 
[root@centos7-desktop ~]# chmod u-w /etc/sudoers
[root@centos7-desktop ~]# ll /etc/sudoers
-r--r-----. 1 root root 4364 12月 20 15:17 /etc/sudoers
[root@centos7-desktop ~]# cat /etc/sudoers
## Next comes the main part: which users can run what software on 
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
##     user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere 
root    ALL=(ALL)     ALL

## Allows members of the 'sys' group to run networking, software, 
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

## Allows people in group wheel to run all commands
%wheel    ALL=(ALL)    ALL

## Same thing without a password
# %wheel    ALL=(ALL)    NOPASSWD: ALL
username   ALL=(ALL)       NOPASSWD:ALL

配置用户有密码 sudo

若允许用户执行sudo命令(默认sudo命令需要输入用户密码),需将用户添加到 wheel 组,使用如下命令

1
$ usermod -a -G username wheel

也可以使用

1
$ gpasswd -a username wheel

此时,username用户执行 sudo ,还需要输入密码。加入wheel用户组,只是允许普通用户能够 sudo -s 切换到 root

使用如下命令,将用户 usernamewheel 组删除

1
$ gpasswd -d username wheel

执行 usermodgpasswd 命令,不会改变 /etc/sudoers 文件内容

操作记录

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 将username添加到wheel组
[root@centos7-desktop etc]# usermod -a -G username wheel
# 也可以使用 gpasswd -a username wheel
# 使用username用户执行sudo,还是需要输入密码
[username@centos7-desktop ~]$ sudo vi /etc/hosts
[sudo] username 的密码:
[username@centos7-desktop ~]$
[root@centos7-desktop etc]# diff /etc/sudoers /etc/sudoers.bak
[root@centos7-desktop etc]# id username
uid=1000(username) gid=1000(username) =1000(username),10(wheel)
将username从wheel用户组删除
[root@centos7-desktop etc]# gpasswd -d username wheel
正在将用户“username”从“wheel”组中删除
[root@centos7-desktop etc]# id username
uid=1000(username) gid=1000(username) =1000(username)
[root@centos7-desktop etc]#

su 命令

su 是切换用户的命令,后面不加用户,默认切换到root,切换后不改变当前环境变量 su - 是切换用户的同时将环境变量修改为目标用户的环境变量

使用su切换用户时,需要输入目标用户的密码

参考:
https://blog.csdn.net/chl183/article/details/108305842
https://zhuanlan.zhihu.com/p/137332644
https://zhuanlan.zhihu.com/p/252345791

Licensed under CC BY-NC-SA 4.0
Built with Hugo
主题 StackJimmy 设计