Centos 7 and 8 modify the various methods of hostname

hostname View the current system host name, know that the current host name is localhost

Of course, the hostnamectl command is specially added in centos7 to view and modify the host name

Use the hostnamectl set-hostname command to modify the host name, which can take effect permanently:

 hostnamectl set-hostname localzly

The host name is set during the installation of the operating system, or dynamically assigned to the virtual machine when the virtual machine is created. There are many reasons why you need to change the host name. Before changing, please make sure that you are a user with sudo privileges. log in.

The host name is a label used to identify devices on the network. On the same network, you should not have two or more computers with the same host name.

The main host names are:

1. static-traditional hostname, which is stored in the /etc/hostname file and can be set by the user.

2. Transient- the dynamic host name maintained by the kernel. The DHCP or mDNS server can change the temporary host name during runtime. By default, it is the same as the static host name.

For static and temporary names (static and transient), such as host.example.com, it is recommended to use a fully qualified domain name (FQDN).

Display the current host name

To view the current host name, execute the following command:

hostnamectl

Three ways to change the hostname

Method 1: Use hostnamectl command

In CentOS 8 and all other Linux distributions that use systemd, you can use the hostnamectl command to change the system hostname and related settings, the syntax is as follows:

sudo hostnamectl set-hostname localzly

sudo hostnamectl set-hostname localzly --static

sudo hostnamectl set-hostname localzly --transient

For example, to change the system static hostname to localzly, you can use the following command:

 sudo hostnamectl set-hostname localzly

To verify that the host name has been successfully changed, use the hostnamectl command.

View transient hostname

hostnamectl --transient

Method 2: Use nmtui command

nmtui is a curses-based tool used to interact with NetworkManager. It can also be used to set or change the hostname.

Start the tool by typing its name in the terminal:

If it is a root account, there is no need for sudo permissions

sudo nmtui

Use the arrow keys to browse through the options, select to set the system hostname, and press Enter:

Enter the new host name:

Press Enter to confirm the new host name:

Finally, restart the systemd-hostnamed service for the changes to take effect:

sudo systemctl restart systemd-hostnamed

Method 3: Use nmcli command

nmcli is a command line tool used to control NetworkManager. It can also be used to change the hostname of the system.

To view the current host name, enter:

sudo nmcli g hostname

To change the hostname to localzly, use the following command:

sudo nmcli g hostname localzly

In order for the changes to take effect, restart the systemd-hostnamed service: 

sudo systemctl restart systemd-hostnamed

in conclusion

The above three are the easiest methods, any of which can be implemented to change the hostname (Hostname) on the CentOS 8 version.

Another method is to directly modify the hostname file, vim /etc/hostname , this method modification is also permanent and effective:

Guess you like

Origin blog.csdn.net/q1246192888/article/details/114556434