Score:2

how to set/change a static IP in "Ubuntu on Windows" (WSL)

la flag

I have installed ubuntu on my PC (Windows) but I can not set an IP...

It seems there is a default IP(eth0) and we are not able to change it...

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: bond0: <BROADCAST,MULTICAST,MASTER> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether ae:64:fb:7d:e3:fd brd ff:ff:ff:ff:ff:ff
3: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether be:68:d7:7d:53:4c brd ff:ff:ff:ff:ff:ff
4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:ae:d2:4d brd ff:ff:ff:ff:ff:ff
    inet 172.23.218.213/20 brd 172.23.223.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:feae:d24d/64 scope link
       valid_lft forever preferred_lft forever

I can not find the below file (00-installer-config.yaml)

testing_01@LMARTINEZ4-7A33:/etc/netplan$ ls -l
total 0 
* Ubuntu on Windows
    
Welcome to Ubuntu 20.04 LTS (GNU/Linux 5.10.16.3-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
    
  System information as of Tue Oct 11 11:39:44 CDT 2022
Terrance avatar
id flag
You might want to see: https://github.com/microsoft/WSL/issues/4150
NotTheDr01ds avatar
vn flag
@Terrance While I think I know which part of that (currently) 628-comment issue you are referring to, that's a bit of a "needle-in-a-haystack" for most people, I would say ;-)
NotTheDr01ds avatar
vn flag
@LuisAguilar What's the actual *problem* you are trying to solve by setting a static IP address? There may be better solutions in WSL. Thanks!
Luis Aguilar avatar
la flag
Hi @NotTheDr01ds thanks for the reply, Basically I just wondering how to change the IP using WSL, it seems it is totally different in normal UBUNTU distribution :(... I have looking WSL forum but there is a lot of info and is not clear, just want to change that IP range on eth0, this one is by default 172.23.x.x ( vEthernet WSL) I will try updating it on the virtual network.
Terrance avatar
id flag
@NotTheDr01ds I didn't want to try tackling this one yet. I know how to change both IPs since the one is tied to the physical ethernet card while the other being virtual is changed via the `netsh` command in Windows. You are correct though in asking for a better understanding in what OP is looking for exactly is the right way to go. Pretty good answer you wrote below. :)
NotTheDr01ds avatar
vn flag
@LuisAguilar Oh, no worries - That wasn't a criticism, really. And apologies if it came across that way -- The wink emoticon was meant say that I was only half-serious. That *is* a good Github issue to read and understand; it's just grown *way* too long over the years IMHO. There are some real gems hidden in there -- IIRC both the SSH suggestion in my answer as well as the Bridged-mode Preview are both covered in there, if one can sort through all the "cruft" to find it.
Terrance avatar
id flag
@NotTheDr01ds I think you intended that for me and not Luis. :D It is all good. I think what you wrote was good and I definitely saw your wink emoji. I am glad that you answered this one the way that you did, as I might have gone a different direction on my answer and made things worse. :D
NotTheDr01ds avatar
vn flag
@Terrance Yes, absolutely meant for you! Thanks for catching that!
Score:3
vn flag

I'm going to answer primarily from your comment:

just wondering how to change the IP using WSL; it seems it is totally different in normal Ubuntu distribution

Yes, it's quite different from a normal Ubuntu distribution. WSL is actually responsible for setting up the networking in Ubuntu in this scenario. This is similar to how Ubuntu works when running in Docker (or any other container-based system) since WSL2 really is a container-based system itself (see this answer for details).

This must be the case since you can run multiple distributions/instances at the same time. Each distribution is running in the same managed VM, and each shares the same IP address (and kernel, etc.). See this answer for some more info on that.

If you do change the IP in one instance/distribution, note that it changes for all the others that are currently running, until such time as the VM itself shuts down.

Typically, I recommend leaving the networking alone unless you have a specific use-case you need to handle. The CentOS question was one good reason, IMHO.

One common question is how to access a service running in WSL2 from another computer on the network. A static IP could make this easier, but I find it even easier to use a SSH tunnel back to the Windows host. That way (if you really need to), you can set the IP of the Windows host to be static and connect.

Note that from the Windows host itself, you should typically be able to access any service running in WSL by simply using localhost. WSL does auto-forwarding of traffic from the Windows host to the WSL2 VM.

So how can you set a static IP on WSL? Well, only manually really. You can manually add a second address to the virtual eth0, but there's just no "normal Ubuntu" way of bringing that up that I know of. See this Super User answer for details. I'm not sure I'd call that a "static IP", technically.

You can also, if you are using Windows 11 Pro (or higher), install a recent release of WSL from the Microsoft Store (currently listed as Windows Subsystem for Linux Preview) and follow the directions here to configure a new virtual WSL2/Hyper-V switch that is bridged to Windows. Although I haven't tested it, this should allow you to ultimately use the same IP address for Ubuntu/WSL2 as you do for Windows. If you set a static IP in Windows, that should ultimately be the same as setting a static IP in WSL (at least from the routing/forwarding perspective).

Luis Aguilar avatar
la flag
Thanks a lot! this is really good info :)
Ronald Johnson avatar
mx flag
GREAT followup. It answers or clears up anything said abive and even answers the OP's original question. So why do I like it so much? Because I personally had a wsl issue and it was this post that solved it, and it too was a purely network based issue. What I want is true bridging and that will be fixed shortly which not only solved my issue, but bridging can and will solve every issue brought about in this entire thread. Sure there are many ways to do things, but it's best to do it the right way. Good Job.
NotTheDr01ds avatar
vn flag
@RonaldJohnson Thanks! It sounds like you are saying that the last paragraph here is what worked for you, right? That's good info, if so - I wasn't sure if that feature was still enabled. Since I posted that answer, the beta has been released (also for Windows 10), but that feature still hasn't been publicly announced in release notes or documented by Microsoft. I was starting to wonder if it was even still enabled!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.