WSL移除PATH中Windows共享的位置

以下运行环境为Windows 10 1909 18363.592,理论上1809之后的版本都通用

WSL默认启动后查看PATH变量内容如下:
在这里插入图片描述
可以看到大量/mnt/c开头的位置,这些都是从Windows系统的PATH变量中合并过来的,这样就容易出现在WSL中如果有和Windows中相同名称的命令,可能会出现调用紊乱,下面记录一下如何让WSL保持自己PATH变量的纯洁性~

$ sudo vim /etc/wsl.conf

这个wsl.conf文件默认是不存在的,直接编辑内容如下:

# 不加载Windows中的PATH内容
[interop]
appendWindowsPath = false

# 不自动挂载Windows系统所有磁盘分区
[automount]
enabled = false

保存退出后关闭WSL,再重新运行WSL,发现并没有卵用?然后在Github上找到了这个Issue

In 1903 we made a change to keep distribuitons running for a while after your last client exits. You could have hit this in 1809, but the Windows was very small. You’d also hit this if you launched something in the background (keeping the distro alive).
Work-around is to manually terminate your distro after modifying /etc/wsl.conf using:
wsl.exe --terminate <distro_name>

好吧~开个cmd执行下面命令:

wsl --list
# 适用于 Linux 的 Windows 子系统:
# Ubuntu-18.04 (默认)
wsl --terminate Ubuntu-18.04

再启动WSL,OJBK了~
在这里插入图片描述

wsl.conf微软官方完整文档:
https://devblogs.microsoft.com/commandline/automatically-configuring-wsl/

发布了202 篇原创文章 · 获赞 92 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/lpwmm/article/details/104326310