gitwindows-10windows-subsystem-for-linuxputtygit-for-windows

Share Git and SSH config between Windows 10 and WSL2


I have Putty and Git installed on Windows 10, and I'm also using WSL2 with Ubuntu 20.04 LTS. Sometimes I want to use SSH and Git from WSL2, other times in Windows. Everything works fine, except I have to duplicate my .gitconfig, .gitignore and .ssh files in both my Windows and WSL2 home directories.

I don't want to maintain this config in two places, so I thought I could symlink the files in WSL2:

# In /home/myuser on WSL2
ln -s /mnt/c/Users/myuser/.ssh
ln -s /mnt/c/Users/myuser/.gitconfig
ln -s /mnt/c/Users/myuser/.gitignore

This kind of works, except for a few problems.

  1. Files in /mnt/c have 0777 permissions and chmod obviously doesn't do anything on them. When I use ssh in WSL2, it complains about the key: Permissions 0777 for '/home/myuser/.ssh/id_rsa' are too open
  2. In .gitconfig I have core.excludesfile set to C:\\\\Users\\myuser\\.gitignore, which works when using Git from Windows but not from WSL2. If I change it to /home/myuser/.gitignore it works in WSL2 but not on Windows. I tried using the relative path .gitignore and completely removing the core.excludesfile directive, but neither worked.

Does anyone know a work around to these issues? It did occur to me that Putty doesn't care about the SSH key permissions, so I could move the files to WSL2 and then update my Putty connections (I have a lot of them!) to access the keys via \\wsl$. However, that wouldn't solve the core.excludesfile problem and I'd prefer to maintain the config in Windows and access it in WSL2 via /mnt/c.


Additional Context

I'm converting my dev environment from Windows to WSL2, using PhpStorm and Docker for PHP development. I have switched Docker to use the WSL2 backend instead of Hyper-V and moved my project files (i.e. git repos) to WSL2. I then open the project in PhpStorm using the \\wsl$ share. All of this is working great and I love how much faster it is, but having to duplicate the git config and ssh keys is annoying.


Solution

  • The solution I have found is to mount the windows ssh config folder over the WSL copy.

    In your /etc/fstab in WSL1/2, add the corresponding entries:

    # WSL 1 - mount C: with correct permissions, then bind mount .ssh
    C:                              /mnt/c                  drvfs   rw,noatime,uid=1000,gid=1000,case=off,umask=0077,fmask=0077 0 0
    /mnt/c/Users/me/.ssh            /home/me/.ssh           none    bind,default    0 0
     
    # WSL2
    C:\Users\me\.ssh\        /home/me/.ssh    drvfs rw,noatime,uid=1000,gid=1000,case=off,umask=0077,fmask=0177 0 0
    

    Updating the usernames and UID/GID as needed.