Linux modify hostname temporarily/permanently

Note The
host name can be a string of any format, up to 64 characters. However, it is recommended that the static and transient names match the fully qualified domain name (FQDN) adopted by DNS, such as host.example.com. It is also recommended to include only 7-byte ASCII lowercase characters in static and transient names, without spaces or dots, and restrict them to the DNS domain name label format, although this is not a strict requirement. Underscores were not allowed in the previous requirements, so it is not recommended to use them here. The hostnamectl tool enforces the following restrictions: static and transient hostnames only contain az, AZ, 0-9, "-", "_" and ".". Periods cannot be used at the beginning or end, and two concatenations are not allowed The period. The size is limited to 64 characters.

1. Temporarily modify the host name

[root@localhost ~]# hostname       //或hostnamectl(CentOS7)

process

[root@localhost ~]# hostname   //查看主机名
localhost.localdomain
[root@localhost ~]# hostname xsnq   //临时修改
[root@localhost ~]# hostname  //此处实际上已经是修改成功的
xsnq

In this method, after the server restarts, the host name will still be the original name.

2. Permanently modify the host name

1. Method One

Use hostnamectl to modify, hostnamectl is a newly added command in centos7.
hostnamectl for more usage click here

语法
hostname set-hostname 主机名 --参数
[root@localhost ~]# hostname set-hostname xsnq --static   //定义静态的(static)主机名
// hostname set-hostname xsnq 设定所有主机名   可加参数 –pretty、–static、--transient 中的一个或多个选项。

//远程重新连接或重启服务器
[root@xsnq ~]# 

2. Method two

Modify the hostname configuration file

[root@localhost ~]# vi /etc/hostname 

Modify localhost.localdomain to the name of the host you want to get.

For example, modify localhost.localdomain to xsnq .

Restart the server (if you have made temporary changes, you only need to save the configuration file, and the new host name can be read after the next server restart)

[root@localhost ~]# init 6
[root@xsnq ~]# 

At this time, the host name has been permanently modified successfully.

If there are any errors in this article, please leave a comment and correct me.

Guess you like

Origin blog.csdn.net/weixin_46623617/article/details/109562102