su usersudo su user的区别

  1. su user 需要提供user的密码
  2. sudo su user 需要提供当前用户的密码

例如: 当前用户是guest su root 需要输入root用户的密码 sudo su root 需要输入guest的密码

su - user su user的区别

  1. - 会切换当前的shell环境(环境变量会切换到对应的user上, 相当于执行 user目录下的 .bash 等文件)

例如: su,su -: 前者只是切换了root身份,但Shell环境仍然是普通用户的Shell;而后者连用户和Shell环境一起切换成root身份了。只有切换了Shell环境才不会出现PATH环境变量错误。su切换成root用户以后,pwd一下,发现工作目录仍然是普通用户的工作目录;而用su -命令切换以后,工作目录变成root的工作目录了

su - usernamesudo su - username

su - username - Asks the system to start a new login session for the specified user. The system will require the password for the user “username” (even if its the same as the current user). su - username - 要求系统为指定用户启动新的登录会话。 系统将要求“username”的密码(即使它与当前用户相同)。

sudo su - username will do the same, but first ask the system to be elevated to super user mode, after which su will not ask for “username”’s password because a super user is allowed to change into any other user without knowing their password. That being said, sudo in itself enforces security by by checking the /etc/sudoers file to make sure the current user is allowed to gain super user permissions,and possibly verifying the current user’s password. sudo su - username也会这样做,但首先要求系统升级到超级用户模式,之后su不会要求“username”的密码,因为超级用户可以在不知道密码的情况下更改为任何其他用户sudo本身通过检查/ etc / sudoers文件来强制执行安全性,以确保允许当前用户获得超级用户权限,并可能需要验证当前用户的密码。

su - 和 sudo -

su - The command su is used to switch to another user (s witch u ser), but you can also switch to the root user by invoking the command with no parameter. su asks you for the password of the user to switch, after typing the password you switched to the user’s environment.

sudo - sudo is meant to run a single command with root privileges. But unlike su it prompts you for the password of the current user. This user must be in the sudoers file (or a group that is in the sudoers file). By default, Ubuntu “remembers” your password for 15 minutes, so that you don’t have to type your password every time.


  • sudo su Calls sudo with the command su. Bash is called as interactive non-login shell. So bash only executes .bashrc. You can see that after switching to root you are still in the same directory:
user@host:~$ sudo su
  • sudo su - This time it is a login shell, so /etc/profile, .profile and .bashrc are executed and you will find yourself in root’s home directory with root’s environment.

  • sudo -i It is nearly the same as sudo su - The -i (simulate initial login) option runs the shell specified by the password database entry of the target user as a login shell. This means that login-specific resource files such as .profile, .bashrc or .login will be read and executed by the shell.

  • sudo /bin/bash This means that you call sudo with the command /bin/bash. /bin/bash is started as non-login shell so all the dot-files are not executed, but bash itself reads .bashrc of the calling user. Your environment stays the same. Your home will not be root’s home. So you are root, but in the environment of the calling user.

  • sudo -s reads the $SHELL variable and executes the content. If $SHELL contains /bin/bash it invokes sudo /bin/bash (see above).