Windows11 WSL subsystem ubuntu22.04 modify hostname

Everyone knows that in the past, we only need to modify
/etc/hostnamethe name in this file to modify the hostname of the Linux system, but modifying the file in WSL will not take effect.

insert image description here

To modify the Hostname and make it effective, we must modify /etc/wsl.confthe file .

edit configuration file

sudo vi /etc/wsl.conf

Change parameters

Change hostname =the following LAPTOP-40AMMKE8to 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.
insert image description here

Handle follow-up questions

But when you use sudocommand escalation to execute the command, the following error message appears

sudo: 无法解析主机:ubuntu22: 未知的名称或服务

insert image description here
We need to edit /etc/hoststhe file to add the previously modified hostname

sudo vi /etc/hosts

add a record127.0.0.1 名称

insert image description here
It is normal to use sudothe command .

insert image description here

Guess you like

Origin blog.csdn.net/no1xium/article/details/131305104