在WSL中如何让root继承Windows的$PATH?

问题描述 投票:0回答:1

在WSL中如何让root继承Windows的$PATH?就像我的非 root 用户一样? 这是我的问题:

hans@DESKTOP-41ABH79:~/projects$ code test.txt
hans@DESKTOP-41ABH79:~/projects$ sudo code test.txt
sudo: code: command not found
hans@DESKTOP-41ABH79:~/projects$ bash -c 'echo $PATH'
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files/WindowsApps/MicrosoftCorporationII.WindowsSubsystemForLinux_2.0.14.0_x64__8wekyb3d8bbwe:/mnt/c/Program Files (x86)/VMware/VMware Player/bin/:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files/Microsoft VS Code/bin:/mnt/c/PHP:/mnt/c/ffmpeg/bin:/mnt/c/Program Files/VideoLAN/VLC:/mnt/c/Users/hans/AppData/Local/Microsoft/WindowsApps:/mnt/c/Program Files/mitmproxy/bin:/snap/bin
hans@DESKTOP-41ABH79:~/projects$ sudo bash -c 'echo $PATH'
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
hans@DESKTOP-41ABH79:~/projects$ bash -c 'type code'
code is /mnt/c/Program Files/Microsoft VS Code/bin/code
hans@DESKTOP-41ABH79:~/projects$ sudo bash -c 'type code'
bash: line 1: type: code: not found
path environment-variables windows-subsystem-for-linux
1个回答
0
投票

对此解决方案不满意,因为它需要每次 Windows PATH 更改时手动更新 /etc/environment,但手动将路径添加到 /etc/environment 确实有效:

root@DESKTOP-41ABH79:/home/hans# cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files/WindowsApps/MicrosoftCorporationII.WindowsSubsystemForLinux_2.0.14.0_x64__8wekyb3d8bbwe:/mnt/c/Program Files (x86)/VMware/VMware Player/bin/:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files/Microsoft VS Code/bin:/mnt/c/PHP:/mnt/c/ffmpeg/bin:/mnt/c/Program Files/VideoLAN/VLC:/mnt/c/Users/hans/AppData/Local/Microsoft/WindowsApps:/mnt/c/Program Files/mitmproxy/bin:/snap/bin"

root@DESKTOP-41ABH79:/home/hans# code test.txt

root@DESKTOP-41ABH79:/home/hans#
© www.soinside.com 2019 - 2024. All rights reserved.