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).