在Windows-Subsystem-for-Linux上如何在'sudo su-'时保持Windows $ PATH

时间:2018-10-12 09:05:38

标签: bash ubuntu windows-subsystem-for-linux

我正在使用Windows子系统从Windows 10顶部的Window Store(版本1803 build 17134.345)运行Ubuntu(版本1804.2018.817.0)。

当我第一次打开控制台时,我使用我第一次运行Ubuntu时创建的帐户登录。在这种情况下,我可以在Windows路径中执行诸如cmd.exe之类的事情。

但是,当我使用sudo su -切换到root时,$ PATH变量不再包含Windows路径信息。因此,当我输入cmd.exe时,它会显示cmd.exe: command not found

我的问题是:

  1. 第一次打开Ubuntu时,Window $ PATH值从哪里来?
  2. 当我使用sudo su -时,root用户为什么不获得Windows $ PATH值?
  3. 当我使用sudo su -时,如何配置bash以使$ PATH包含窗口路径值,以便我可以运行诸如cmd.exe之类的命令?

1 个答案:

答案 0 :(得分:0)

我回答,尽管该主题属于askubuntu

  1. 第一次打开Ubuntu时,Window $ PATH值从哪里来?
    来自标准Windows %PATH% environment variable,因为ubuntu.exe进程是conhost.exe的子级(以及Windows命令提示符cmd.exe)。

  2. 为什么我使用sudo su -时root用户不能获得Windows $ PATH值?
    从{{1}中检查sudo sudo -V | grep PATH }提示。阅读wsl以获得解释:

man sudo
     

默认安全策略为 sudoers ,该策略通过   文件ENVIRONMENT sudo utilizes … environment variables. The security policy has control over the actual content of the command's environment. … PATH May be overridden by the security policy.

尝试下面的示例(将以下三个命令分别粘贴到打开的/etc/sudoers命令提示符中):

cmd

结果(后一个命令也查看wsl echo ${#PATH} wsl sudo su -c 'echo ${PATH} ^&^& echo ${#PATH}' wsl sudo su - -c 'echo ${PATH} ^&^& echo ${#PATH} ^&^& cat /etc/sudoers ^| grep Default' ):

/etc/sudoers

还请阅读d:\test> wsl echo ${#PATH} 1557 d:\test> wsl sudo su -c 'echo ${PATH} ^&^& echo ${#PATH}' [sudo] password for user: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games 88 d:\test> wsl sudo su - -c 'echo ${PATH} ^&^& echo ${#PATH} ^&^& cat /etc/sudoers ^| grep Default' [sudo] password for user: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin 70 Defaults env_reset Defaults mail_badpass Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin" ,以进一步了解默认选项:

man sudoers
  1. 如何配置bash,以便在使用env_reset If set, sudo will run the command in a minimal environment containing the TERM, PATH, HOME, MAIL, SHELL, LOGNAME, USER, USERNAME and SUDO_* variables… If the secure_path option is set, its value will be used for the PATH environment variable. This flag is on by default. secure_path Path used for every command run from sudo. If you don't trust the people running sudo to have a sane PATH environment variable you may want to use this. Another use is if you want to have the “root path” be separate from the “user path”. Users in the group specified by the exempt_group option are not affected by secure_path. This option is not set by default. 时使$ PATH包含窗口路径值,以便我可以运行诸如sudo su - 的命令? >

    • 您可以通过cmd.exe使用env_keep选项(不建议security reasons使用)保留用户的环境变量。
    • 要快速前进,请将以下内容复制并粘贴到sudoedit提示符下。它将所有文件夹路径从当前Windows root变量添加到%path%。您可以考虑使其成为$PATH脚本并添加一些高级逻辑:
.sh

结果

cmd.exe /c echo works!            # merely for documentation
echo "${#PATH}"                   # merely for documentation
cmd_path=`/mnt/c/Windows/System32/cmd.exe /C echo "%path:\\\\=\\\\\\\\%"`
OIFS="$IFS"                       # backup $IFS
IFS=';'
read -a c_p_n <<< "${cmd_path}"
IFS="$OIFS"                       # restore $IFS
OPATH="${PATH}"                   # backup  $PATH
for i in "${!c_p_n[@]}"; do PATH=${PATH%/}:$(sed -e 's#^\(.\):#/mnt/\L\1#' -e 's#\\#/#g' <<< "${c_p_n[$i]}"); done
echo "${#PATH}"                   # merely for documentation
cmd.exe /c echo works!            # merely for documentation