Skip to content

Instantly share code, notes, and snippets.

@ThYpHo0n
Last active April 11, 2024 19:00
Show Gist options
  • Star 45 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save ThYpHo0n/349f1f6473e207b866f65aca4728da3e to your computer and use it in GitHub Desktop.
Save ThYpHo0n/349f1f6473e207b866f65aca4728da3e to your computer and use it in GitHub Desktop.
WSL(2) bash profile helpers
# WSL?
if [[ "$(< /proc/sys/kernel/osrelease)" == *microsoft* ]]; then
export $(dbus-launch)
export LIBGL_ALWAYS_INDIRECT=1
export WSL_VERSION=$(wsl.exe -l -v | grep -a '[*]' | sed 's/[^0-9]*//g')
export WSL_HOST=$(tail -1 /etc/resolv.conf | cut -d' ' -f2)
export DISPLAY=$WSL_HOST:0
# pip path if using --user
export PATH=$PATH:$HOME/.local/bin
# SSH
eval $(/mnt/c/weasel-pageant/weasel-pageant -r)
fi
@xiaodaigh
Copy link

doesn't seem to work

@bglopez
Copy link

bglopez commented Aug 4, 2020

doesn't seem to work

@xiaodaigh
Are you using VcXsrv or something else? These instructions didn't work for me with MobaXterm -- I ended up needing to grab the IP address of the Windows machine itself on my local network and exporting that IP as the DISPLAY var. Check my gist for an example of how to grab that, it is untested on any machine but my local one so there may be issues: https://gist.github.com/bglopez/e5a472236ad1e305da93473db9eb123b

@ionling
Copy link

ionling commented Sep 14, 2020

@bglopez Your gist is 404.

@ThYpHo0n
Copy link
Author

Hey I updated the gist with my latest version I use at the moment. It works on WSL2 with ArchLinux. You can find the latest version in my dotfiles: https://github.com/ThYpHo0n/dotfiles/blob/master/zsh/.zshrc#L57

@elig0n
Copy link

elig0n commented Jan 18, 2021

my /etc/resolv.conf doesn't have the host address, what are other ways to get it ?

@ThYpHo0n
Copy link
Author

my /etc/resolv.conf doesn't have the host address, what are other ways to get it ?

@elig0n I guess you are using some different unix derivate or WSL is for some reason not able to write to its /etc/resolv.conf Which one are you using? We are also talking about WSL2 right?

@elig0n
Copy link

elig0n commented Jan 18, 2021

my /etc/resolv.conf doesn't have the host address, what are other ways to get it ?

@elig0n I guess you are using some different unix derivate or WSL is for some reason not able to write to its /etc/resolv.conf Which one are you using? We are also talking about WSL2 right?

WSL2 Ubuntu. I can write to resolv.conf. I maybe disabled its default writing there, not sure. Still looking for alternative way

@ionling
Copy link

ionling commented Jan 19, 2021

@elig0n Did you edit /etc/wsl.conf? You maybe set generateResolvConf to false.
See Configure per distro launch settings with wslconf.

@elig0n
Copy link

elig0n commented Jan 19, 2021

@ionling yes I did. I would still rather handle my own resolv.conf , is there another way to get the IP ?

@ionling
Copy link

ionling commented Feb 12, 2021

@elig0n Use ipconfig.exe to get vEthernet (WSL) address. Thats what you want.

@jamesharris-garmin
Copy link

Is there a resaonable way to determine which IP address we should use? i.e. some way to test available IP addresses to detect if the xserver is accessable?

@Digiover
Copy link

Digiover commented Mar 29, 2021

@jamesharris-

Is there a resaonable way to determine which IP address we should use? i.e. some way to test available IP addresses to detect if the xserver is accessable?

It may depend on how you installed WSL/WSL2 and perhaps your network layout. This works for me, because my Windows computer and my WSL2 VM both use an external DNS server:

$ cat /etc/resolv.conf
nameserver 192.168.46.1

export DISPLAY=$(ipconfig.exe | grep IPv4 | head -1 | rev | awk '{print $1}' | rev | tr -d '\r'):0.0

Use set -x for debugging purposes. This way I found that ipconfig.exe prints an carriage return, which needs to be removed with tr -d '\r'

$ export foo=$(ipconfig.exe | grep IPv4 | head -1 | rev | awk '{print $1}' | rev):0.0
++ ipconfig.exe
++ grep --color=auto IPv4
++ head -1
++ rev
++ awk '{print $1}'
++ rev
+ export $'foo=192.168.46.151\r:0.0'
+ foo=$'192.168.46.151\r:0.0'
$ export foo=$(ipconfig.exe | grep IPv4 | head -1 | rev | awk '{print $1}' | rev | tr -d '\r'):0.0
++ ipconfig.exe
++ grep --color=auto IPv4
++ head -1
++ rev
++ awk '{print $1}'
++ rev
++ tr -d '\r'
+ export foo=192.168.46.151:0.0
+ foo=192.168.46.151:0.0

Disable debug set +x

Edit: if the above fails, use

export DISPLAY=$(ip -4 route | awk '{ if ($1~/default/)  print $3 }'):0.0

@boypt
Copy link

boypt commented Apr 6, 2021

if /etc/resolve.conf can't get what you want, iproute2 util is another proper way to get the gateway address:

 ip -o route get 1.1.1.1 | cut -d' ' -f 3

the 1.1.1.1 can be any address of foreign network, it just show how kernel visit that address.

@crkolimago
Copy link

export DISPLAY=$(ipconfig.exe | grep IPv4 | head -1 | rev | awk '{print $1}' | rev | tr -d '\r'):0.0

I used this approach but added a grep to extract just the Wi-Fi adapter because the IPv4 part was just fetching the first match so:

export DISPLAY=$(ipconfig.exe | grep -A 6 'Wireless LAN adapter Wi-Fi' | grep IPv4 | head -1 | rev | awk '{print $1}' | rev | tr -d '\r'):0.0

@smerschjohann
Copy link

smerschjohann commented Nov 3, 2021

I came up with this: HOST_IP=$(route.exe print | grep "0.0.0.0 0.0.0.0" | awk '{print $4}')

I changed the routing inside my wsl as a workaround for my VPN software. So there is no way to get the Windows external IP on the linux side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment