当前位置: 首页 > news >正文

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

在Windows 安装的Ubuntu,如何修改主机名。有列了两种方法,提供给大家参照。
在这里插入图片描述

文章目录

    • 方法一:hostname指令修改
    • 方法二:修改配置文件修改hostnanme
    • wsl.conf 文件配置选项
    • 推荐阅读

方法一:hostname指令修改

hostname指令修改hostname:

sudo hostname test

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

hostname

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

mirror@Y2040001-PC:~$ hostname
test

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

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

sudo vi /etc/wsl.conf
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
sudo vi /etc/hosts

红框处修改为新的hostname:
在这里插入图片描述

# 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查看子系统是否仍然还在运行。

PS C:\Users\Administrator> wsl --list --running

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

wsl -t Ubuntu

或者

wsl --shutdown

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

mirror@Ubuntu22:~$ hostname
Ubuntu22
mirror@Ubuntu22:~$

wsl.conf 文件配置选项

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

# 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

推荐阅读

  • Ubuntu挂载NFS(Network File System) ,怎么解决权限不一致的问题?
  • Ubuntu镜像源cn.arichinve.ubuntu.com不可用原因分析和解决
  • Windows 11安装Ubuntu 实战教程
  • PowerShell无人参与安装最新版本SQL Server Management Studio (SSMS)
  • VRRP(虚拟路由器冗余协议)标准协议工作机制与优势介绍
http://www.lryc.cn/news/246062.html

相关文章:

  • 如何使用内网穿透将Tomcat网页发布到公共互联网上【内网穿透】
  • 网络入门---网络的大致了解
  • 构建沉浸式 AI 文本编辑器:开源 3B 编辑器的设计原则与思路
  • 【从删库到跑路 | MySQL总结篇】表的增删查改(进阶上)
  • [每周一更]-(第74期):Docker-compose 部署Jenkins容器-英文版及错误纠错
  • MySQL日期函数sysdate()与now()的区别,获取当前时间,日期相关函数
  • 邦芒解析:面试怎么谈自身优缺点
  • 【libGDX】加载G3DJ模型
  • 0基础学习VR全景平台篇第123篇:VR视频航拍补天 - PR软件教程
  • webpack打包三方库直接在html里面使用
  • Redis使用increment方法返回null的原因以及解决方案
  • springMVC,什么是Spring MVC? Spring MVC的主要组件? springMVC工作原理/流程 MVC框架
  • 【论文阅读】TACAN:控制器局域网中通过隐蔽通道的发送器认证
  • C语言第三十五弹---打印九九乘法表
  • 线性代数的艺术
  • 基于注解配置的AOP
  • 【Qt】QStackedWidget、QRadioButton、QPushButton及布局实现程序首页自动展示功能
  • 探索 V8 引擎的内部:深入理解 JavaScript 执行的本质
  • 单片机学习11——矩阵键盘
  • Java游戏 王者荣耀
  • 接口测试场景:怎么实现登录之后,需要进行昵称修改?
  • 石油化工专业MR仿真情景教学演练
  • Docker配置Halo搭建个人博客-快速入门
  • 禁止编辑的PPT幻灯片,如何有效保护文件安全?
  • 优化前端性能
  • 1.1 C语言之入门:使用Visual Studio Community 2022运行hello world
  • Android NDK开发中常用的gradle配置
  • 【AUTOSAR-DoIP】通过 DoIP 进行符合 Autosar 的车辆诊断
  • GPIO的使用--操作PE02 PE03 PE04实现开关控制灯泡亮灭
  • 【算法萌新闯力扣】:回文链表