当我 sudo bash -c 时会发生什么?
What happens when I sudo bash -c?
我知道 sudo bash -c 'some_command'
将 运行 some_command
具有与 sudo 相同的权限。
我对发生了什么感到困惑? 运行 some_command
在 bash 中作为 sudo(与 sudo bash
相同)然后切换回我当前的用户吗?为什么我没有像 运行 sudo bash
?
那样留在具有 sudo 权限的 bash 实例中
我试过 运行ning man bash
并且它描述了 -c
选项(在下面引用)。
但是,我正在努力拼凑描述与我在 运行ning sudo bash -c 'some_command'
时观察到的行为的关系
If the -c option is present, then commands are read from the first non-option argument command_string. If there are arguments after
the command_string, they are assigned to the positional parameters, starting with [=19=].
sudo
切换用户然后执行 bash
,将其他参数传递给它。 bash
运行 因为新用户在 -c
.
之后运行参数中的命令
考虑这个片段(注意 UID 和 PID/PPID 列。):
$ sudo ps -f
UID PID PPID C STIME TTY TIME CMD
root 8997 8715 0 11:57 pts/17 00:00:00 sudo ps -f
root 8998 8997 0 11:57 pts/17 00:00:00 ps -f
$ sudo bash -c 'ps -f'
UID PID PPID C STIME TTY TIME CMD
root 8909 8715 3 11:55 pts/17 00:00:00 sudo bash -c ps -f
root 8910 8909 0 11:55 pts/17 00:00:00 ps -f
$ sudo bash -c 'echo hi; ps -f'
hi
UID PID PPID C STIME TTY TIME CMD
root 8957 8715 0 11:56 pts/17 00:00:00 sudo bash -c echo hi; ps -f
root 8958 8957 0 11:56 pts/17 00:00:00 bash -c echo hi; ps -f
root 8959 8958 0 11:56 pts/17 00:00:00 ps -f
- 在第一种情况下,
sudo
作为 root
用户启动 ps -f
。
- 在第二种情况下,
sudo
作为 root
用户使用参数 -c 'ps -f'
启动 bash
。看来,作为一种优化,bash
正在使用 exec
来启动 ps -f
。因此,只看到 2 个进程。
- 在第 3 种情况下,
sudo
作为 root
用户使用参数 -c 'echo hi; ps -f'
启动 bash
。命令(-c
的参数)不是简单的可执行文件 + args。它们是由 ;
分隔的 2 个命令。所以,bash
不能直接调用 exec
。它使用标准的 fork+exec
机制。因此,bash
是 ps
进程的父进程。
我知道 sudo bash -c 'some_command'
将 运行 some_command
具有与 sudo 相同的权限。
我对发生了什么感到困惑? 运行 some_command
在 bash 中作为 sudo(与 sudo bash
相同)然后切换回我当前的用户吗?为什么我没有像 运行 sudo bash
?
我试过 运行ning man bash
并且它描述了 -c
选项(在下面引用)。
但是,我正在努力拼凑描述与我在 运行ning sudo bash -c 'some_command'
If the -c option is present, then commands are read from the first non-option argument command_string. If there are arguments after the command_string, they are assigned to the positional parameters, starting with [=19=].
sudo
切换用户然后执行 bash
,将其他参数传递给它。 bash
运行 因为新用户在 -c
.
考虑这个片段(注意 UID 和 PID/PPID 列。):
$ sudo ps -f
UID PID PPID C STIME TTY TIME CMD
root 8997 8715 0 11:57 pts/17 00:00:00 sudo ps -f
root 8998 8997 0 11:57 pts/17 00:00:00 ps -f
$ sudo bash -c 'ps -f'
UID PID PPID C STIME TTY TIME CMD
root 8909 8715 3 11:55 pts/17 00:00:00 sudo bash -c ps -f
root 8910 8909 0 11:55 pts/17 00:00:00 ps -f
$ sudo bash -c 'echo hi; ps -f'
hi
UID PID PPID C STIME TTY TIME CMD
root 8957 8715 0 11:56 pts/17 00:00:00 sudo bash -c echo hi; ps -f
root 8958 8957 0 11:56 pts/17 00:00:00 bash -c echo hi; ps -f
root 8959 8958 0 11:56 pts/17 00:00:00 ps -f
- 在第一种情况下,
sudo
作为root
用户启动ps -f
。 - 在第二种情况下,
sudo
作为root
用户使用参数-c 'ps -f'
启动bash
。看来,作为一种优化,bash
正在使用exec
来启动ps -f
。因此,只看到 2 个进程。 - 在第 3 种情况下,
sudo
作为root
用户使用参数-c 'echo hi; ps -f'
启动bash
。命令(-c
的参数)不是简单的可执行文件 + args。它们是由;
分隔的 2 个命令。所以,bash
不能直接调用exec
。它使用标准的fork+exec
机制。因此,bash
是ps
进程的父进程。