abdusco / abdus.dev

Personal website
https://abdus.dev
1 stars 0 forks source link

posts/fixing-wsl2-localhost-access-issue/ #35

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Fixing WSL2 localhost access issue - abdus.dev

A fix for when WSL localhost redirection stops working

https://abdus.dev/posts/fixing-wsl2-localhost-access-issue/

RubenSibon commented 3 years ago

Thank you Abdussamet. This was helpful!

Xetera commented 3 years ago

Just want to add it might be a better idea to use a domain like wsl.local because many oauth providers and different types of tooling freak out when they can't see a TLD in a host name. Also, if you're working on something like next.js where the client (outside of wsl) and server (inside wsl) share the same host definition, it's a good idea to also edit /etc/hosts with

127.0.0.1 wsl.local

to have it point back to localhost so server side requests don't get confused.

Extremely useful post otherwise, thanks a lot it saved me a bunch of headache.

66Antigravity commented 3 years ago

Wonderful and very well explained. Thank you very much!

killshot13 commented 3 years ago

Very very useful!

@abdusco your brilliance with this script is much appreciated. Sağol!

@Xetera your point was also valuable and I implemented it seamlessly.

Rather than having a subdomain, though, I opted to use a bit of a different version.

http://wslhost

Then I edited /etc/hosts with

127.0.0.1        wslhost

It satisfies the concern about authentication issues but keeps a naming convention patterned from localhost So far at least, this has been holding true on my end. :)

Cheers!

isidoroi commented 3 years ago

I have an error when I run this script.

Eccezione durante la chiamata di "Match" con "1" argomento/i: "Il valore non può essere null. Nome parametro: input" In C:\Users\utente\Desktop\wsl-network.ps1:6 car:1

abdusco commented 3 years ago

@isidoroi What's the output of:

wsl -- ip -4 addr show eth0

Which distro are you running?

If you're using a distro other than Ubuntu you might have to tweak the command and the regex pattern to extract the IP address.

isidoroi commented 3 years ago

Distro Debian 10 output of wsl -- ip -4 addr show eth0 is null PS: ifconfig on distro it works and I connect to localhost correctly

abdusco commented 3 years ago

@isidoroi you should be able to replace ip addr ... command with

ifconfig eth0

It should output the ip address of eth0 like inet 10.11.12.13

isidoroi commented 3 years ago

Thanks and sorry for the delay. I have partially solved. First I had to set the default distro wsl -s Debian and then I used the command wsl hostname -I but it displays two ip, where the first is that of the eth0 of the distro. I run the script and I have no errors, but I don't think it still works correctly

PS: Excuse my English

Ilardo Isidoro cell. 3407967622

Il giorno dom 23 mag 2021 alle ore 13:01 Abdussamet Koçak < @.***> ha scritto:

@isidoroi https://github.com/isidoroi you should be able to replace ip addr ... command with

ifconfig eth0

It should output the ip address of eth0 like inet 10.11.12.13

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/abdusco/abdus.dev/issues/35#issuecomment-846544031, or unsubscribe https://github.com/notifications/unsubscribe-auth/APLRMF23ATI73VCBIBJWRKTTPDOA3ANCNFSM4Z5LHPLA .

abdusco commented 3 years ago

@isidoroi Windows sets up an interface (usually eth0) to share localhost address space. We're piggybacking that by aliasing the IP address of that interface to a hostname (wsl). For this solution to work, you should be able to ping your WSL instance. Make sure you can get (using ifconfig / ip what have you) and ping the IP address of eth0. Then you can use that as the command and everything else should just work.

If you post the commands you use and their outputs / errors, it might be easier to figure what's actually going on.

luisdev commented 3 years ago

Thanks. Great article.

At the end the instruction says: "Run the task and check the hosts file to see if WSL IP address is added."

Which hosts file is this referring to? Where is that hosts file created?

abdusco commented 3 years ago

@luisdev Thanks. It refers to %windir%/system32/etc/hosts (i.e. c:\windows\system32\etc\hosts file). It has always been there, the script just adds an alias to WSL IP in it.

thangnqs commented 3 years ago

It works! Thanks a tons :)

jeanatpi commented 2 years ago

@abdusco you saved my life or at least make it easier!!

On my side I've also added as @killshot13 mentioned:

127.0.0.1 wsl.local

Also changed /etc/wsl.conf to:

[network]
generateHosts = false

to keep this change.

For now it's working well with the following stack:

abdusco commented 2 years ago

@jeanatpi Glad to be of help :)

It caused me a lot of headache on Windows 10. Not sure if this is fixed on Windows 11, but I'd wager it is not. I cannot / don't even know how to replicate it consistently. It just breaks, sometimes.

NurjanovUlugbek commented 2 years ago

I can't use this ps1 script can you help me please?

I run with admin option Getting error... Move-Item : Cannot create a file when that file already exists. At C:\wslhost\wsl.ps1:36 char:5

phi-friday commented 2 years ago

Can I use reblog this post on my blog?

micmalti commented 1 year ago

Thank you so much. After wasting two days trying to figure out what was wrong with my port forwarding configuration, I followed your advice and managed to get remote access to a Jupyter notebook.

Tim-Gabrikowski commented 11 months ago

You are my Hero!!!!!