Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ubuntu on windows 10 wsl2 - chown chmod doesn't work on copied files

I just installed ubuntu 20.04.1 LTS on windows 10 (2004) WSL 2 from windows store.

I want to switch from cygwin, so i copy cygwin ssh configuration from /mnt/d to ~ with

cp -R /mnt/d/dev/cygwin64/root/home/myuser/.ssh/ .

my usual ssh connection doesn't work and it seems to come from file/folder permission of ~/.ssh

a ls on this folder doesn't work

$ ls -al .ssh/
ls: cannot access '.ssh/id_rsa.pub': Permission denied
ls: cannot access '.ssh/id_rsa': Permission denied
ls: cannot access '.ssh/..': Permission denied
ls: cannot access '.ssh/.': Permission denied
ls: cannot access '.ssh/known_hosts': Permission denied
total 0
d????????? ? ? ? ?            ? .
d????????? ? ? ? ?            ? ..
-????????? ? ? ? ?            ? id_rsa
-????????? ? ? ? ?            ? id_rsa.pub
-????????? ? ? ? ?            ? known_hosts

i tried

sudo chown myuser:myuser .ssh
sudo chown myuser:myuser .ssh/*

.ssh is still the same with ????????

here is a view of the home folder

$ ls -al
total 44
drwxr-xr-x 5 myuser myuser 4096 Aug 25 10:54 .
drwxr-xr-x 3 root  root  4096 Aug 24 21:14 ..
-rw------- 1 myuser myuser  974 Aug 25 11:12 .bash_history
-rw-r--r-- 1 myuser myuser  220 Aug 24 21:14 .bash_logout
-rw-r--r-- 1 myuser myuser 5026 Aug 25 10:53 .bashrc
drwxr-xr-x 2 myuser myuser 4096 Aug 24 21:19 .docker
drwxr-xr-x 2 myuser myuser 4096 Aug 24 21:15 .landscape
-rw-r--r-- 1 myuser myuser    0 Aug 26 17:00 .motd_shown
-rw-r--r-- 1 myuser myuser  807 Aug 24 21:14 .profile
drw-r--r-- 2 myuser myuser 4096 Aug 25 10:54 .ssh
-rw-r--r-- 1 myuser myuser    0 Aug 24 21:16 .sudo_as_admin_successful
-rw------- 1 myuser myuser  792 Aug 25 10:53 .viminfo

I tried to add a wsl.conf file with automount without success

$ cat /etc/wsl.conf
[automount]
enabled = true
options = "metadata"
like image 318
Rexave Avatar asked Aug 26 '20 15:08

Rexave


People also ask

How do I change file permissions in wsl?

You can configure your file permissions inside of your Windows drives using the mount options in wsl. conf. The mount options allow you to set umask , dmask and fmask permissions masks. The umask is applied to all files, the dmask is applied just to directories and the fmask is applied just to files.

How do I copy files from WSL2 to Windows?

Suppose, create a text file using the “touch” command: Open the File Explorer again to check if the file is created: To copy the file in Windows, right-click on it and select copy: Paste it in the Windows directory where you want to transfer the file.

Can I have both wsl1 and WSL2?

You can run WSL 1 and WSL 2 distros side by side, and can upgrade and downgrade any distro at any time. Adding WSL 2 as a new architecture presents a better platform for the WSL team to deliver features that make WSL an amazing way to run a Linux environment in Windows.


3 Answers

Ssh configuration is per user so you actually would need to copy it to /home/myuser. https://devblogs.microsoft.com/commandline/sharing-ssh-keys-between-windows-and-wsl-2/ provides an excellent how-to. Summarized and tweaked to match Cygwin path:

As your WSL user:

Edit your /etc/wsl.conf to match the following:

[automount]
enabled = true
options = "metadata,uid=1000,gid=1000,umask=0022,fmask=11,case=off"
mountFsTab = false
crossDistro = true

[filesystem]
umask = 0022

[network]
generateHosts = true
generateResolvConf = true

[interop]
enabled = true
appendWindowsPath = true

The additional metadata options as well as filesystem option should help ensure permissions are properly assigned. You also need to reset your WSL distro session in order for the changes to be read and propagated. From CMD or Powershell, do wsl --shutdown. Enter your distro again, usually with wsl.exe

Delete previous file and folder .ssh (with bad permissions)

Then:

cp -r /mnt/c/dev/cygwin64/root/home/myuser/.ssh/ ~/.ssh
sudo chown myuser:myuser .ssh 
sudo chown myuser:myuser .ssh/*
sudo chmod 600 ~/.ssh/id_rsa
like image 147
WSLUser Avatar answered Oct 19 '22 02:10

WSLUser


Another solution would be to use the Ubuntu file system.

You can access it through the path \\wsl$ on Windows.

Using the Ubuntu file system might create other issues. But file permissions wont be one because you would solely use the Ubuntu file system and sync these files to Windows (not the other way around).

like image 26
Tobi G. Avatar answered Oct 19 '22 03:10

Tobi G.


To share your SSH Keys across both OS.

sudo umount /mnt/c

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

Or Try

 ln -s /mnt/c/Users/MyUsername/.ssh ~/.ssh

The catch is to mount your ssh key in your windows 10 in such a way that wsl can read it.

like image 28
Saif Siddiqui Avatar answered Oct 19 '22 02:10

Saif Siddiqui