将 sudo 与其他命令一起使用

Using sudo with other commands

假设我运行命令

exit

显然终端退出,因为这是命令的目的。

我理解 sudo 意味着 运行 具有管理员权限的命令。如果我 运行:

sudo exit

然后我从终端得到 "sudo: exit: command not found"。为什么终端在这种情况下不能识别命令并继续退出终端,就像 运行 没有 sudo 时那样?

unix-like 环境中的大多数 "commands" 实际上是外部程序和脚本。一些,如 exit,是 shell 中内置的实际命令。 sudo无法识别后者

来自手册页:

When sudo runs a command, it calls fork(2), sets up the execution environment as described above, and calls the execve system call in the child process.

来自 execve 手册页:

execve() executes the program pointed to by filename.

没有名为exit的程序;这只是 shell 识别的命令。

这样想:Unix 授予进程权限。 sudo 运行 具有 elevated/modified 权限的进程。但是当你 运行 exit 时,你并没有产生新的进程。 sudo 无法提升 already-running shell 的权限;那么它可能会做什么?

你可以告诉 sudo 生成另一个 shell,将 exit 作为命令传递给 运行 shell...当然什么都不做。