Copy Files to Windows Subsystem for Linux WSL2 with SSH
In working with Windows Subsystem for Linux, you will undoubtedly want to work with files with your WSL Linux distribution. You may wonder how you can work with files effectively. This includes copying files both to and from Windows Subsystem for Linux distributions. This is much easier for large file edits and other configurations that lend themselves to a GUI text editor of sorts. Additionally, if you want to copy files back and forth between environments for development, it is important to be able to do this uninhibited. This post will take a look at how to copy files to Windows Subsystem for Linux WSL2 with SSH and see how to configure this.
Ways to copy files to Windows Subsystem for Linux WSL
By default, Windows Subsystem for Linux automatically mounts the C$ drive on the Windows 10 host system which allows you to see the files on the local hard drive. However, this is a one-way view. With WSL2, you can’t see the mounted file system as it is abstracted by way of the transparent Hyper-V utility VM that is used to house the WSL instance.
With that being said, you can certainly copy files from the mounted drive from the Windows 10 host and we will see how that is possible. However, most are familiar and used to working with Linux environments and file movement by way of SSH, SCP, SFTP, etc. So, we will take a look at how this can be enabled within the WSL/WSL2 Linux image.
Using the built-in /mnt for the host filesystem
As mentioned, by default, the Windows Subsystem for Linux automatically mounts the Windows 10 host C drive. With this being the case, you have automatic access to be able to copy files from the Windows 10 host to the Windows Subsystem for Linux WSL.
Below is a screenshot from a WSL Ubuntu 20.04 LTS image running. If you look at the mounts that have been provisioned, you will see the C drive for the Windows 10 host has been mounted and you can enumerate the files and folders on the Windows 10 host.
Below, I have created a testfile.txt file on the Windows 10 host C drive. Running the same ls/mnt/* command, we see the testfile.txt file listed. With this being the case, we can now simply run a Linux cp command to copy a file from the Windows C drive to a folder in the Windows Subsystem for Linux environment.
Copy Files to Windows Subsystem for Linux WSL2 with SSH
As you can imagine, it can get cumbersome to have to “sneakernet” files from your Windows host to your Windows Subsystem for Linux WSL environment. A much better way would be to have SSH access to your WSL Linux environment to allow easily moving files back and forth, editing WSL files directly, and other processes. Before you can use SSH to move files back and forth between the Windows 10 host and the Windows Subsystem for Linux environment, you need to enable SSH in your WSL Linux instance.
Enable SSH in Windows Subsystem for Linux WSL
For the most part, enabling SSH in the WSL environment follows suit with most Linux installations. However, there are a couple of quirks to note. The first, to have a clean slate with a working SSH configuration, the first thing to do is uninstall the openssh server that is installed by default. I had issues configuring the default installation of openssh. A quick Google saw others having issues as well. A quick uninstall and install of openssh resolves these.
sudo apt remove openssh-server
After uninstalling the openssh server, reinstall it.
sudo apt install openssh-server
After reinstalling openssh-server, edit the sshd_config file to allow password authentication.
nano /etc/ssh/sshd_config PasswordAuthentication yes
If you want to allow root to be able to login as well, you can allow this with the PermitRootLogin configuration.
PermitRootLogin yes
After editing your sshd_config file, restart your SSH service using the following command:
sudo service ssh restart
Now that you have the SSH service properties edited and the SSH service restarted, we can gather the information to connect. Find the IP address for your WSL installation.
sudo apt install net-tools ifconfig
Below, I have ran the ifconfig command and see the configured IP address.
Now, we can use something like WinSCP to connect to the WSL installation.
After connecting, we see the filesystem and are now in a position to start copying files to Windows Subsystem for Linux WSL and back to the Windows host if needed. Also, you can edit files directly.
Wrapping Up
Hopefully this primer on how to copy files to Windows Subsystem for Linux WSL2 with SSH will help any who may be struggling to effectively access their WSL or WSL2 Linux installations. WSL is a great tool for development and other IT operations tasks by giving quick access to a Linux environment to run Linux tools, utilities, and applications.