30-Jan-2025

WSL Commands and Notes

A collection of commands I find useful for running Windows Subsystem Linux.

Managing WSL Linux

Reboot Linux

This is best done from PowerShell.

wsl --shutdown

Then simply log back in from the terminal.

Backing Up your Linux on Windows Installation

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.

Restoring Linux

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>

HTML Hosting Services

Ideally these would start when you first start WSL and can be set up in /etc/wsl.confunder the [boot] section. More information https://docs.microsoft.com/en-us/windows/wsl/wsl-config#configure-settings-with-wslconfig-and-wslconf

Installing Apache

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.

Starting Apache web server:

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

Installing MySQL

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

Managing MySQL server:

sudo /etc/init.d/mysql start
sudo /etc/init.d/mysql restart
sudo /etc/init.d/mysql stop

Starting Services Automatically on Boot

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.

For Windows 11

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.

For Windows 10

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

PHPmyadmin

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

User Web Directories

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

Not Enough Upload File Size Error

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

See also https://phoenixnap.com/kb/fix-the-uploaded-file-exceeds-the-upload-max-filesize-directive-in-php-ini-wordpress.

Find WSL's IP address

To find the IP address, enter the following:

ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | cut -d/ -f1

Point a Domain to WSL

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

Using the Hosts File

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.

Using Bind

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.

Midnight Commander

Hot Keys

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

ActionDescriptionKeys
Command screen go full sizeCtrl-o
Copy current file to command lineAlt-Enter
Directory Hotlist
Midnight Commander can store a list of frequently visited directories.Ctrl-\

VIM

A very useful editor which has a steep learning curve.

ActionKeys
Over write modeShift + R
Replace current single character and then return to command mode. r (just press the "r" key)

More Information

Featured photo by Sora Shimazaki from Pexels

Leave a Reply

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram