Linux中改变主机名的4种方法
昨天我们已经在我们的网站中写过如何在 Linux 中修改主机名的文章。今天,我们将向你展示使用不同的方法来修改主机名。你可以从中选取最适合你的方法。
使用 systemd
的系统自带一个名为 hostnamectl
的好用工具,它可以使我们能够轻易地管理系统的主机名。
当你使用这个原生命令时,它可以立刻改变主机名而无需重启来生效。
但假如你通过手动修改某个配置文件来更改主机名,那么就可能需要经过重启来生效。
在这篇文章中,我们将展示在使用 systemd
的系统中改变主机名的 4 种方法。
hostnamectl
命令允许在 Linux 中设置三类主机名,它们的细节如下:
- 静态: 这是静态主机名,由系统管理员添加。
- 瞬时/动态: 这个由 DHCP 或者 DNS 服务器在运行时赋予。
- 易读形式: 它可以由系统管理员赋予。这个是自由形式的主机名,以一种易读形式来表示服务器。
这些都可以使用下面 4 种方法来设置。
hostnamectl
命令:控制系统主机名。nmcli
命令:是一个控制 NetworkManager 的命令行工具。nmtui
命令:是一个控制 NetworkManager 的文本用户界面。/etc/hostname
文件:这个文件中包含系统的静态主机名。
方法 1:在 Linux 中使用 hostnamectl 来改变主机名
hostnamectl
可被用来查询和改变系统的主机名,以及相关设定。只需运行 hostnamectl
便可以查看系统的主机名了。
<span class="pln">$ </span><span class="kwd">hostnamectl</span>
或者使用下面的命令:
<span class="pln">$ </span><span class="kwd">hostnamectl</span><span class="pln"> status</span>
<span class="pln"> </span><span class="typ">Static</span><span class="pln"> </span><span class="kwd">hostname</span><span class="pun">:</span><span class="pln"> daygeek</span><span class="pun">-</span><span class="pln">Y700</span>
<span class="pln"> </span><span class="typ">Icon</span><span class="pln"> name</span><span class="pun">:</span><span class="pln"> computer</span><span class="pun">-</span><span class="pln">laptop</span>
<span class="pln"> </span><span class="typ">Chassis</span><span class="pun">:</span><span class="pln"> laptop</span>
<span class="pln"> </span><span class="typ">Machine</span><span class="pln"> ID</span><span class="pun">:</span><span class="pln"> </span><span class="lit">31bdeb7b83230a2025d43547368d75bc</span>
<span class="pln"> </span><span class="typ">Boot</span><span class="pln"> ID</span><span class="pun">:</span><span class="pln"> </span><span class="lit">267f264c448f000ea5aed47263c6de7f</span>
<span class="pln"> </span><span class="typ">Operating</span><span class="pln"> </span><span class="typ">System</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Manjaro</span><span class="pln"> </span><span class="typ">Linux</span>
<span class="pln"> </span><span class="typ">Kernel</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Linux</span><span class="pln"> </span><span class="lit">4.19</span><span class="pun">.</span><span class="lit">20</span><span class="pun">-</span><span class="lit">1</span><span class="pun">-</span><span class="pln">MANJARO</span>
<span class="pln"> </span><span class="typ">Architecture</span><span class="pun">:</span><span class="pln"> x86</span><span class="pun">-</span><span class="lit">64</span>
假如你想改变主机名,可以使用下面的命令格式:
语法格式:
<span class="pln">$ </span><span class="kwd">hostnamectl</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">-</span><span class="kwd">hostname</span><span class="pln"> </span><span class="pun">[</span><span class="pln">YOUR NEW HOSTNAME</span><span class="pun">]</span>
使用下面的命令来使用 hostnamectl
更改主机名。在这个例子中,我将把主机名从 daygeek-Y700
改为 magi-laptop
。
<span class="pln">$ </span><span class="kwd">hostnamectl</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">-</span><span class="kwd">hostname</span><span class="pln"> magi</span><span class="pun">-</span><span class="pln">laptop</span>
你可以使用下面的命令来查看更新后的主机名。
<span class="pln">$ </span><span class="kwd">hostnamectl</span>
<span class="pln"> </span><span class="typ">Static</span><span class="pln"> </span><span class="kwd">hostname</span><span class="pun">:</span><span class="pln"> magi</span><span class="pun">-</span><span class="pln">laptop</span>
<span class="pln"> </span><span class="typ">Icon</span><span class="pln"> name</span><span class="pun">:</span><span class="pln"> computer</span><span class="pun">-</span><span class="pln">laptop</span>
<span class="pln"> </span><span class="typ">Chassis</span><span class="pun">:</span><span class="pln"> laptop</span>
<span class="pln"> </span><span class="typ">Machine</span><span class="pln"> ID</span><span class="pun">:</span><span class="pln"> </span><span class="lit">31bdeb7b83230a2025d43547368d75bc</span>
<span class="pln"> </span><span class="typ">Boot</span><span class="pln"> ID</span><span class="pun">:</span><span class="pln"> </span><span class="lit">267f264c448f000ea5aed47263c6de7f</span>
<span class="pln"> </span><span class="typ">Operating</span><span class="pln"> </span><span class="typ">System</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Manjaro</span><span class="pln"> </span><span class="typ">Linux</span>
<span class="pln"> </span><span class="typ">Kernel</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Linux</span><span class="pln"> </span><span class="lit">4.19</span><span class="pun">.</span><span class="lit">20</span><span class="pun">-</span><span class="lit">1</span><span class="pun">-</span><span class="pln">MANJARO</span>
<span class="pln"> </span><span class="typ">Architecture</span><span class="pun">:</span><span class="pln"> x86</span><span class="pun">-</span><span class="lit">64</span>
方法 2:在 Linux 中使用 nmcli 命令来更改主机名
nmcli
是一个命令行工具,旨在控制 NetworkManager 并报告网络状态。
nmcli
被用来创建、展示、编辑、删除、激活和注销网络连接,同时还可以用来控制和展示网络设备的状态。另外,它也允许我们更改主机名。
使用下面的命令来利用 nmcli
查看当前的主机名。
<span class="pln">$ nmcli general </span><span class="kwd">hostname</span>
<span class="pln">daygeek</span><span class="pun">-</span><span class="pln">Y700</span>
语法格式:
<span class="pln">$ nmcli general </span><span class="kwd">hostname</span><span class="pln"> </span><span class="pun">[</span><span class="pln">YOUR NEW HOSTNAME</span><span class="pun">]</span>
使用下面的命令来借助 nmcli
命令可以更改主机名。在这个例子中,我将把主机名从 daygeek-Y700
变成 magi-laptop
。
<span class="pln">$ nmcli general </span><span class="kwd">hostname</span><span class="pln"> magi</span><span class="pun">-</span><span class="pln">laptop</span>
它可以在不重启下设备的情况下生效,但为了安全目的,只需要重启 systemd-hostnamed
服务来使得更改生效。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> restart </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">hostnamed</span>
再次运行相同的 nmcli
命令来检查更改后的主机名。
<span class="pln">$ nmcli general </span><span class="kwd">hostname</span>
<span class="pln">magi</span><span class="pun">-</span><span class="pln">laptop</span>
方法 3:在 Linux 中使用 nmtui 来更改主机名
nmtui
是一个基于 curses
库的 TUI 应用,被用来和 NetworkManager 交互。当启动 nmtui
后,如果没有指定 nmtui
的前面一个命令行参数,它将提醒用户选择执行某项活动。
在终端中运行下面的命令来开启文本用户界面。
<span class="pln">$ nmtui</span>
使用向下箭头按键来选择 “Set system hostname” 这个选项,然后敲击回车键。
下面的截图展示的是原来的主机名。
我们需要做的就是删除原来的主机名,再输入新的主机名,然后选中 “OK” 敲击回车确认就可以了。
然后它将在屏幕中向你展示更新后的主机名,再次选中 “OK” 敲击回车确认就完成更改了。
选中 “Quit” 按钮来从 nmtui
终端界面离开。
它可以在不重启设备的情况下生效,但为了安全目的,需要重启 systemd-hostnamed
服务来使得更改生效。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> restart </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">hostnamed</span>
你可以运行下面的命令来查看更新后的主机名。
<span class="pln">$ </span><span class="kwd">hostnamectl</span>
<span class="pln"> </span><span class="typ">Static</span><span class="pln"> </span><span class="kwd">hostname</span><span class="pun">:</span><span class="pln"> daygeek</span><span class="pun">-</span><span class="pln">Y700</span>
<span class="pln"> </span><span class="typ">Icon</span><span class="pln"> name</span><span class="pun">:</span><span class="pln"> computer</span><span class="pun">-</span><span class="pln">laptop</span>
<span class="pln"> </span><span class="typ">Chassis</span><span class="pun">:</span><span class="pln"> laptop</span>
<span class="pln"> </span><span class="typ">Machine</span><span class="pln"> ID</span><span class="pun">:</span><span class="pln"> </span><span class="lit">31bdeb7b83230a2025d43547368d75bc</span>
<span class="pln"> </span><span class="typ">Boot</span><span class="pln"> ID</span><span class="pun">:</span><span class="pln"> </span><span class="lit">267f264c448f000ea5aed47263c6de7f</span>
<span class="pln"> </span><span class="typ">Operating</span><span class="pln"> </span><span class="typ">System</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Manjaro</span><span class="pln"> </span><span class="typ">Linux</span>
<span class="pln"> </span><span class="typ">Kernel</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Linux</span><span class="pln"> </span><span class="lit">4.19</span><span class="pun">.</span><span class="lit">20</span><span class="pun">-</span><span class="lit">1</span><span class="pun">-</span><span class="pln">MANJARO</span>
<span class="pln"> </span><span class="typ">Architecture</span><span class="pun">:</span><span class="pln"> x86</span><span class="pun">-</span><span class="lit">64</span>
方法 4:在 Linux 中使用 /etc/hostname 来更改主机名
除了上面的方法外,我们还可以通过修改 /etc/hostname
文件来达到修改主机名的目的。但这个方法需要服务器重启才能生效。
使用下面的命令来检查 /etc/hostname
文件以查看当前的主机名:
<span class="pln">$ </span><span class="kwd">cat</span><span class="pln"> </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">hostname</span>
<span class="pln">daygeek</span><span class="pun">-</span><span class="pln">Y700</span>
要改变主机名,只需覆写这个文件就行了,因为这个文件只包含主机名这一项内容。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">echo</span><span class="pln"> </span><span class="str">"magi-daygeek"</span><span class="pln"> </span><span class="pun">></span><span class="pln"> </span><span class="str">/etc/</span><span class="kwd">hostname</span>
<span class="pln">$ </span><span class="kwd">cat</span><span class="pln"> </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">hostname</span>
<span class="pln">magi</span><span class="pun">-</span><span class="pln">daygeek</span>
然后使用下面的命令重启系统:
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">init</span><span class="pln"> </span><span class="lit">6</span>