在Windows WSL (Linux的Windows子系统)上运行的Ubuntu如何更改主机名

在Windows 安装的Ubuntu,如何修改主机名。有列了两种方法,提供给大家参照。

文章目录

方法一:hostname指令修改

hostname指令修改hostname:

bash 复制代码
sudo hostname test

修改后,需要reboot,我们可以通过执行特定指令查看是否有修改:

bash 复制代码
hostname

可以看到hostname已经暂时修改,但

bash 复制代码
mirror@Y2040001-PC:~$ hostname
test

方法二:修改配置文件修改hostnanme

打开 /etc/wsl.conf wsl.conf这个文件,如果这个文件不存在,则新建:

bash 复制代码
sudo vi /etc/wsl.conf
bash 复制代码
hostname = Ubuntu22
generateHosts = false
  • hostname = Ubuntu22将更新/etc/hostname中的主机名
  • generateHosts = false 将阻止WSL自动生成/etc/hosts文件。否则,在重新启动Ubuntu时,hosts文件更改将被覆盖,会出现报错:sudo: unable to resolve host test: Name or service not known
bash 复制代码
sudo vi /etc/hosts

红框处修改为新的hostname:

bash 复制代码
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateHosts = false
127.0.0.1       localhost
127.0.1.1       Ubuntu22.localdomain    Ubuntu22                                                                                                                                                                                             127.0.0.1       localhost

修改完成后,保存并退出。关闭子系统或者重新运行子系统,

修改不会立即生效,关闭子系统后必须等待~8秒。必须等待8秒的原因,官方解释如下:

If you launch a distribution (ie. Ubuntu), modify the wsl.conf file, close the distribution, and then re-launch it. You might assume that your changes to the wsl.conf file have immediately gone into effect. This is not currently the case as the subsystem could still be running. You must wait ~8 seconds for the subsystem to stop before relaunching in order to give enough time for your changes to be picked up.

可以在power shell中执行wsl --list --running查看子系统是否仍然还在运行。

bash 复制代码
PS C:\Users\Administrator> wsl --list --running

如果想要立即停止,可以执行如下:

bash 复制代码
wsl -t Ubuntu

或者

bash 复制代码
wsl --shutdown

重新开机子系统,验证hostname修改成功。

bash 复制代码
mirror@Ubuntu22:~$ hostname
Ubuntu22
mirror@Ubuntu22:~$

wsl.conf 文件配置选项

wsl.conf 文件,有一些配置选项,大家可以根据自己需要参考:

bash 复制代码
# Automatically mount Windows drive when the distribution is launched
[automount]

# Set to true will automount fixed drives (C:/ or D:/) with DrvFs under the root directory set above. Set to false means drives won't be mounted automatically, but need to be mounted manually or with fstab.
enabled = true

# Sets the directory where fixed drives will be automatically mounted. This example changes the mount location, so your C-drive would be /c, rather than the default /mnt/c. 
root = /

# DrvFs-specific options can be specified.  
options = "metadata,uid=1003,gid=1003,umask=077,fmask=11,case=off"

# Sets the `/etc/fstab` file to be processed when a WSL distribution is launched.
mountFsTab = true

# Network host settings that enable the DNS server used by WSL 2. This example changes the hostname, sets generateHosts to false, preventing WSL from the default behavior of auto-generating /etc/hosts, and sets generateResolvConf to false, preventing WSL from auto-generating /etc/resolv.conf, so that you can create your own (ie. nameserver 1.1.1.1).
[network]
hostname = DemoHost
generateHosts = false
generateResolvConf = false

# Set whether WSL supports interop processes like launching Windows apps and adding path variables. Setting these to false will block the launch of Windows processes and block adding $PATH environment variables.
[interop]
enabled = false
appendWindowsPath = false

# Set the user when launching a distribution with WSL.
[user]
default = DemoUser

# Set a command to run when a new WSL instance launches. This example starts the Docker container service.
[boot]
command = service docker start

推荐阅读

相关推荐
南林yan10 分钟前
Linux:命令行参数
linux
小鹿( ﹡ˆoˆ﹡ )15 分钟前
Excel数据清洗工具:提高数据处理效率的利器
linux·运维·excel
人世间的烟火29 分钟前
linux更换阿里镜像源
linux·运维·服务器
陈小也~43 分钟前
嵌入式-QT学习-小练习
linux·c++·qt
鹏大师运维1 小时前
【信创】麒麟KOS上安装使用网络抓包工具Wireshark
linux·网络·测试工具·wireshark·国产化·麒麟·kylinos2403
码农研究僧1 小时前
详细分析linux中的MySql跳过密码验证以及Bug(图文)
linux·mysql·bug·密码验证
CHHC18802 小时前
NetCore DynamicExpresso 动态表达式使用例子
windows
像风一样自由20202 小时前
Ubuntu系统入门指南:常用命令详解
linux·运维·ubuntu
shall_zhao2 小时前
安装OpenResty(Linux-Docker)
linux·docker·openresty
bits/stdc++.h2 小时前
Linux升级OpenSSH9.8版本步骤
linux·运维·服务器·ssh