如果您需要知道Ubuntu计算机的名称,这篇文章将向您展示几种查找方法。我们还将向您展示如何轻松更改您的主机名。在Ubuntu 17.x中,有一个设备名和一个主机名,看起来是不一样的。设备名称显示在GUI中,主机名显示在终端(Terminal)中。我会告诉你如何改变两者。
查找 Ubuntu 主机名
您的主机名可以在多个地方找到。我们首先要看的是终端(Terminal)窗口。要打开终端(Terminal)窗口,请选择Accessories | Terminal应用程序(Applications)菜单中的终端。在较新版本的Ubuntu中,例如Ubuntu 17.x,您需要单击活动(Activities),然后输入终端(terminal)。
您的主机名显示在您的用户名和终端(Terminal)窗口标题栏中的“ @在我们的例子中,我们的主机名是“ ubuntu-virtual ”。它也显示在提示本身中。
这可能看起来多余,因为主机名已经显示在标题栏和提示符中,但是您也可以输入以下命令来查看主机名。
$ hostname
更改 Ubuntu 主机名
/etc目录中有一个主机名(hostname)文件,其中包含您的主机名。以前,这是更改主机名的唯一方法。幸运的是,还有一种更简单的方法。更改Ubuntu主机名的最简单方法是直接转到Settings。
根据您的版本,它是屏幕右上角的齿轮图标或新底座中的齿轮图标。如果齿轮图标位于右上角,请单击About this Computer。
如果您必须打开设置(Settings),请一直向下滚动到显示详细信息(Details)的位置,然后单击它。
当您单击详细信息时,它应该会自动将您带到关于(About)屏幕。您将看到一个名为Device name( Device name)的可编辑文本框。只需(Just)输入您想要的新主机名,然后按Enter。
值得注意的是,此方法将永久更改主机名,这意味着您可以重新启动,新名称将保留。
编辑主机名和主机文件
更改主机名的另一种方法是打开终端(Terminal)窗口并输入以下命令:
sudo hostname new-name
这将更改主机名,但它会在重新启动后回到 /etc/hostname 文件中的任何内容。如果您需要永久更改它,您可以编辑我之前谈到的主机名文件。为此,请打开终端并输入以下命令:
sudo vi /etc/hostname
该文件应该只有第一行中的主机名。在 vi 中,您可以通过按字母 i进入插入模式。(Insert)要删除字符,请按 x 键。您也可以按字母 a 进入追加(Append)模式。请注意,要使用 x 删除字母,您必须先按ESC退出插入(Insert)或追加(Append)模式。
在那里更改名称后,您还需要在另一个位置进行更改。使用以下命令:
sudo vi /etc/hosts
更改以 127.0.0.1 开头的第二行。
在这两个位置更改它后,您可以重新启动,新名称将保留。值得注意的是,设置(Settings)中的设备名称仍会显示其他内容。因此,设备名称和主机名称之间似乎存在差异。我不确定有什么区别以及为什么它没有在“关于(About)”页面中更新,但这就是它对我有用的方式。享受!
Find and Change Your Hostname in Ubuntu
If yоu need to know the name of your Ubuntu computer, this post shоws you several ways to find it. We will wіll also show yoυ how to easily change your hоst name. In Ubuntυ 17.x, there is a device name and a host name, which seem to be different. The device name shows up in the GUI and the host name shows up in Terminal. I’ll show you how to change both.
Find Ubuntu Host Name
Your host name can be found in several places. The first place we will look is the Terminal window. To open the Terminal window, select Accessories | Terminal from the Applications menu. In newer versions of Ubuntu, like Ubuntu 17.x, you need to click on Activities and then type in terminal.
Your host name displays after your username and the “@” symbol in the title bar of the Terminal window. In our case, our host name is “ubuntu-virtual.” It also displays in the prompt itself.
It may seem redundant, as the host name already displays in the title bar and the prompt, but you can also enter the following command to view the host name.
$ hostname
Change Ubuntu Hostname
There is a hostname file in the /etc directory that contains your hostname. Previously, it was the only way to change the host name. Luckily, there is a different way that is easier. The easiest way to change your Ubuntu hostname is to simply go to Settings.
Depending on your version, it’s the gear icon at the top right of the screen or the gear icon in the new dock. If the gear icon is in the top-right corner, click on About this Computer.
If you had to open Settings, scroll all the way down to where it says Details and click on that.
When you click on Details, it should bring you to the About screen automatically. You will see an editable text box called Device name. Just type the new host name you would like and press Enter.
It’s worth noting that this method will change the host name permanently, meaning you can restart and the new name will remain.
Edit Hostname and Hosts Files
Another way to change the host name is to open a Terminal window and type in the following command:
sudo hostname new-name
This will change the host name, but it will go back to whatever is in the /etc/hostname file after a restart. If you need to change it permanently, you can edit the hostname file I talked about earlier. To do that, open terminal and type in the following command:
sudo vi /etc/hostname
The file should have nothing but the hostname in the first line. In vi, you can go into Insert mode by pressing the letter i. To delete characters, you press the x key. You can also press the letter a to go to Append mode. Note that to delete letters using x, you have to press ESC first to get out of Insert or Append mode first.
Once you change the name there, you also need to change it in one other location. Use the following command:
sudo vi /etc/hosts
Change the second line that starts with 127.0.0.1.
Once you change it in both of these locations, you can restart and the new name will remain. It’s worth noting that the device name in Settings will still show something else. So there seems to be a difference between the device name and the host name. I’m not sure what the difference is and why it doesn’t update in the About page, but that’s how it worked for me. Enjoy!