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

Viewed 14181

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"
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

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).

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.

Related