Everyone knows that in the past, we only need to modify
/etc/hostname
the name in this file to modify the hostname of the Linux system, but modifying the file in WSL will not take effect.
To modify the Hostname and make it effective, we must modify /etc/wsl.conf
the file .
edit configuration file
sudo vi /etc/wsl.conf
Change parameters
Change hostname =
the following LAPTOP-40AMMKE8
to the name you want
[network]
hostname = LAPTOP-40AMMKE8
generateHosts = false
For example, if we change to ubuntu22, save and exit
[network]
hostname = ubuntu22
generateHosts = false
Restart WSL
- exit WSL
exit
- close WSL
wsl --shutdown
- Start WSL
wsl
So far the renaming operation has been completed.
Handle follow-up questions
But when you use sudo
command escalation to execute the command, the following error message appears
sudo: 无法解析主机:ubuntu22: 未知的名称或服务
We need to edit /etc/hosts
the file to add the previously modified hostname
sudo vi /etc/hosts
add a record127.0.0.1 名称
It is normal to use sudo
the command .