在 Linux 操作系统中,主机名是分配给连接到网络的设备(例如计算机或服务器)的名称。主机名用于标识网络上的设备,通常是字母和数字的组合。在这种情况下,我们可以为每台机器分配一个唯一的主机名,以便轻松记住它们。主机名还允许每台计算机使用设备名称而不是 IP 地址进行通信。
在 Rocky Linux 9 上更改主机名
第 1 步。第一步是将系统更新到最新版本的软件包列表。为此,请运行以下命令:
<span class="pln">sudo dnf check</span><span class="pun">-</span><span class="pln">update sudo dnf install dnf</span><span class="pun">-</span><span class="pln">utils</span>
第 2 步。检查您当前的主机名。
在设置主机名之前,检查系统的当前主机名非常重要。您可以使用以下命令进行检查:
<span class="pln">hostnamectl</span>
输出:
<span class="typ">Static</span><span class="pln"> hostname</span><span class="pun">:</span><span class="pln"> meilana </span><span class="typ">Icon</span><span class="pln"> name</span><span class="pun">:</span><span class="pln"> laptopQ </span><span class="typ">Chassis</span><span class="pun">:</span><span class="pln"> laptop </span><span class="typ">Machine</span><span class="pln"> ID</span><span class="pun">:</span><span class="pln"> e46efe3a7cg0detz9fb521a2021b2e </span><span class="typ">Boot</span><span class="pln"> ID</span><span class="pun">:</span><span class="pln"> ef56510okb9cadd81nikah99419 </span><span class="typ">Operating</span> <span class="typ">System</span><span class="pun">:</span> <span class="typ">Rocky</span> <span class="typ">Linux</span> <span class="lit">9</span> <span class="typ">Kernel</span><span class="pun">:</span> <span class="typ">Linux</span> <span class="lit">5.6</span><span class="pun">.</span><span class="lit">0</span><span class="pun">-</span><span class="lit">07</span><span class="pun">-</span><span class="kwd">generic</span> <span class="typ">Architecture</span><span class="pun">:</span><span class="pln"> x86</span><span class="pun">-</span><span class="lit">64</span>
第 3 步。更改系统主机名。
要更改主机名,您可以使用后跟要使用的新主机名的选项。例如:set-hostname
<span class="pln">sudo hostnamectl </span><span class="kwd">set</span><span class="pun">-</span><span class="pln">hostname idroot</span><span class="pun">-</span><span class="pln">machine</span>
运行上述命令后,您可以使用以下命令确认新主机名:
<span class="pln">hostnamectl</span>
输出:
<span class="typ">Static</span><span class="pln"> hostname</span><span class="pun">:</span><span class="pln"> idroot</span><span class="pun">-</span><span class="pln">machine </span><span class="typ">Icon</span><span class="pln"> name</span><span class="pun">:</span><span class="pln"> laptopQ </span><span class="typ">Chassis</span><span class="pun">:</span><span class="pln"> laptop </span><span class="typ">Machine</span><span class="pln"> ID</span><span class="pun">:</span><span class="pln"> e46efe3a7cg0detz9fb521a2021b2e </span><span class="typ">Boot</span><span class="pln"> ID</span><span class="pun">:</span><span class="pln"> ef56510okb9cadd81nikah99419 </span><span class="typ">Operating</span> <span class="typ">System</span><span class="pun">:</span> <span class="typ">Rocky</span> <span class="typ">Linux</span> <span class="lit">9</span> <span class="typ">Kernel</span><span class="pun">:</span> <span class="typ">Linux</span> <span class="lit">5.6</span><span class="pun">.</span><span class="lit">0</span><span class="pun">-</span><span class="lit">07</span><span class="pun">-</span><span class="kwd">generic</span> <span class="typ">Architecture</span><span class="pun">:</span><span class="pln"> x86</span><span class="pun">-</span><span class="lit">64</span>
请记住,主机名是系统范围的设置,因此您可能需要注销并重新登录才能使更改生效。您可能还需要更新引用旧主机名的任何配置文件或脚本。
选择一个反映计算机用途的描述性和唯一主机名也是一个好主意。这样可以更轻松地在网络或日志中识别计算机,并有助于防止与网络上的其他设备发生冲突。
感谢您使用本教程更改 Rocky Linux 9 系统上的主机名。如需其他帮助或有用信息,我们建议您查看 Rocky Linux 官方网站。