lay*_*yka 7 shell bash sudo shell-script
我的环境是Ubuntu 12.04 LTS,sudo版本是1.8.3p1。
首先我以普通用户身份登录:
$ whoami
fin
$ cat /etc/passwd | grep -i "root\|fin"
root:x:0:0:root:/root:/bin/bash
fin:x:1000:1000:This is a normal user:/home/fin:/bin/bash
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Mar 30 2012 /bin/sh -> dash
$ ls -l /bin/bash
-rwxr-xr-x 1 root root 920788 Apr 3 2012 /bin/bash
$ echo $SHELL
/bin/bash
$ ps | grep "$$" | awk '{ print $4 }'
bash
$ ls -l ./test.sh
-rwxr-xr-x 1 fin fin 37 Sep 27 16:46 test.sh
$ cat ./test.sh
ps | grep "$$" | awk '{ print $4 }'
$ ./test.sh
bash
$ sudo ./test.sh
sh
Run Code Online (Sandbox Code Playgroud)
我想最后的输出也应该是bash因为/etc/passwd显示 root 使用bash,我是否遗漏了任何关于sudo?
Sté*_*las 12
它使用_PATH_BSHELL像execvp()其在Linux上被定义为/bin/sh在/usr/include/paths.h。这应该与使用env或find -exec例如执行时相同。
它当然不应该使用用户的登录 shell。您在bash上面看到的事实是因为它bash(您在其中输入命令行的外壳)尝试执行它,并且当它从中获取ENOEXEC错误代码时,execve它决定用自己来解释它(在sh兼容模式下)。
因为您不使用-s选项,所以sudo将使用_PATH_BSHELL(/usr/include/paths.h在 Ubuntu 12.04 LTS 中定义)来设置$SHELL它运行。查看sudo源代码:
/* Stash user's shell for use with the -s flag; don't pass to plugin. */
if ((ud->shell = getenv("SHELL")) == NULL || ud->shell[0] == '\0') {
ud->shell = pw->pw_shell[0] ? pw->pw_shell : _PATH_BSHELL;
}
Run Code Online (Sandbox Code Playgroud)
如果您使用-s选项,sudo将使用您的$SHELL代替_PATH_BSHELL:
$ cat ./test.sh
ps | grep "$$" | awk '{ print $4 }'
$ ./test.sh
bash
$ sudo -s ./test.sh
bash
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2709 次 |
| 最近记录: |