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.
system has not been booted with systemd as init system (pid 1)
is normal for WSL1 - it uses a Microsoft specificinit
. The/etc/hosts
file should get autogenerated (unless you modified/etc/wsl.conf
as well?). – steeldriver May 23 '22 at 20:29/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