A collection of commands I find useful for running Windows Subsystem Linux.
This is best done from PowerShell.
wsl --shutdown
Then simply log back in from the terminal.
Windows 10 v1903 includes a built-in WSL export/import command. Specifically, wsl --export
, which produces a tar
file and wsl --import
, which imports a previously exported tar
file. You can then move this file between computers.
For example:
wsl --export Ubuntu Ubuntu-BU_2025-01-30.tar
The "tarball" file can be opened with 7-Zip and you should see the entire Linux file structure.
See https://docs.microsoft.com/en-us/windows/wsl/use-custom-distro for more information.
You can also specify stdin/stdout with -
, which should allow you to directly transfer to another machine and import using a remote shell like ssh
.
This doesn't work the way you expect, that is, restore the files to the original location (under user/AppData/Packages
). Instead a disk image file is created under <InstallLocation>
, for example D:\wslDistroStorage\Ubuntu-20.04\ext4.vhdx
. This is probably better as it's now easier to back up your Linux installation. Note that <DistroName>
must not already exist.
wsl --import <DistroName> <InstallLocation> <InstallTarFile>
Ideally these would start when you first start WSL and can be set up in /etc/wsl.conf
under the [boot]
section. More information https://docs.microsoft.com/en-us/windows/wsl/wsl-config#configure-settings-with-wslconfig-and-wslconf
If not installed this will be need to be done.
Update your system first:
sudo apt update
Next, install Apache:
sudo apt install apache2
Next, start the server with the following command:
sudo /etc/init.d/apache2 start
Go to localhost/
and you should see the Apache default page.
The following are useful for managing the Apache service.
sudo /etc/init.d/apache2 start sudo /etc/init.d/apache2 stop sudo /etc/init.d/apache2 restart
Likewise for Apache, if MySQL is not present on your system install it with the command below:
sudo apt install mysql-server
An alternative to MySQL is to install MariaDB, which is an open-source drop in replacement for MySQL:
sudo apt update
sudo apt install mariadb-server
sudo mysql_secure_installation
sudo /etc/init.d/mysql start sudo /etc/init.d/mysql restart sudo /etc/init.d/mysql stop
To use your WSL installation as a web and database server having these start on boot is much easier than using the manual methods above.
You can now execute an arbitrary command line when starting an instance by creating/editing /etc/wsl.conf
(via sudo) with the following:
[boot]
system=true
command="service apache2 start; service mysql start"
Note that multiple commands must be done on one line separated by semicolons as shown. If multiple lines are used only the last one is executed.
See https://learn.microsoft.com/en-us/windows/wsl/wsl-config for more information.
On WSL with Windows 10, you'll need to start the service via one of your user's shell startup scripts. If you have WSL 2 installed the Windows 11 method may work.
Use the following syntax in your ~/.bash_profile
:
wsl.exe -u root service ssh status || wsl.exe -u root service ssh start
wsl.exe -u root
has the advantage of not requiring the sudo
password when starting up every time. From PowerShell and CMD, it can be called without the exe
, but from within WSL it does require the extension.
Of course, you can also use sudoers
to suppress the requirement for the password, but WSL just makes this unnecessary.
Note that this will generate one or two messages every time you start. To suppress this, use syntax such as:
wsl.exe -u root service ssh status > /dev/null || wsl.exe -u root service ssh start > /dev/null
(From https://superuser.com/questions/1701853/how-to-enable-a-service-to-start-with-wsl2, viewed 30-Jan-2025.)
An excellent way to manage the database is to use PHPmyadmin. It can be installed with:
sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl
If user web directories are required, that is http://localhost/~username is accessible, do the following:
$ ln -s /etc/apache2/mods-available/userdir.conf /etc/apache2/mods-enabled/userdir.conf $ ln -s /etc/apache2/mods-avaliable/userdir.load /etc/apache2/mods-enabled/userdir.load
If you run into the problem with not enough upload file size change the following line in /etc/php/7.4/apache2/php.ini
. Search for the setting first and change it, otherwise add a new line to the file.
upload_max_filesize = 100M
To find the IP address, enter the following:
ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | cut -d/ -f1
This is an excellent way to set up WordPress websites using a domain. Some plug-ins, like Oxygen Builder, have problems working with a URL and a subfolder.
To access the local server, use the following:
http://[::1]/
To use the actual IP address determine the address using:
ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | cut -d/ -f1
Then use this as https://[ip-address]
.
In the files of /etc/hosts
and /Windows/System32/drivers/etc/hosts
. Add the following:
127.0.0.1 example.test
::1 example.test localhost
From https://github.com/microsoft/WSL/issues/5728, viewed 28-Oct-2022.
I didn't much success with this and it may be more trouble than it's worth. See https://ubuntu.com/server/docs/service-domain-name-service-dns on how to do it.
Windows Terminal may have overridden some of Midnight Commander's hot keys, like Alt+Enter for example, which copies the current file to the command line. To change it go to Settings/Actions
and search for Alt+Enter. Change it to another key combination.
First copy the keymap to your own user area:
cp /etc/mc/mc.keymap ~/.config/mc/
https://superuser.com/questions/461452/is-there-a-way-to-change-shortcuts-in-midnight-commander
Action | Description | Keys |
---|---|---|
Command screen go full size | Ctrl-o | |
Copy current file to command line | Alt-Enter | |
Directory Hotlist | Midnight Commander can store a list of frequently visited directories. | Ctrl-\ |
A very useful editor which has a steep learning curve.
Action | Keys |
---|---|
Over write mode | Shift + R |
Replace current single character and then return to command mode. | r (just press the "r" key) |
![]() | doc/filePanels/hotkeys – Midnight CommanderCollection of hot keys when using Midnight Commander. |
![]() | How to Access Your Linux (WSL) Files in Windows 10Windows 10’s May 2019 Update introduced an easy, safe, and officially supported way to access and work with your Linux files from within File Explorer and other applications. Here’s how to get at your Windows Subsystem for Linux (WSL) files. |
![]() | How to create symbolic links in UbuntuA symbolic link is a shortcut file for any directory or file. The symlink or soft link are the other names of the symbolic link. In Ubuntu, symbolic links work like a string that generates paths between various files and directories. |
![]() | How To Install and Secure phpMyAdmin on Ubuntu 20.04 | DigitalOcean |
![]() | How to Install phpMyAdmin on Windows WSL with Apache - Website for Students |
![]() | How to Install WordPress on Windows WSL - Geek RewindThis brief tutorial shows students and inexperienced users how to install WordPress on Windows 10 WSL (Windows Subsystem for Linux) 2 with Nginx HTTP server on Ubuntu 20.04 | 18.04. |
![]() | Importing & Exporting WSLImport any Linux distribution to use with WSL | Microsoft Docs |
![]() | Linux Commands - A practical referenceThis is a linux command line reference for common operations. |
![]() | Rebooting WSLHow to Reboot WSL (Windows subsystem Linux) in Windows 10 or 11 - H2S Media |
![]() | Tutorials | DigitalOceanUseful tutorials for working with computer systems. |
![]() | WSL NewsWhat’s new for WSL in Windows 10 version 1903? - Windows Command Line |
Featured photo by Sora Shimazaki from Pexels
You must be logged in to post a comment.