Windows

Copy Files to Windows Subsystem for Linux WSL2 with SSH

Copy Files to Windows Subsystem for Linux WSL2 with SSH by default mount in WSL as well as using the steps to enable SSH in WSL and WSL2

Highlights

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

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.

Looking-at-the-built-in-mounts-that-allow-seeing-the-Windows-10-host-file-system
Looking at the built-in mounts that allow seeing the Windows 10 host file system

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.

Copying-a-local-Windows-10-file-to-your-Windows-Subsystem-for-Linux-instance
Copying a local Windows 10 file to your Windows Subsystem for Linux instance

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
Uninstall-openssh-in-Windows-Subsystem-for-Linux
Uninstall openssh in Windows Subsystem for Linux

After uninstalling the openssh server, reinstall it.

sudo apt install openssh-server
Install-openssh-in-the-Windows-Subsystem-for-Linux-Ubuntu-image-1
Install openssh in the Windows Subsystem for Linux Ubuntu image

After reinstalling openssh-server, edit the sshd_config file to allow password authentication.

nano /etc/ssh/sshd_config
PasswordAuthentication yes
Allow-password-authentication-in-the-sshd_config-file
Allow password authentication in the sshd_config file

If you want to allow root to be able to login as well, you can allow this with the PermitRootLogin configuration.

PermitRootLogin yes
Permit-root-login-via-SSH-to-Windows-Subsystem-for-Linux
Permit root login via SSH to Windows Subsystem for Linux

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.

Find-the-IP-address-for-the-Windows-Subsytem-for-Linux-installation
Find the IP address for the Windows Subsytem for Linux installation

Now, we can use something like WinSCP to connect to the WSL installation.

Connect-via-WinSCP-to-the-WSL-installation
Connect via WinSCP 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.

Connected-to-WSL-via-SSH-using-WinSCP
Connected to WSL via SSH using WinSCP

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.

Subscribe to VirtualizationHowto via Email 🔔

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com and has over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, Brandon has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.