Hostname is used to name the systems. Normally the computer networks work with IP addresses in order to identify and communicate. But hostname provides human friendly way to name, identify, communicate with other systems.
主机名用于命名系统。 通常,计算机网络使用IP地址进行识别和通信。 但是主机名提供了人类友好的方式来命名,标识和与其他系统通信。
主机名示例 (Example Hostname)
Hostname is just a meaningful word for the system administrators to name a system. For example we can use db1
for first database server or app3
for the third application server.
主机名只是系统管理员命名系统的一个有意义的词。 例如,我们可以将db1
用于第一台数据库服务器,将app3
用于第三台应用程序服务器。
默认主机名 (Default Hostname)
Hostnames generally set during the system installation. But may change after the installation. Different Linux distributions uses different default hostnames during installation. Here are some of them.
主机名通常在系统安装期间设置。 但是安装后可能会改变。 在安装过程中,不同Linux发行版使用不同的默认主机名。 这里是其中的一些。
德比安 (Debian)
debian
Ubuntu:
Ubuntu:
ubuntu
Fedora:
软呢帽:
localhost
CentOS:
CentOS的:
localhost
使用hostname命令打印主机名 (Print Hostname with hostname Command)
There are different ways to print default hostname of the system. hostname
command is the most known and popular way in order to print hostname of the current system. This command will work on all Linux distributions like Debian, Ubuntu, Mint, Kali, CentOS, RedHat, Fedora etc.
有多种方法可以打印系统的默认主机名。 hostname
命令是打印当前系统主机名的最广为人知的方式。 此命令将在所有Linux发行版(如Debian,Ubuntu,Mint,Kali,CentOS,RedHat,Fedora等)上运行。
$ hostname
As we can see the hostname of the given system is poftut1
如我们所见,给定系统的主机名是poftut1
使用hostnamectl命令打印主机名 (Print Hostname with hostnamectl Command)
There is a new command which is supported by all popular Linux distributions like Debian, Ubuntu, Mint, Kali CentOS, RedHat, Fedora. We can use hostnamectl
command which will provide the hostname of the current system and more like Chassis
, Operating System
etc.
所有流行Linux发行版(如Debian,Ubuntu,Mint,Kali CentOS,RedHat,Fedora)都支持一个新命令。 我们可以使用hostnamectl
命令,该命令将提供当前系统的主机名,例如Chassis
, Operating System
等。
$ hostnamectl
更改Ubuntu,Debian,Mint,Kali的主机名(Change Hostname For Ubuntu, Debian, Mint, Kali)
Now we will start to learn how to change hostname of the deb
based Linux distributions like Ubuntu, Debian, Mint, Kali. These distributions stored hostname settings in /etc/hostname
file. So changing this file will change hostname but this will require a reboot to make changes affect.
现在,我们将开始学习如何更改基于deb
Linux发行版(如Ubuntu,Debian,Mint和Kali)的主机名。 这些发行版将主机名设置存储在/etc/hostname
文件中。 因此,更改此文件将更改主机名,但这需要重新启动才能使更改生效。
In this example we will change hostname to the POF1
. We will also use sudo
because hostname change operation requires root
privileges.
在此示例中,我们将主机名更改为POF1
。 我们还将使用sudo
因为主机名更改操作需要root
特权。
$ sudo echo "POF1" > /etc/hostname
更改CentOS,RedHat的主机名 (Change Hostname For CentOS, RedHat)
Hostname configuration of rpm
based disributions like CentOs, RedHat, Fedora are stored in /etc/sysconfig/network
. HOSTNAME
line provides the hostname of the current system. Example configuration file is like below.
基于rpm
的发行版(例如CentOs,RedHat和Fedora)的主机名配置存储在/etc/sysconfig/network
。 HOSTNAME
行提供当前系统的主机名。 示例配置文件如下所示。
NETWORKING=yes
HOSTNAME="poftut1.domainname.com"
GATEWAY="192.168.1.1"
GATEWAYDEV="eth0"
FORWARD_IPV4="yes"
Just put the hostname you want to set to the poftut
. But there is also domain name part which is domainname.com
. We can set as default or change according to our settings. But if we do not need we can set whatever we want.
只需将要设置的主机名放入poftut
。 但是还有域名部分,即domainname.com
。 我们可以设置为默认值或根据我们的设置进行更改。 但是,如果我们不需要,我们可以设置我们想要的任何东西。
In order to make change affect we need to restart network services with the following command.
为了使更改生效,我们需要使用以下命令重新启动网络服务。
$ sudo systemctl restart network
使用sysctl命令更改主机名,适用于Ubuntu,Debian,Mint,Kali,CentOS,RedHat (Change Hostname with sysctl Command For Ubuntu, Debian, Mint, Kali, CentOS, RedHat)
sysctl
is a command use to configuration low level settings of a Linux system and kernel. We can use it to set hostname. In this example we will set as poftut1
.
sysctl
是用于配置Linux系统和内核的低级设置的命令。 我们可以使用它来设置主机名。 在此示例中,我们将设置为poftut1
。
$ sudo sysctl kernel.hostname="poftut1"
使用hostnamectl命令更改主机名对于Ubuntu,Debian,Mint,Kali,CentOS,RedHat (Change Hostname with hostnamectl Command For Ubuntu, Debian, Mint, Kali, CentOS, RedHat)
We can also use hostnamectl
command in order to change current hostname configuration. In this case we will use set-hostname
parameter. In this example we will set the hostname as poftut1
我们也可以使用hostnamectl
命令来更改当前的主机名配置。 在这种情况下,我们将使用set-hostname
参数。 在此示例中,我们将主机名设置为poftut1
$ sudo hostnamectl set-hostname "poftut1"
翻译自: https://www.poftut.com/how-to-change-linux-hostname-in-ubuntu-debian-mint-kali-centos-redhat/