microsoft / WSL

Issues found on WSL
https://docs.microsoft.com/windows/wsl
MIT License
17.53k stars 823 forks source link

hostname for bash prompt #7108

Open suntong opened 3 years ago

suntong commented 3 years ago

I noticed changing my hostname doesn't affect bash prompt under WSL, and I'm wondering if that can be changed --

I'ved changed:

sudo hostname wsl

I also changed /etc/hostname to "wsl" and changed /etc/hosts as well.

But my hostname in bash prompt never changed.

As per bash prompt user manual

\h
The hostname, up to the first ‘.’.

So I think, for normal case, it is read from either /etc/hostname or /etc/hosts.

Describe the solution you'd like

Any of the above changing of my hostname method can affect bash prompt.

For reason behind that, "I have 6 different wsl instances for different purposes. Most of the times I can't figure out which instance I'm currently in."

Describe alternatives you've considered

I know the official solution is to change the host system's name from System --> Change Settings --> Change, but I'd like to identify my WSL distribution in different sessions, e.g. UbuntuA, UbuntuB and UbuntuC.

Additional context

I know "WSL doesn't allow you to set a hostname separately from the computer".
and I know from #125 that "Changing the hostname from Bash is currently not supported"

but if you do a search on "WSL Change hostname", you can see that it is a common request, meaning most people have the same desire as this.

Please consider. thx!

therealkenc commented 3 years ago

Ask would be for the WSL instances to have their own UTS namespace with a /etc/wsl.conf entry. But this wouldn't work in the current WSL model. Whether the hostname is different or not, all WSL distribution instances share the same NET namespace, by-design. Having the same IP address with different hostnames, while in principle possible, would be a mess. Effectively dupe #4711 if WSL had a different net namespace per distro instance (but it does not).

suntong commented 3 years ago

Hmm... suppose different WSL hostnames is allowed, would that have any external impact?

I was thinking that the WSL hostname is just an internal thing, internal in the sense it matters only to the WSL Linux system itself. Never thought the hostname would have any external impact. For e.g., I think it is impossible to address to WSL using the hostname in any way, as there might be many different WSL running just using the same hostname and same IP.

This is all just me thinking, as I know next to nothing about WSL internals. Comments?

PS. For ages, Linux chroot environment must have the same hostname as the host, but it is no longer the case anymore nowadays.