Chmod WSL (Bash)不工作

在 Windows10上运行 bash,下面的简单语法在我 SSH 到我的 webserver 时起作用,但是在我退出并在本地机器上时就不起作用了。它没有给我一个错误,但我可以看到权限是不变的。我必须检查我在我的计算机上设置为管理员。这是一个错误还是仅仅是本地操作系统是窗口的结果?如果是后者,我会质疑在 Windows 上使用 bash 的价值,如果这样的常见操作不起作用的话。

$chmod 644 filename
65511 次浏览

There was an update to WSL recently (source), which lets you change permissions to files (Insider Build 17063).

All you have to do is to run:

sudo umount /mnt/c
sudo mount -t drvfs C: /mnt/c -o metadata

To enable changing file owners & permissions, you need to edit /etc/wsl.conf and insert the below config options:

[automount]
options = "metadata"

Do this inside the WSL shell, potentially needing sudo to edit/create the file.

This may require restarting WSL (such as with wsl --shutdown which is a Windows command, not one within WSL) or the host machine to take effect. This has been possible since 2018:

You can now set the owner and group of files using chmod/chown and modify read/write/execute permissions in WSL. You can also create special files like fifos, unix sockets, and device files. We’re introducing new mounting options with DrvFs for projecting permissions onto files alongside providing new Linux metadata on files and folders.

[cite: Microsoft Dev Blog]


You can also temporarily re-mount a drive with the following commands:

sudo umount /mnt/c
sudo mount -t drvfs C: /mnt/c -o metadata

...but please note, the command only takes effect in session scope. If you exit current bash, you'll lose your settings (credit: answerer Amade).


Reference:

Automatically Configuring WSL

Both Amades and Chaos answers are correct. But it only works for local drives not for mapped network drives. Z: is one of my network drives. Same operation on /mnt/c/Users/xxx/ works fine.

$sudo mount -t drvfs Z: /mnt/z -o metadata
$touch test
$chmod +w test
chmod: changing permissions of 'test': Operation not permitted

This is a known issue, see drvfs: metadata (chmod\chown) possible for mounted SMB drives?