如何在Ubuntu,Debian,Mint,Kali,CentOS,RedHat中更改Linux主机名?

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
Print Hostname with hostname Command
Print Hostname with hostname Command
使用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命令,该命令将提供当前系统的主机名,例如ChassisOperating System等。

$ hostnamectl
Print Hostname with hostnamectl Command
Print Hostname with hostnamectl Command
使用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.

现在,我们将开始学习如何更改基于debLinux发行版(如Ubuntu,Debian,Mint和Kali)的主机名。 这些发行版将主机名设置存储在/etc/hostname文件中。 因此,更改此文件将更改主机名,但这需要重新启动才能使更改生效。

LEARN MORE  What Is "server_name" in Nginx Web Server Configuration?
了解更多Nginx Web服务器配置中的“ server_name”是什么?

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/networkHOSTNAME行提供当前系统的主机名。 示例配置文件如下所示。

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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值