wsl2使用vscode无法写入文件permission denied解决方法

文章目录

  • Problem
  • Solution
  • Reference
  • 总结

Problem

Come cross a problem in wsl2 to use vscode.

未能保存“settings.json”: 无法写入文件/code/.vscode/settings.json(NoPermissions (FileSystemError): Error: EACCES: permission denied

wsl2使用vscode无法写入文件permission denied解决方法_第1张图片

Solution

In official documentation, I found this:
wsl2使用vscode无法写入文件permission denied解决方法_第2张图片

在stackoverflow上,看到的这个解决方法
在/etc下面新建wsl.conf文件,然后对其进行配置

cd /etc
sudo vim wsl.conf
然后就是vim中复制进去信息(可见下文)即可
主要的是文件的权限的配置

wsl2使用vscode无法写入文件permission denied解决方法_第3张图片

官方文档(参考链接)中复制出来:

# Automatically mount Windows drive when the distribution is launched
[automount]

# Set to true will automount fixed drives (C:/ or D:/) with DrvFs under the root directory set above. Set to false means drives won't be mounted automatically, but need to be mounted manually or with fstab.
enabled = true

# Sets the directory where fixed drives will be automatically mounted. This example changes the mount location, so your C-drive would be /c, rather than the default /mnt/c. 
root = /

# DrvFs-specific options can be specified.  
options = "metadata,uid=1003,gid=1003,umask=077,fmask=11,case=off"

# Sets the `/etc/fstab` file to be processed when a WSL distribution is launched.
mountFsTab = true

# Network host settings that enable the DNS server used by WSL 2. This example changes the hostname, sets generateHosts to false, preventing WSL from the default behavior of auto-generating /etc/hosts, and sets generateResolvConf to false, preventing WSL from auto-generating /etc/resolv.conf, so that you can create your own (ie. nameserver 1.1.1.1).
[network]
hostname = DemoHost
generateHosts = false
generateResolvConf = false

# Set whether WSL supports interop process like launching Windows apps and adding path variables. Setting these to false will block the launch of Windows processes and block adding $PATH environment variables.
[interop]
enabled = false
appendWindowsPath = false

# Set the user when launching a distribution with WSL.
[user]
default = DemoUser

# Set a command to run when a new WSL instance launches. This example starts the Docker container service.
[boot]
command = service docker start

但是完全照抄会有如下问题,只怪自己没有仔细阅读。

PS E:\shizheng_coding\software_design_venkat\reference_text_books\book_code_functional_prog_Java_venkat> wsl
<3>init: (278) ERROR: CreateProcessEntryCommon:336: getpwnam(DemoUser) failed 0
<3>init: (278) ERROR: CreateProcessEntryCommon:517: chdir(/e/shizheng_coding/software_design_venkat/reference_text_books/book_code_functional_prog_Java_venkat) failed 13

其实,需要将上面官方文档中的配置修剪一下,只剩下自己需要的上面的报错就会消失

# Automatically mount Windows drive when the distribution is launched
[automount]

# Set to true will automount fixed drives (C:/ or D:/) with DrvFs under the root directory set above. Set to false means drives won't be mounted automatically, but need to be mounted manually or with fstab.
enabled = true


# DrvFs-specific options can be specified.  
options = "metadata,umask=22,fmask=11"

# Sets the `/etc/fstab` file to be processed when a WSL distribution is launched.
mountFsTab = true

这里的核心点是 all newly created files will use umask 22 (chmod 775) and fmask 11 (chmod 644),意思是所有新创建的文件都可以有775的权限,可读可写。

然后我把之前permission denied的文件:.vscode/settings.json 删掉了

 sudo rm -r .vscode/

然后重新运行java项目,会生成新的json文件,此时不会报错

Reference

https://stackoverflow.com/questions/61973385/vscode-unable-to-save-files-inside-my-wsl2-home-folder

https://docs.microsoft.com/en-us/windows/wsl/file-permissions

总结

遇到问题,解决之,记录之,经验重复使用,以免重蹈覆辙,反复踩坑。

你可能感兴趣的:(踩坑,Linux,wsl2,vscode)