Linux 命令 su 和 sudo 的区别

su 命令介绍及主要用法

首先需要解释下 su 代表什么意思。
su 是表示 「switch user」「切换用户」

su 的一般使用方法是:

su  <user_name>
su - <user_name>
su - -c "指令串"  # 以 root 的方式执行 "指令串"
[zhangsan@localhost root]$ su - -c "tail -n 10 /etc/passwd"
密码:   #输入 root 用户密码
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zhangsan:x:1000:1001::/home/zhangsan:/bin/bash

两种方法只差了一个字符 -,会有比较大的差异:

如果加入了 - 参数,那么是一种 login-shell 的方式,意思是说切换到另一个用户 <user_name> 之后,当前的 shell 会加载 <user_name> 对应的环境变量和各种设置;

如果没有加入 - 参数,那么是一种 non-login-shell 的方式,意思是说我现在切换到了 <user_name>,但是当前的 shell 还是加载切换之前的那个用户的环境变量以及各种设置

sudo命令介绍及主要用法

sudo 的英文全称是 super user do,即以超级用户(root 用户)的方式执行命令。这里的 sudo 和之前 su 表示的 switch user 是不同的

  • 主要用法

我们在 Linux 中经常会碰到 Permission denied 这种情况,比如以 zhangsan 用户的身份查看 /etc/shadow 的内容。因为这个文件的内容是只有 root 用户能查看的

[zhangsan@localhost root]$ tail -n 3 /etc/shadow
tail: 无法打开"/etc/shadow" 读取数据: 权限不够
[zhangsan@localhost root]$ sudo !!
sudo tail -n 3 /etc/shadow
[sudo] zhangsan 的密码:
ntp:!!:19104::::::
tcpdump:!!:19104::::::
zhangsan:$6$WxrcPCRU$x/jWGSH0pcF1K/IytsUPbW29cvX2PXetcnXAR15Zl1NhoCt5EYcs2tAlb/z.1K.L6ltdG7jCSJ5jBicAuumSP/:19244:0:99999:7:::

ps:实例中,我们使用了 sudo !! 这个小技巧,表示重复上面输入的命令,只不过在命令最前面加上 sudo

因为我已经设置了 sudo 命令不需要输入密码,所以这里 sudo !! 就能直接输出内容。如果没有设置的话,需要输入当前这个用户的密码,例如本例中,我就应该输入 ubuntu 用户的登录密码。

两次相邻的 sudo 操作,如果间隔在 5min 之内,第二次输入 sudo 不需要重新输入密码;如果超过 5min,那么再输入 sudo 时,又需要输入密码。所以一个比较省事的方法是设置 sudo 操作不需要密码。

  • 切换到 root 用户
sudo su -

This method can also switch to the root user using login-shell, but it is different from the su - method: in the former
, after entering sudo su -, you need to provide the login password of the current user, which is the password of the ubuntu user;
After entering su -, the user needs to provide the login password of the root user.

sudo -i

This command has the same effect as sudo su -. It also switches to the root user and requires the login password of the current user.

Whether a user can use the sudo command depends on the settings of the /etc/sudoers file.
From the above view of /etc/shadow, we have seen that user zhangsan can use sudo normally. This is because zhangsan ALL=(ALL) ALL has been configured in the /etc/sudoers file.

/etc/sudoers is also a text file, but because of its specific syntax, we do not need to use vim or vi to edit it directly. We need to use visudothis command. After entering this command, you can directly edit the /etc/sudoers file.
It should be noted that only rootusers have permission to use visudothe command.

Compare the differences between the two

  • Use su -, provide rootaccount 密码, you can switch to the root user;

  • Use sudo su -, provided 当前用户, 密码you can also switch to the root user

Guess you like

Origin blog.csdn.net/weixin_43824520/article/details/126773837