Search for your favourite WSL tips below! Use the search box or simply browse the full list 🔍
Have Git in WSL use your Windows credentials
If you have Git set up on Windows, you can use your Git credentials stored in Windows to do Git operations in Linux. Run the command below in bash to set this up:
git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"
For more Git setup tips check out this tutorial on the WSL docs.
- More from this author:
- craigaloewen
- craigloewen-msft
- Website
Access copy and paste in WSL
You can get and set copy and paste output straight from WSL!
Set the clipboard from WSL output
You can use clip.exe
to set Windows’ clipboard from WSL. For example, this line sets the last few lines of your .bashrc file to be your clipboard.
tail ~/.bashrc | clip.exe
Get the clipboard values in WSL
You can use the PowerShell command Get-Clipboard
to get clipboard values from within WSL.
powershell.exe /c Get-Clipboard
- More from this author:
- craigaloewen
- craigloewen-msft
- Website
Access your Linux files from File Explorer
You can access your current folder from your WSL instance by running the following command:
powershell.exe /c start .
You will need to be on Windows 10 version 1903 or higher to access this feature. Check out this blog post for more info.
- More from this author:
- craigaloewen
- craigloewen-msft
- Website
Access a WSL 2 distribution from your LAN
When using a WSL 1 distribution, if your computer was set up to be accessed by your LAN, then applications run in WSL could be accessed on your LAN as well.
This isn’t the default case in WSL 2. WSL 2 has a virtualized ethernet adapter with its own unique IP address. Currently, to enable this workflow you will need to go through the same steps as you would for a regular virtual machine.
Here’s an example PowerShell command to add a port proxy that listens on port 4000 on the host and connects it to port 4000 to the WSL 2 VM with IP address 192.168.101.100.
netsh interface portproxy add v4tov4 listenport=4000 listenaddress=0.0.0.0 connectport=4000 connectaddress=192.168.101.100
More info can be found here on the WSL docs.
- More from this author:
- craigaloewen
- craigloewen-msft
- Website
Duplicate a WSL distro
You can duplicate a WSL distro by running wsl --export <filename>
in PowerShell to export your distro a tar file, and then you can run wsl --import <DistroName> <InstallLocation> <FileName>
to import it back to any location on your machine, under any new name that you like. To run this distro again, you can run wsl -d <DistroName>
or you can set it as your default with wsl -s <DistroName>
.
You will need to be on Windows 10 version 1903 or higher to access this feature. Check out this blog post for more info.
- More from this author:
- craigaloewen
- craigloewen-msft
- Website
Install a Distro to a different drive
First export your WSL distro a tar file using this command in PowerShell:
wsl --export <filename>
From there, using that file, import it back as a new distribution using this command in PowerShell:
wsl --import <DistroName> <InstallLocation> <FileName>
Make sure to choose an install location that’s under a new drive, and a useful distro name. Keep in mind that you won’t be able to access this distro using the classic distro launcher app anymore (as that is referencing the old distro stored on your C drive), you will instead need to run wsl -d <DistroName>
or you can set it as your default with wsl -s <DistroName>
and use wsl
to access this distro in the future.
You will need to be on Windows 10 version 1903 or higher to access this feature. Check out this blog post for more info.
- More from this author:
- craigaloewen
- craigloewen-msft
- Website
Run commands across WSL distros
You can pipe outputs from various distros and Windows as well to run commmands across WSL distros. For example, this command will take output from PowerShell, filter it in Debian, and then output it using cowsay in Ubuntu.
Write-Output "Hello from Windows`nHello from Linux!" | wsl -d Debian grep -i linux | wsl -d Ubuntu cowsay
- More from this author:
- craigaloewen
- craigloewen-msft
- Website
Use sudo without inputting a password
If you want to still be able to use sudo commands, but never want to input your password, then run this command in your shell.
echo "%${USER} ALL=(ALL) NOPASSWD:ALL" | sudo EDITOR='tee ' visudo --quiet --file=/etc/sudoers.d/passwordless-sudo
Keep in mind that this can have security complications, as now a user can get root access to your WSL distro using your user account.
- More from this author:
- craigaloewen
- craigloewen-msft
- Website
Piping inside WSL when using Powershell
When running a wsl.exe
command in Powershell, it can be needed/useful to pipe the output inside the WSL shell instead of Powershell (see Run commands across WSL distros
In order to achieve that, we need to use the wsl.exe --
instead of wsl.exe --exec
command as we need the WSL shell to be spawned (which --exec
doesn’t do).
Finally, we also need to “escape” the pipe character so it’s not interpreted by Powershell:
wsl.exe -- cat /etc/hosts `| grep localhost
Compress distribution backups
The wsl.exe –export command Exports the specified distribution to a tar file. But it doesn’t compress it due to it will take more time. If you are short of space or want to upload it to the cloud, it is better if it is compressed.
Suppose that you make this export in cmd:
wsl.exe --export WLinux %USERPROFILE%\mybackup.tar
After that you can compress the backup in cmd with:
wsl.exe gzip %USERPROFILE%\mybackup.tar
The above command results in a mybackup.tar.gz file. The funny thing is that you can use this file directly in the import command without decompressing it first:
mkdir C:\MyDistros\MyPengwin
wsl.exe --import MyPengwin C:\MyDistros\MyPengwin %USERPROFILE%\mybackup.tar.gz
Hope it helps
-Carlos
- More from this author:
- crramirezc
- crramirez
- Website