WSL中的文件权限设置

WSL中默认的文件权限是777,把它修改为和Linux默认的一致(目录755,文件644)。

WSL目录下的文件

.profile.zshenv或中加入:

1
2
3
if [[ "$(umask)" == '000' ]]; then
umask 022
fi

Windows目录下的文件

/etc/wsl.conf中加入(如果文件不存在,就新建一个):

1
2
3
4
5
[automount]
enabled = true
root = /mnt/
options = "metadata,dmask=022,fmask=133"
mountFsTab = false

如果在options中直接设置umask=022或者设置中包含fmask=133, wsl-terminalWSLtty将无法运行,而从Windows Store安装的WSL程序可以运行。猜测是因为wsl-terminal和WSLtty的可执行程序以644的权限挂载,导致没有运行权限而无法运行。可能的解决方法(未验证),设置mountFsTab = true,然后在etc/fstab中单独设置每个盘的挂载选项,把wsl-terminal或WSLtty所在的盘以744的权限(fmask = 033)挂载。

References

1. http://zuyunfei.com/2018/06/15/file-system-configuration-in-wsl/
2. https://blogs.msdn.microsoft.com/commandline/2018/02/07/automatically-configuring-wsl/