how to keep windows $PATH when 'sudo su -' on windows-subsystem-for-linux












0














I'm running Ubuntu (version 1804.2018.817.0) from the Window Store on top of Windows 10 (version 1803 build 17134.345) using the Window Subsystem for Linux.



When I first open the console I'm logged in with the account I created when I first ran Ubuntu. In this context I can execute things in the Windows path like cmd.exe.



However, when I switch to root using sudo su -, the $PATH variable no longer contains the Windows path information. So when I type cmd.exe it says cmd.exe: command not found.



My questions are:




  1. Where do the Window $PATH values come from when I first open Ubuntu?

  2. Why doesn't the root user get the Windows $PATH values when I use sudo su -?

  3. How can I configure bash so that $PATH contains Window path values when I use sudo su -, so that I can run commands like cmd.exe?










share|improve this question






















  • Open HKCUSoftwareMicrosoftWindowsCurrentVersionLxss in regedit > find ``DistributionName"="Ubuntu1804"` > What's the value of DefaultUid and Flags?
    – Biswapriyo
    Oct 12 '18 at 9:29
















0














I'm running Ubuntu (version 1804.2018.817.0) from the Window Store on top of Windows 10 (version 1803 build 17134.345) using the Window Subsystem for Linux.



When I first open the console I'm logged in with the account I created when I first ran Ubuntu. In this context I can execute things in the Windows path like cmd.exe.



However, when I switch to root using sudo su -, the $PATH variable no longer contains the Windows path information. So when I type cmd.exe it says cmd.exe: command not found.



My questions are:




  1. Where do the Window $PATH values come from when I first open Ubuntu?

  2. Why doesn't the root user get the Windows $PATH values when I use sudo su -?

  3. How can I configure bash so that $PATH contains Window path values when I use sudo su -, so that I can run commands like cmd.exe?










share|improve this question






















  • Open HKCUSoftwareMicrosoftWindowsCurrentVersionLxss in regedit > find ``DistributionName"="Ubuntu1804"` > What's the value of DefaultUid and Flags?
    – Biswapriyo
    Oct 12 '18 at 9:29














0












0








0







I'm running Ubuntu (version 1804.2018.817.0) from the Window Store on top of Windows 10 (version 1803 build 17134.345) using the Window Subsystem for Linux.



When I first open the console I'm logged in with the account I created when I first ran Ubuntu. In this context I can execute things in the Windows path like cmd.exe.



However, when I switch to root using sudo su -, the $PATH variable no longer contains the Windows path information. So when I type cmd.exe it says cmd.exe: command not found.



My questions are:




  1. Where do the Window $PATH values come from when I first open Ubuntu?

  2. Why doesn't the root user get the Windows $PATH values when I use sudo su -?

  3. How can I configure bash so that $PATH contains Window path values when I use sudo su -, so that I can run commands like cmd.exe?










share|improve this question













I'm running Ubuntu (version 1804.2018.817.0) from the Window Store on top of Windows 10 (version 1803 build 17134.345) using the Window Subsystem for Linux.



When I first open the console I'm logged in with the account I created when I first ran Ubuntu. In this context I can execute things in the Windows path like cmd.exe.



However, when I switch to root using sudo su -, the $PATH variable no longer contains the Windows path information. So when I type cmd.exe it says cmd.exe: command not found.



My questions are:




  1. Where do the Window $PATH values come from when I first open Ubuntu?

  2. Why doesn't the root user get the Windows $PATH values when I use sudo su -?

  3. How can I configure bash so that $PATH contains Window path values when I use sudo su -, so that I can run commands like cmd.exe?







bash ubuntu windows-subsystem-for-linux






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 12 '18 at 9:05









RafeRafe

2,62053557




2,62053557












  • Open HKCUSoftwareMicrosoftWindowsCurrentVersionLxss in regedit > find ``DistributionName"="Ubuntu1804"` > What's the value of DefaultUid and Flags?
    – Biswapriyo
    Oct 12 '18 at 9:29


















  • Open HKCUSoftwareMicrosoftWindowsCurrentVersionLxss in regedit > find ``DistributionName"="Ubuntu1804"` > What's the value of DefaultUid and Flags?
    – Biswapriyo
    Oct 12 '18 at 9:29
















Open HKCUSoftwareMicrosoftWindowsCurrentVersionLxss in regedit > find ``DistributionName"="Ubuntu1804"` > What's the value of DefaultUid and Flags?
– Biswapriyo
Oct 12 '18 at 9:29




Open HKCUSoftwareMicrosoftWindowsCurrentVersionLxss in regedit > find ``DistributionName"="Ubuntu1804"` > What's the value of DefaultUid and Flags?
– Biswapriyo
Oct 12 '18 at 9:29












1 Answer
1






active

oldest

votes


















0














I answer although this topic belongs rather to askubuntu.




  1. Where do the Window $PATH values come from when I first open Ubuntu?.

    From standard Windows %PATH% environment variable because ubuntu.exe process is a child of conhost.exe (as well as the Windows command prompt cmd.exe).


  2. Why doesn't the root user get the Windows $PATH values when I use sudo su -?
    Check sudo sudo -V | grep PATH from wsl prompt. Read man sudo for the explanation:




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.


The default security policy is sudoers, which is configured via
the file /etc/sudoers.




Try this illustrative example (paste following three commands severally into an open cmd command prompt):



wsl echo ${#PATH}
wsl sudo su -c 'echo ${PATH} ^&^& echo ${#PATH}'
wsl sudo su - -c 'echo ${PATH} ^&^& echo ${#PATH} ^&^& cat /etc/sudoers ^| grep Default'


Result (the latter command takes a look at /etc/sudoers as well):



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"


Read man sudoers as well for further explanation of Defaults options:




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.





  1. How can I configure bash so that $PATH contains Window path values when I use sudo su -, so that I can run commands like cmd.exe?




    • You could use env_keep option via sudoedit (not recommended for security reasons) to preserve your user's environment variables.

    • For swift advance, copy and paste the following into the root prompt. It will add all folder paths from current Windows %path% variable to $PATH. You might consider make it a .sh script and add some advanced logic:




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/L1#' -e 's#\#/#g' <<< "${c_p_n[$i]}"); done
echo "${#PATH}" # merely for documentation
cmd.exe /c echo works! # merely for documentation


Result:



user@COMP:~$ sudo su -
root@COMP:~# cmd.exe /c echo works! # merely for documentation
cmd.exe: command not found
root@COMP:~# echo "${#PATH}" # merely for documentation
70
root@COMP:~# cmd_path=`/mnt/c/Windows/System32/cmd.exe /C echo "%path:\\=\\\\%"`
root@COMP:~# OIFS="$IFS" # backup $IFS
root@COMP:~# IFS=';'
root@COMP:~# read -a c_p_n <<< "${cmd_path}"
root@COMP:~# IFS="$OIFS" # restore $IFS
root@COMP:~# OPATH="${PATH}" # backup $PATH
root@COMP:~# for i in "${!c_p_n[@]}"; do PATH=${PATH%/}:$(sed -e 's#^(.):#/mnt/L1#' -e 's#\#/#g' <<< "${c_p_n[$i]}"); done
root@COMP:~# echo "${#PATH}" # merely for documentation
1555
root@COMP:~# cmd.exe /c echo works! # merely for documentation
works!





share|improve this answer























  • I also suggest reading the man page for su. It calls out the - argument in su -: - ... start the shell as a login shell .... initializes the environment variables HOME, SHELL, USER, LOGNAME, and PATH. In other words, by su -, you are telling it to throw away the environment variables such as PATH of the current user.
    – omajid
    Nov 21 '18 at 22:48










  • @omajid Compare wsl sudo su - -c 'echo $PATH ^&^& cat /etc/sudoers ^| grep Defaults' (note the - argument) and wsl sudo su -c 'echo $PATH ^&^& cat /etc/sudoers ^| grep Defaults'. Different $PATH, different secure_path. In any way, Windows %path% equivalent is missing thru env_reset.
    – JosefZ
    Nov 21 '18 at 23:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52776019%2fhow-to-keep-windows-path-when-sudo-su-on-windows-subsystem-for-linux%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I answer although this topic belongs rather to askubuntu.




  1. Where do the Window $PATH values come from when I first open Ubuntu?.

    From standard Windows %PATH% environment variable because ubuntu.exe process is a child of conhost.exe (as well as the Windows command prompt cmd.exe).


  2. Why doesn't the root user get the Windows $PATH values when I use sudo su -?
    Check sudo sudo -V | grep PATH from wsl prompt. Read man sudo for the explanation:




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.


The default security policy is sudoers, which is configured via
the file /etc/sudoers.




Try this illustrative example (paste following three commands severally into an open cmd command prompt):



wsl echo ${#PATH}
wsl sudo su -c 'echo ${PATH} ^&^& echo ${#PATH}'
wsl sudo su - -c 'echo ${PATH} ^&^& echo ${#PATH} ^&^& cat /etc/sudoers ^| grep Default'


Result (the latter command takes a look at /etc/sudoers as well):



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"


Read man sudoers as well for further explanation of Defaults options:




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.





  1. How can I configure bash so that $PATH contains Window path values when I use sudo su -, so that I can run commands like cmd.exe?




    • You could use env_keep option via sudoedit (not recommended for security reasons) to preserve your user's environment variables.

    • For swift advance, copy and paste the following into the root prompt. It will add all folder paths from current Windows %path% variable to $PATH. You might consider make it a .sh script and add some advanced logic:




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/L1#' -e 's#\#/#g' <<< "${c_p_n[$i]}"); done
echo "${#PATH}" # merely for documentation
cmd.exe /c echo works! # merely for documentation


Result:



user@COMP:~$ sudo su -
root@COMP:~# cmd.exe /c echo works! # merely for documentation
cmd.exe: command not found
root@COMP:~# echo "${#PATH}" # merely for documentation
70
root@COMP:~# cmd_path=`/mnt/c/Windows/System32/cmd.exe /C echo "%path:\\=\\\\%"`
root@COMP:~# OIFS="$IFS" # backup $IFS
root@COMP:~# IFS=';'
root@COMP:~# read -a c_p_n <<< "${cmd_path}"
root@COMP:~# IFS="$OIFS" # restore $IFS
root@COMP:~# OPATH="${PATH}" # backup $PATH
root@COMP:~# for i in "${!c_p_n[@]}"; do PATH=${PATH%/}:$(sed -e 's#^(.):#/mnt/L1#' -e 's#\#/#g' <<< "${c_p_n[$i]}"); done
root@COMP:~# echo "${#PATH}" # merely for documentation
1555
root@COMP:~# cmd.exe /c echo works! # merely for documentation
works!





share|improve this answer























  • I also suggest reading the man page for su. It calls out the - argument in su -: - ... start the shell as a login shell .... initializes the environment variables HOME, SHELL, USER, LOGNAME, and PATH. In other words, by su -, you are telling it to throw away the environment variables such as PATH of the current user.
    – omajid
    Nov 21 '18 at 22:48










  • @omajid Compare wsl sudo su - -c 'echo $PATH ^&^& cat /etc/sudoers ^| grep Defaults' (note the - argument) and wsl sudo su -c 'echo $PATH ^&^& cat /etc/sudoers ^| grep Defaults'. Different $PATH, different secure_path. In any way, Windows %path% equivalent is missing thru env_reset.
    – JosefZ
    Nov 21 '18 at 23:12
















0














I answer although this topic belongs rather to askubuntu.




  1. Where do the Window $PATH values come from when I first open Ubuntu?.

    From standard Windows %PATH% environment variable because ubuntu.exe process is a child of conhost.exe (as well as the Windows command prompt cmd.exe).


  2. Why doesn't the root user get the Windows $PATH values when I use sudo su -?
    Check sudo sudo -V | grep PATH from wsl prompt. Read man sudo for the explanation:




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.


The default security policy is sudoers, which is configured via
the file /etc/sudoers.




Try this illustrative example (paste following three commands severally into an open cmd command prompt):



wsl echo ${#PATH}
wsl sudo su -c 'echo ${PATH} ^&^& echo ${#PATH}'
wsl sudo su - -c 'echo ${PATH} ^&^& echo ${#PATH} ^&^& cat /etc/sudoers ^| grep Default'


Result (the latter command takes a look at /etc/sudoers as well):



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"


Read man sudoers as well for further explanation of Defaults options:




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.





  1. How can I configure bash so that $PATH contains Window path values when I use sudo su -, so that I can run commands like cmd.exe?




    • You could use env_keep option via sudoedit (not recommended for security reasons) to preserve your user's environment variables.

    • For swift advance, copy and paste the following into the root prompt. It will add all folder paths from current Windows %path% variable to $PATH. You might consider make it a .sh script and add some advanced logic:




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/L1#' -e 's#\#/#g' <<< "${c_p_n[$i]}"); done
echo "${#PATH}" # merely for documentation
cmd.exe /c echo works! # merely for documentation


Result:



user@COMP:~$ sudo su -
root@COMP:~# cmd.exe /c echo works! # merely for documentation
cmd.exe: command not found
root@COMP:~# echo "${#PATH}" # merely for documentation
70
root@COMP:~# cmd_path=`/mnt/c/Windows/System32/cmd.exe /C echo "%path:\\=\\\\%"`
root@COMP:~# OIFS="$IFS" # backup $IFS
root@COMP:~# IFS=';'
root@COMP:~# read -a c_p_n <<< "${cmd_path}"
root@COMP:~# IFS="$OIFS" # restore $IFS
root@COMP:~# OPATH="${PATH}" # backup $PATH
root@COMP:~# for i in "${!c_p_n[@]}"; do PATH=${PATH%/}:$(sed -e 's#^(.):#/mnt/L1#' -e 's#\#/#g' <<< "${c_p_n[$i]}"); done
root@COMP:~# echo "${#PATH}" # merely for documentation
1555
root@COMP:~# cmd.exe /c echo works! # merely for documentation
works!





share|improve this answer























  • I also suggest reading the man page for su. It calls out the - argument in su -: - ... start the shell as a login shell .... initializes the environment variables HOME, SHELL, USER, LOGNAME, and PATH. In other words, by su -, you are telling it to throw away the environment variables such as PATH of the current user.
    – omajid
    Nov 21 '18 at 22:48










  • @omajid Compare wsl sudo su - -c 'echo $PATH ^&^& cat /etc/sudoers ^| grep Defaults' (note the - argument) and wsl sudo su -c 'echo $PATH ^&^& cat /etc/sudoers ^| grep Defaults'. Different $PATH, different secure_path. In any way, Windows %path% equivalent is missing thru env_reset.
    – JosefZ
    Nov 21 '18 at 23:12














0












0








0






I answer although this topic belongs rather to askubuntu.




  1. Where do the Window $PATH values come from when I first open Ubuntu?.

    From standard Windows %PATH% environment variable because ubuntu.exe process is a child of conhost.exe (as well as the Windows command prompt cmd.exe).


  2. Why doesn't the root user get the Windows $PATH values when I use sudo su -?
    Check sudo sudo -V | grep PATH from wsl prompt. Read man sudo for the explanation:




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.


The default security policy is sudoers, which is configured via
the file /etc/sudoers.




Try this illustrative example (paste following three commands severally into an open cmd command prompt):



wsl echo ${#PATH}
wsl sudo su -c 'echo ${PATH} ^&^& echo ${#PATH}'
wsl sudo su - -c 'echo ${PATH} ^&^& echo ${#PATH} ^&^& cat /etc/sudoers ^| grep Default'


Result (the latter command takes a look at /etc/sudoers as well):



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"


Read man sudoers as well for further explanation of Defaults options:




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.





  1. How can I configure bash so that $PATH contains Window path values when I use sudo su -, so that I can run commands like cmd.exe?




    • You could use env_keep option via sudoedit (not recommended for security reasons) to preserve your user's environment variables.

    • For swift advance, copy and paste the following into the root prompt. It will add all folder paths from current Windows %path% variable to $PATH. You might consider make it a .sh script and add some advanced logic:




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/L1#' -e 's#\#/#g' <<< "${c_p_n[$i]}"); done
echo "${#PATH}" # merely for documentation
cmd.exe /c echo works! # merely for documentation


Result:



user@COMP:~$ sudo su -
root@COMP:~# cmd.exe /c echo works! # merely for documentation
cmd.exe: command not found
root@COMP:~# echo "${#PATH}" # merely for documentation
70
root@COMP:~# cmd_path=`/mnt/c/Windows/System32/cmd.exe /C echo "%path:\\=\\\\%"`
root@COMP:~# OIFS="$IFS" # backup $IFS
root@COMP:~# IFS=';'
root@COMP:~# read -a c_p_n <<< "${cmd_path}"
root@COMP:~# IFS="$OIFS" # restore $IFS
root@COMP:~# OPATH="${PATH}" # backup $PATH
root@COMP:~# for i in "${!c_p_n[@]}"; do PATH=${PATH%/}:$(sed -e 's#^(.):#/mnt/L1#' -e 's#\#/#g' <<< "${c_p_n[$i]}"); done
root@COMP:~# echo "${#PATH}" # merely for documentation
1555
root@COMP:~# cmd.exe /c echo works! # merely for documentation
works!





share|improve this answer














I answer although this topic belongs rather to askubuntu.




  1. Where do the Window $PATH values come from when I first open Ubuntu?.

    From standard Windows %PATH% environment variable because ubuntu.exe process is a child of conhost.exe (as well as the Windows command prompt cmd.exe).


  2. Why doesn't the root user get the Windows $PATH values when I use sudo su -?
    Check sudo sudo -V | grep PATH from wsl prompt. Read man sudo for the explanation:




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.


The default security policy is sudoers, which is configured via
the file /etc/sudoers.




Try this illustrative example (paste following three commands severally into an open cmd command prompt):



wsl echo ${#PATH}
wsl sudo su -c 'echo ${PATH} ^&^& echo ${#PATH}'
wsl sudo su - -c 'echo ${PATH} ^&^& echo ${#PATH} ^&^& cat /etc/sudoers ^| grep Default'


Result (the latter command takes a look at /etc/sudoers as well):



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"


Read man sudoers as well for further explanation of Defaults options:




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.





  1. How can I configure bash so that $PATH contains Window path values when I use sudo su -, so that I can run commands like cmd.exe?




    • You could use env_keep option via sudoedit (not recommended for security reasons) to preserve your user's environment variables.

    • For swift advance, copy and paste the following into the root prompt. It will add all folder paths from current Windows %path% variable to $PATH. You might consider make it a .sh script and add some advanced logic:




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/L1#' -e 's#\#/#g' <<< "${c_p_n[$i]}"); done
echo "${#PATH}" # merely for documentation
cmd.exe /c echo works! # merely for documentation


Result:



user@COMP:~$ sudo su -
root@COMP:~# cmd.exe /c echo works! # merely for documentation
cmd.exe: command not found
root@COMP:~# echo "${#PATH}" # merely for documentation
70
root@COMP:~# cmd_path=`/mnt/c/Windows/System32/cmd.exe /C echo "%path:\\=\\\\%"`
root@COMP:~# OIFS="$IFS" # backup $IFS
root@COMP:~# IFS=';'
root@COMP:~# read -a c_p_n <<< "${cmd_path}"
root@COMP:~# IFS="$OIFS" # restore $IFS
root@COMP:~# OPATH="${PATH}" # backup $PATH
root@COMP:~# for i in "${!c_p_n[@]}"; do PATH=${PATH%/}:$(sed -e 's#^(.):#/mnt/L1#' -e 's#\#/#g' <<< "${c_p_n[$i]}"); done
root@COMP:~# echo "${#PATH}" # merely for documentation
1555
root@COMP:~# cmd.exe /c echo works! # merely for documentation
works!






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 20:12

























answered Nov 21 '18 at 22:35









JosefZJosefZ

15.7k42040




15.7k42040












  • I also suggest reading the man page for su. It calls out the - argument in su -: - ... start the shell as a login shell .... initializes the environment variables HOME, SHELL, USER, LOGNAME, and PATH. In other words, by su -, you are telling it to throw away the environment variables such as PATH of the current user.
    – omajid
    Nov 21 '18 at 22:48










  • @omajid Compare wsl sudo su - -c 'echo $PATH ^&^& cat /etc/sudoers ^| grep Defaults' (note the - argument) and wsl sudo su -c 'echo $PATH ^&^& cat /etc/sudoers ^| grep Defaults'. Different $PATH, different secure_path. In any way, Windows %path% equivalent is missing thru env_reset.
    – JosefZ
    Nov 21 '18 at 23:12


















  • I also suggest reading the man page for su. It calls out the - argument in su -: - ... start the shell as a login shell .... initializes the environment variables HOME, SHELL, USER, LOGNAME, and PATH. In other words, by su -, you are telling it to throw away the environment variables such as PATH of the current user.
    – omajid
    Nov 21 '18 at 22:48










  • @omajid Compare wsl sudo su - -c 'echo $PATH ^&^& cat /etc/sudoers ^| grep Defaults' (note the - argument) and wsl sudo su -c 'echo $PATH ^&^& cat /etc/sudoers ^| grep Defaults'. Different $PATH, different secure_path. In any way, Windows %path% equivalent is missing thru env_reset.
    – JosefZ
    Nov 21 '18 at 23:12
















I also suggest reading the man page for su. It calls out the - argument in su -: - ... start the shell as a login shell .... initializes the environment variables HOME, SHELL, USER, LOGNAME, and PATH. In other words, by su -, you are telling it to throw away the environment variables such as PATH of the current user.
– omajid
Nov 21 '18 at 22:48




I also suggest reading the man page for su. It calls out the - argument in su -: - ... start the shell as a login shell .... initializes the environment variables HOME, SHELL, USER, LOGNAME, and PATH. In other words, by su -, you are telling it to throw away the environment variables such as PATH of the current user.
– omajid
Nov 21 '18 at 22:48












@omajid Compare wsl sudo su - -c 'echo $PATH ^&^& cat /etc/sudoers ^| grep Defaults' (note the - argument) and wsl sudo su -c 'echo $PATH ^&^& cat /etc/sudoers ^| grep Defaults'. Different $PATH, different secure_path. In any way, Windows %path% equivalent is missing thru env_reset.
– JosefZ
Nov 21 '18 at 23:12




@omajid Compare wsl sudo su - -c 'echo $PATH ^&^& cat /etc/sudoers ^| grep Defaults' (note the - argument) and wsl sudo su -c 'echo $PATH ^&^& cat /etc/sudoers ^| grep Defaults'. Different $PATH, different secure_path. In any way, Windows %path% equivalent is missing thru env_reset.
– JosefZ
Nov 21 '18 at 23:12


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52776019%2fhow-to-keep-windows-path-when-sudo-su-on-windows-subsystem-for-linux%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python