Install Arch Linux in WSL2 on a managed device

RMAG news

Acquiring the image

First, you need to download and install a bootstrapped Arch Linux image.

Open a Windows PowerShell and download a bootstrapped tarball from a suitable Arch Linux mirror:

Invoke-WebRequest -Uri “https://archlinux.mirror.pcextreme.nl/iso/latest/archlinux-bootstrap-x86_64.tar.gz” -OutFile “$HOMEDownloadsarchlinux-bootstrap-x86_64.tar.gz”

Extract and repackage the bootstrap image

Extract and repackage the downloaded bootstrap image in another Linux distro, e.g. WSL Ubuntu.

# Go to the user’s Downloads folder
cd /mnt/c/Users/$USER/Downloads/

# Move the downloaded file to the Linux home folder
mv archlinux-bootstrap-x86_64.tar.gz ~

# Go to your home folder
cd

# Extract the tar.gz bootstap file
tar -zxvf archlinux-bootstrap-x86_64.tar.gz

# Go to the following subdirectory
cd root.x86_64

# Repackage the extracted tar.gz (there is a dot at the end)
tar -zcvf archlinux-bootstrap_repackage-x86_64.tar.gz .

# Put the file back to your Windows Downloads folder
mv archlinux-bootstrap_repackage-x86_64.tar.gz /mnt/c/Users/$USER/Downloads/

Pick an installation location

Create a directory in you home folder labeled “wsl”. Make sure that each distro has their own location.

md “$HOMELinux DistrosArchLinux”

Import the tarball and update the system

Import the tarball requires an installation location with no other distro, or otherwise it will fail.

wsl –import Arch “$HOMELinux DistrosArchLinux” “$HOMEDownloadsarchlinux-bootstrap_repackage-x86_64.tar.gz”

When importing the tarball is complete, run

wsl -d Arch

You will be greeted with a root user prompt #.

Switch to the home directory with cd.

Enable DNS resolving

Now you will need a working internet connection. In my case, DNS was not resolving from within WSL. If that’s the case for you as well do the following:

cat > /etc/wsl.conf<< EOF
[network]
generateResolvConf = false
EOF

And use Google’s DNS:

cat > /etc/resolv.conf<< EOF
nameserver 8.8.8.8
nameserver 1.1.1.1
EOF

Make pacman work

sed -i ‘s:#Server:Server:g’ /etc/pacman.d/mirrorlist

Initialize the keyring and populate it

pacman-key –init && pacman-key –populate archlinux

Update packages

Let’s update the system first now that we should have a working network:

pacman -Syu

Use reflector to find the fastest mirrors

# First install reflector
pacman -Sy reflector

# Then use reflector to find the fastest mirror for your location (“Netherlands” in my case).
reflector –country “Netherlands” –age 12 –protocol https –sort rate –save /etc/pacman.d/mirrorlist

Installing some required packages

pacman -Sy base-devel git vim nano wget sudo which go openssh man-db bash-completion fontconfig ntp

Setting up passwords and one user with sudo privileges

export EDITOR=vim; visudo

Uncomment the line containing %wheel ALL=(ALL) ALL; do the same to the next line with %wheel if you want to run sudo tasks without having to provide your password.

root password

Set the root user password with passwd before setting up the login user account.

passwd

Creating a user and setting a password

Now create a user and set a password.

useradd -m -G wheel -s /bin/bash -d /home/rubenjs rubenjs; passwd rubenjs

Setting up wsl.conf

Because Arch Linux was imported, by default the login user is the root user even with a user account. To prevent this, a config file needs to be created to tell wsl which user to log in. Create the file /etc/wsl.conf and copy the following, replacing ‘rubenjs’ with your own username.

Settings for wsl.conf

[automount]
enabled = true
options = “metadata,uid=1000,gid=1000,umask=22,fmask=11,case=off”
mountFsTab = true
crossDistro = true

[network]
generateHosts = true # When using with systemd, otherwise `false`.
generateResolvConf = true

[interop]
enabled = true
appendWindowsPath = true

[user]
default = rubenjs

[boot]
systemd=true

Finishing the setup process

If you use Windows Terminal, the best way is to close it, and then reopen it. A new profile for Arch has been added.

Check if systemd is working

sudo systemctl status

If systemd is functional, you should see a green indication, and you can create your own systemctl scripts, but before that, let’s first enable some services.

sudo systemctl enable –now dbus.service ntpd.service sshd.service

Installing and aur helper such as yay

git clone https://aur.archlinux.org/yay-bin
cd yay-bin
makepkg -si –noconfirm

Adjust locale

sudo vim /etc/locale.gen

Uncomment the locales you use on your instance. For me, I uncomment nl_NL.UTF-8 UTF-8 and en_US.UTF-8 UTF-8.

Then generate the locales

sudo locale-gen

sudo su

echo “LANG=en_US.UTF-8
LANGUAGE=en_US
LC_CTYPE=nl_NL.UTF-8
LC_NUMERIC=nl_NL.UTF-8
LC_TIME=nl_NL.UTF-8
LC_COLLATE=nl_NL.UTF-8
LC_MONETARY=nl_NL.UTF-8
LC_MESSAGES=en_US.UTF-8
LC_PAPER=nl_NL.UTF-8
LC_NAME=nl_NL.UTF-8
LC_ADDRESS=nl_NL.UTF-8
LC_TELEPHONE=nl_NL.UTF-8
LC_MEASUREMENT=nl_NL.UTF-8
LC_IDENTIFICATION=nl_NL.UTF-8
LC_ALL=
> /etc/locale.conf
Optional: Enabling multilib
linenumber=$(grep -nr \#\[multilib\]” /etc/pacman.conf | gawk ‘{print $1}’ FS=“:”)
sed -i ${linenumber}s:.*:[multilib]:” /etc/pacman.conf
linenumber=$((linenumber+1))
sed -i ${linenumber}s:.*:Include = /etc/pacman.d/mirrorlist:” /etc/pacman.conf

Leave a Reply

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