0

So I'm a newbie with WSL and Ubuntu 22.04.

I saw someone with a prompt with the username plus a cool name for the host (username@hostname...). So I tried to do the same, but I ended up erasing everything that was in the hosts file. I tried:

sudo nano /etc/hosts

Inside, I saw something like:

127.1.1.0 localhost
127.1.1.0 hostname.something # I changed this one
# and some additional lines

After I erased that and pressed Ctrl+X to get out, the whole hosts folder is completely empty.

So I said, "Ok don't panic. Maybe just reboot everything?"

sudo reboot

... and these errors appeared:

system has not been booted with systemd as init system (pid 1). can't operate. 
failed to connect to bus: host is down, fail to talk to init daemon

So then I decided to try reinstalling, but I still have the same problem.

I don't know how to reset it so the hosts file is completely back to normal. How can I get everything inside hosts back to where it was, because right now it's just an empty file.

NotTheDr01ds
  • 17,888
  • 2
    system has not been booted with systemd as init system (pid 1) is normal for WSL1 - it uses a Microsoft specific init. The /etc/hosts file should get autogenerated (unless you modified /etc/wsl.conf as well?). – steeldriver May 23 '22 at 20:29
  • 1
    Welcome to Ask Ubuntu. I've done some extensive editing on your question based on what I think you meant. Please review it and let us know (in a reply here) if it is accurate now. If not, please edit the question to correct anything I have wrong. Thanks! – NotTheDr01ds May 23 '22 at 22:01
  • Also a heads-up that /etc/hosts is a file, not a folder. In your latest edit, you continue to call it a folder, so I'm not sure if you really mean /etc/hosts. – NotTheDr01ds May 23 '22 at 22:46

1 Answers1

4

You seem to have two different questions, with perhaps another two implied questions:

  • How to recover your /etc/hosts file.
  • How to reboot Ubuntu on WSL.
  • How to change the hostname.
  • How to change the prompt (if I'm understanding your question correctly)

Rebooting Ubuntu on WSL

First, it's important to note that Ubuntu under WSL behaves quite differently in many ways from Ubuntu on a virtual or physical machine. One of those areas is that it does not use Systemd. Which is why you get that particular error message when attempting to use the reboot command (which maps to a Systemd command in Ubuntu).

Another difference is that it has no concept of "power", "on/off", "reboot", etc. It's not a virtual machine, but a namespace/container running in the WSL2 virtual machine (that we can't access).

In general, if you just exit the shell and give it a few seconds, WSL will detect that nothing is running in Ubuntu (if that's the case) and shut it down automatically. You can check this in PowerShell with:

wsl -l -v

If it's showing as "Running", then you can manually stop it with (also from PowerShell):

wsl --terminate <distro>

... with <distro> being the name that showed up in wsl -l -v (probably "Ubuntu-22.04" for you).

In some cases, you'll want to stop the entire WSL2 VM. This can be done with:

wsl --shutdown

This will, of course, not only stop the WSL VM, but all running instances in it (and WSL1 instances as well).

Regenerating /etc/hosts

Just reboot (restart Ubuntu from a "Stopped" state). As @Steeldriver mentioned in the comments, that file is automatically created by WSL on startup. The only exception should be if you've manually disabled that functionality through /etc/wsl.conf settings, which seems unlikely in your case.

Changing the hostname

My recommendation -- Don't. WSL automatically creates the hostname in WSL from your Windows Computer Name. If you really want to change it, change the Computer Name in Windows (and reboot Windows).

Changing it to be something different will just (most likely) cause other things to not work properly in the future. By the time you run across a problem, you'll have forgotten that it was probably caused by the difference in hostname.

Changing the bash prompt

It's far, far easier to just change the prompt itself, rather than the hostname. You can put whatever you want in the prompt -- It doesn't have to be the computed username@hostname pair.

Try something like (warning, I am not a "cool prompt" designer):

LIGHTRED="\[\033[1;31m\]"
RED="\[\033[0;31m\]"
WHITE="\[\033[0;37m\]"
PS1="${RED}rivas@${WHITE}my${LIGHTRED}W${WHITE}S${LIGHTRED}L${RESET}> "

You can then put that in your ~/.bashrc (but don't erase it! ) to have it automatically run for each shell invocation.

There are plenty of custom prompt functions out there to work from if you'd like.

NotTheDr01ds
  • 17,888