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/