CentOS 中的 `su` 和 `su -` 有什么区别?

CentOS 中的 `su` 和 `su -` 有什么区别?

我尝试通过运行在 CentOS 中切换到“root” su。它确实更改为“root”,但它似乎不是真正的“root”用户。

例如,默认情况下,在普通用户的路径中找不到ifconfig命令,但对于“root”,可以找到该命令。当我运行 时,当我尝试运行 时,我仍然会得到输出。/sbinsubash: ifconfig: command not foundifconfig

但是,当我运行时su -ifconfig就可以正常工作。

CentOS 中的su和有什么区别?su -

答案1

添加-选项会影响您的环境行为。出于所有实际目的,环境将完全重置。一般来说,您可能希望使用su -而不是普通的su。从手册页中:

 -l      Simulate a full login.  The environment is discarded except for HOME, SHELL, PATH, TERM, and USER.  HOME and SHELL are modified as above.
         USER is set to the target login.  PATH is set to ``/bin:/usr/bin''.  TERM is imported from your current environment.  The invoked shell is
         the target login's, and su will change directory to the target login's home directory.

 -       (no letter) The same as -l.

答案2

su -调用登录 shell,除其他外,它确保 root.bashrc和其他 shell 启动脚本运行,就像您通过控制台或 SSH 直接以 root 身份登录一样。root 的配置文件通常会设置您的路径以包含通常所在/sbin的路径ifconfig

相关内容