#
Ubuntu setup
#
Installation tips
When prompted to update to the new installer, do so.
Ubuntu Server (minimized) works best, but once it's finished you need to install extra stuff not usually found in server distros, including basic ones such as
bash-completion
,nano
,ping
and similar. It's not a big deal though.When prompted, disable LVM. It's rarely used anyways. Even more so for this use-case.
When prompted, enable OpenSSH server. This will make our life easier later.
#
Post-installation steps
Once you've installed Ubuntu, there are some steps we need to follow to make our own WSL2 install work correctly. Such steps are outlined here.
Important note
We will be using "WSL2"
as the argument for controlvm
. This is the name we set up when creating the VM in the "VM Setup" section.
As for natpf1
, this assumes we're altering the first NIC in the VM and that such NIC is the one attached to NAT. Same goes for the TCP22
rule to enable OpenSSH that we'll need to remove once we've set up automatic port forwarding.
#
Enable OpenSSH to login remotely
Warning
Make sure to open the command prompt as administrator only if you set the VirtualBox apps to run as administrator as well.
Open a command prompt and run this to enable port forwarding from localhost:22
to the VM at port 22, so we can connect to it with ssh localhost
:
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm "WSL2" natpf1 "TCP22,tcp,,22,,22"
In the same command prompt, run ssh user@localhost
to connect to your VM (replace user
with the username you set in the Ubuntu install step, of course)
Important
If you get an error that says REMOTE HOST IDENTIFICATION HAS CHANGED!
, you can delete the known_hosts
file running this: del %USERPROFILE%\.ssh\known_hosts
. It's annoying sometimes.
#
Add SSH key to enable automatic login
Important
This step is required if you want the automatic port forward feature!
To enable automatic login, you need an SSH key. This can easily be done using Putty. Download and then install it, then follow these steps:
- Run PuTTY Key Generator (puttygen.exe)
- Click Generate
- Save all your keys (usually a good idea. Setting a passphrase isn't recommended)
- Click Save public key
- Click Save private key
- Click Conversions menu
- Click Export OpenSSH key. Save it as
user.key
or something. We'll need it later. - In the box below the text "Public key for pasting into OpenSSH authorized_keys file", there should be a bunch of text starting with
ssh-rsa ...
. In that box, do this:- Right-click
- Select all
- Right-click
- Copy
- Paste and save this somewhere in a text file. You'll need it later.
Note
The reason we're not using passphrases and we're enabling automatic login is because this will make our life easier when we run the automatic port forwarding script and when we use VSCode or other tools. Just like WSL2 when you open a WSL2 terminal it and it "automatically" logs in to your user.
Now that you're there, run this to enable your current user to use the SSH key we generated. Of course, replace LONG_KEY_GOES_HERE
with the value you got from the PuTTY Key Generator:
echo "ssh-rsa LONG_KEY_GOES_HERE" > ~/.ssh/authorized_keys
#
Configure the new host in your SSH config
This step is optional, but it's nice to have. If you don't have it already, create a file as %USERPROFILE%\.ssh\config
and add the following contents:
Host *
AddressFamily inet
ServerAliveInterval 10
StrictHostKeyChecking no
Host wsl2
HostName localhost
User user
PasswordAuthentication false
IdentityFile "C:\Users\User\.ssh\keys\user.key"
Of course, replace user
with your username and "C:\Users\User\.ssh\keys\user.key"
with the full path of the user.key
file we saved in PuTTY Key Generator, step 5.
Success!
You can easily log in from any terminal just by typing: ssh wsl2
.
#
Set up apt-get and update your distro
In this last step, you'll configure apt-get to be a little bit faster, and get your distro up-to-date. This will make our life easier later:
sudo apt update
cat <<EOF > 99parallel
APT::Acquire::Queue-Mode "access";
APT::Acquire::Retries 3;
EOF
sudo mv 99parallel /etc/apt/apt.conf.d/99parallel
sudo apt update
sudo apt dist-upgrade -y
#
Disable IPv6
For some reason, Ubuntu 21.10 is a little bit slow waiting for network to be configured even if we already have IPv4 configured. We can speed up this process by editing /etc/default/grub
and changing the following lines with this content:
GRUB_CMDLINE_LINUX_DEFAULT="ipv6.disable=1"
GRUB_CMDLINE_LINUX="ipv6.disable=1"
Then run sudo update-grub
and reboot the VM.
#
Install VirtualBox Guest Additions
In the VirtualBox window, click Devices -> Install Guest Additions CD Image... , then do this:
sudo apt install -y build-essential
sudo mkdir /media/cdrom
sudo mount -t iso9660 /dev/cdrom /media/cdrom
cd /media/cdrom
sudo ./VBoxLinuxAdditions.run
Then reboot the VM.