Skip to content

Instantly share code, notes, and snippets.

@haakemon
Last active August 16, 2023 14:24
Show Gist options
  • Select an option

  • Save haakemon/31028cd0b479ce8546c7809f6f0a7400 to your computer and use it in GitHub Desktop.

Select an option

Save haakemon/31028cd0b479ce8546c7809f6f0a7400 to your computer and use it in GitHub Desktop.

Use these scripts to quickly bootstrap new WSL instances. The scripts can be executed with this command:

Setup wsl.conf

  • bash <(curl -sL https://gist.github.com/haakemon/31028cd0b479ce8546c7809f6f0a7400/raw/wsl-init.sh)

appendWindowsPath is set to false, to avoid issues where certain tools (often npm) gets executed with the Windows instance instead of the WSL instance.

Setup ssh keys

  • bash <(curl -sL https://gist.github.com/haakemon/31028cd0b479ce8546c7809f6f0a7400/raw/ssh-keys-init.sh)
touch ~/.ssh/id_ed25519--git
touch ~/.ssh/id_ed25519--git.pub
touch ~/.ssh/allowed_signers
chmod 600 ~/.ssh/*
ssh-keygen -t ed25519 -a 100
#!/usr/bin/env bash
if [ -e "/etc/wsl.conf" ]; then
echo "/etc/wsl.conf already exists."
echo "Creating backup at /etc/wsl.conf.pre-wsl-init."
sudo cp /etc/wsl.conf /etc/wsl.conf.pre-wsl-init
fi
winhostname=$(/mnt/c/Windows/System32/HOSTNAME.EXE)
# Trim leading and trailing whitespace from the output
winhostname="${winhostname#"${winhostname%%[![:space:]]*}"}"
winhostname="${winhostname%"${winhostname##*[![:space:]]}"}"
if [ -z "$winhostname" ]; then
read -p "Could not automatically resolve Windows hostname. Please enter current Windows hostname (case sensitive): " winhostname
fi
read -p "Enter default WSL username (the user needs to already exist): " username
read -p "Enter desired WSL hostname: " hostname
sudo tee -a /etc/wsl.conf > /dev/null <<EOF
[boot]
systemd=true
[user]
default=${username}
[network]
generateHosts=false
hostname=${hostname}
[interop]
appendWindowsPath=false
EOF
sudo cp /etc/hosts /etc/hosts.pre-wsl-init
sudo sed -i "s/${winhostname}/${hostname}/g" /etc/hosts
echo "You should now restart the wsl instance (wsl --terminate INSTANCE_NAME)."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment