Skip to content

Instantly share code, notes, and snippets.

@mrsombre
Last active April 2, 2021 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrsombre/66f330efd542e2237ea86324c3b285bb to your computer and use it in GitHub Desktop.
Save mrsombre/66f330efd542e2237ea86324c3b285bb to your computer and use it in GitHub Desktop.
WSL 2 Scripts and Hacks

A set of Hacks and scripts for WSL2 Ubuntu distro

# if running bash
if [ -n "$BASH_VERSION" ]; then
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
# WSL hacks
export WINIP=$($HOME/bin/wslhost)
export WSLIP=$($HOME/bin/wslip)
export DISPLAY="${WINIP}:0.0"
export LIBGL_ALWAYS_INDIRECT=1
. $HOME/bin/wsldns
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi
# reset port forwarding rules
netsh interface portproxy reset
#!/usr/bin/env bash
# change wsl to use openvp dns
PATH=$PATH:/mnt/c/Windows/System32/WindowsPowerShell/v1.0
OPENVPN=$(powershell.exe -Command 'Get-DnsClientServerAddress -AddressFamily ipv4' | grep OpenVPN | grep -P '\{.+\}')
BACKUP=/run/resolv.conf
if [[ ! -z $OPENVPN ]]; then
if [[ ! -f /etc/resolv.conf || ! $(grep -iF openvpn /etc/resolv.conf) ]]; then
if [[ -f /etc/resolv.conf ]]; then
sudo cp -f /etc/resolv.conf $BACKUP
fi
echo '# This file was automatically generated using OpenVPN DNS' | sudo tee /etc/resolv.conf &> /dev/null
powershell.exe -Command "Get-DnsClientServerAddress -AddressFamily ipv4 | Select-Object -ExpandProperty ServerAddresses" \
| grep -P '[\d\.]+' | sed 's/^/nameserver /' | sed $'s/\r$//' | sudo tee -a /etc/resolv.conf &> /dev/null
echo "Enabled OpenVPN DNS"
fi
else
if [[ -f /etc/resolv.conf && $(grep -iF openvpn /etc/resolv.conf) ]]; then
sudo rm -f /etc/resolv.conf
if [[ ! -f $BACKUP ]]; then
echo "DNS backup missing, reload required"
exit 1
fi
sudo mv -f $BACKUP /etc/resolv.conf
echo "Enabled WSL DNS"
fi
if [[ -f $BACKUP ]]; then
sudo rm -f $BACKUP
fi
fi
#!/usr/bin/env bash
# return windows host ip address
route -n | grep ^0.0.0.0 | awk '{print $2}' | tr -d "\n"
#!/usr/bin/env bash
# return wsl instance ip address
ip route get 8.8.8.8 | awk '{print $7}' | tr -d "\n"
# proxy port 9003 from host to wsl instance
netsh interface portproxy add v4tov4 listenport=9003 connectaddress=$(wsl /home/user/bin/wslip) connectport=9003
netsh interface portproxy show all
Read-Host -Prompt "Press Enter to continue"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment