如何在Ubuntu Linux上更改主机名(计算机名)

ubuntu-linux-change-hostname-or-computer-name-shown-in-terminal

Ubuntu asks you to choose a name for your computer — known as a “hostname” — when you install it. You can change this hostname later, but Ubuntu doesn’t provide a graphical interface for doing so.

Ubuntu要求您在安装计算机时为计算机选择一个名称,即“主机名”。 您可以稍后更改此主机名,但Ubuntu不会提供图形界面。

The process here will also work on Linux Mint and other Debian-based Linux distributions. Other Linux distributions — for example, Fedora and other Red Hat-based distributions — use different methods for specifying a hostname.

这里的过程也将在Linux Mint和其他基于Debian的Linux发行版上工作 。 其他Linux发行版(例如Fedora和其他基于Red Hat的发行版)使用不同的方法来指定主机名。

选择一个主机名 (Choosing a Hostname)

A hostname is a name assigned to a “host” — a computer on a network. The hostname is basically just your computer’s name. It’s used to identify your computer on the network. It’s also shown very visibly in the terminal.

主机名是分配给“主机”(网络上的计算机)的名称。 主机名基本上只是您计算机的名称。 它用于识别网络上的计算机。 在终端中也非常明显地显示了它。

You can change the hostname to anything that you want. However, you shouldn’t use the same hostname on two different computers on a network, as this can cause problems. The hostname is supposed to uniquely identify a computer on a network.

您可以将主机名更改为所需的任何名称。 但是,不应在网络上的两台不同计算机上使用相同的主机名,因为这可能会导致问题。 主机名应该用来唯一标识网络上的计算机。

Don’t get too fancy with your hostname.  Host names can only contain letters (a through z), digits (0 through 9), and the hyphen character ( – ), and the period character ( . ). A hostname must begin and end with a letter or number — not a hyphen or period. Letters are also case-insensitive, so “COMPUTER” is equivalent to “computer.” The hostname must be between 2 and 63 characters long, although you’ll probably find shorter hostnames more convenient.

不要太看重您的主机名。 主机名只能包含字母(a到z),数字(0到9),连字符(-)和点号(。)。 主机名必须以字母或数字开头和结尾,而不是连字符或句号。 字母也不区分大小写,因此“计算机”等同于“计算机”。 主机名必须在2到63个字符之间,尽管您可能会发现较短的主机名更方便。

ubuntu-change-hostname-or-computer's-name

编辑您的/ etc / hostname文件 (Edit Your /etc/hostname File)

Open a terminal window to get started. In Ubuntu’s Unity desktop, click the Ubuntu button to pull up the dash, search for Terminal, and press Enter.

打开终端窗口以开始使用。 在Ubuntu的Unity桌面中,单击Ubuntu按钮以拉起破折号,搜索Terminal ,然后按Enter。

open-terminal-on-ubuntu-linux

You’ll need to edit your /etc/hostname file, which is where Ubuntu and other Debian-based distributions store the hostname. Run the following command in the terminal to open the /etc/hostname file in the graphical “gedit” text editor for editing:

您需要编辑/ etc / hostname文件,该文件是Ubuntu和其他基于Debian的发行版存储主机名的位置。 在终端中运行以下命令,以在图形“ gedit”文本编辑器中打开/ etc / hostname文件进行编辑:

sudo gedit /etc/hostname

sudo gedit / etc /主机名

(You could also use terminal-based text editors like nano or vi for this — you’d run the sudo nano /etc/hostname or sudo vi /etc/hostname commands to open the file for editing. Consult our guide to using nano or introduction to using vi for help using these text editors.)

(您也可以为此使用基于终端的文本编辑器,例如nano或vi -您将运行sudo nano / etc / hostnamesudo vi / etc / hostname命令来打开文件进行编辑。请查阅我们的使用nano或有关使用这些文本编辑器使用vi的帮助的简介。)

open-etc-hostname-file-for-editing

The /etc/hostname file is very simple. It’s contains only one thing — your computer’s hostname. To change it, just delete the existing hostname from the file. Replace it with your new desired hostname and then save the text file.

/ etc / hostname文件非常简单。 它仅包含一件事-您计算机的主机名。 要更改它,只需从文件中删除现有的主机名。 将其替换为所需的新主机名,然后保存文本文件。

etc-hostname-file-on-ubuntu,-mint,-or-debian

Ubuntu and other Debian-based distribution read the /etc/hostname file while booting up and set your computer’s hostname to the one contained in the file. Your change won’t take effect immediately — you’ll have to reboot or use the hostname command to change it immediately.

Ubuntu和其他基于Debian的发行版在启动时读取/ etc / hostname文件,并将计算机的主机名设置为文件中包含的主机名。 您所做的更改不会立即生效-您必须重新启动或使用hostname命令立即进行更改。

set-new-hostname-on-ubuntu-linux

编辑您的/ etc / hosts文件 (Edit Your /etc/hosts File)

The hostname is also stored in the /etc/hosts file, where it’s set to redirect to your local computer — localhost. You’ll need to change the hostname in your hosts file, too.

主机名也存储在/ etc / hosts文件中,该文件名设置为重定向到本地计算机localhost 。 您还需要在hosts文件中更改主机名。

Open the /etc/hosts file for editing with a command like the following one:

使用以下命令打开/ etc / hosts文件进行编辑:

sudo gedit /etc/hosts

须藤gedit / etc / hosts

(Once again, you could use any other text editor you want — nano, vi, or another graphical text editor of your choice.)

(再次,您可以使用所需的任何其他文本编辑器-nano,vi或您选择的其他图形文本编辑器。)

open-hosts-file-for-editing-on-ubuntu

Locate your old hostname in the hosts file. It’ll be on a line that looks something like:

在hosts文件中找到您的旧主机名。 它将显示在类似于以下内容的行中:

127.0.1.1 your-old-hostname

127.0.1.1您的旧主机名

add-new-hostname-to-hosts-file-on-ubuntu

Once again, replace the old hostname with your new hostname. You can then save the /etc/hosts file.

再次用新的主机名替换旧的主机名。 然后,您可以保存/ etc / hosts文件。

new-hostname-added-to-etc-hosts-file-on-ubuntu-linux

立即更改您的主机名 (Change Your Hostname Immediately)

The change above will take effect when you reboot, but you can use the hostname command in a terminal to change your hostname immediately. This command only changes the hostname until the next time you reboot, so you do have to change the /etc/hostname file to change it permanently.

上面的更改将在重新启动后生效,但是您可以在终端中使用hostname命令立即更改您的主机名。 该命令仅会更改主机名,直到下次重新引导为止,因此您必须更改/ etc / hostname文件才能对其进行永久更改。

Run the hostname command in a terminal window to specify a new hostname. If you chose an invalid hostname, this command will tell you and you can try again. Run the command like so:

在终端窗口中运行hostname命令以指定新的主机名。 如果您选择了无效的主机名,该命令将告诉您,您可以重试。 像这样运行命令:

sudo hostname your-new-hostname

sudo主机名your-new-hostname

change-hostname-without-rebooting-on-ubuntu

Your computer’s hostname will be changed immediately, although the change won’t appear in the terminal immediately. The terminal will notice when you re-open it — or if you just open a new one.

您的计算机的主机名将立即更改,尽管更改不会立即显示在终端中。 终端将在您重新打开时(或刚打开一个新终端时)发出通知。

change-hostname-immediately-on-ubuntu


That’s it; you should be done. On Linux distributions not based on Debian, there’s a different process. Check your Linux distribution’s documentation for more information. Some Linux distributions may even provide a graphical utility you can use to quickly change your hostname, so you don’t have to use the terminal — unless you want to.

而已; 你应该做的。 在不基于DebianLinux发行版中,有一个不同的过程。 查看Linux发行版的文档以获取更多信息。 某些Linux发行版甚至可能提供可用于快速更改主机名的图形实用程序,因此您不必使用终端,除非您愿意。

翻译自: https://www.howtogeek.com/197934/how-to-change-your-hostname-computer-name-on-ubuntu-linux/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值