" command; 3. Use "sudo hostname " command."/> " command; 3. Use "sudo hostname " command.">

Home  >  Article  >  Operation and Maintenance  >  How to change the host name in linux

How to change the host name in linux

青灯夜游
青灯夜游Original
2021-12-21 16:00:0875920browse

Method: 1. Open the "/etc/hostname" file, delete the old hostname, replace it with the new hostname, and save the file; 2. Use "sudo hostnamectl set-hostname " command; 3. Use the "sudo hostname " command.

How to change the host name in linux

The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

View the current host name

First we use the hostname command to view the current host name.

hostname

You can also use the hostnamctl command

hostnamectl

How to change the host name in linux

Method 1: Modify the configuration file

The host name is saved in /etc/ hostname file, so we can open this file and manually edit the hostname.

sudo nano /etc/hosts

Delete the old host name, replace it with the new host name, and save the file. Pay attention to capitalization.

How to change the host name in linux

If you don't update the /etc/hosts file, then some programs, such as sudo, don't know how to resolve the new hostname.

If you are changing the host name of the Linux server, the new host name should be resolved to the public IP of the Linux server. If you change the hostname of your PC, the new hostname should resolve to 127.0.0.1, or 127.0.1.1.

127.0.1.1 is the IP of the local host resolved by the Debian Linux distribution. When the Debian system is installed, if the computer's IP is dynamic, the Debian installation program will create the 127.0.1.1 entry in the /etc/hosts file. The 127.0.1.1 address uses the loopback network card. In fact, 127.0.0.0/8 (127.0.0.0 ~ 127.255.255.255) all use the loopback network card.

Method 2: hostnamectl command

The command syntax is:

sudo hostnamectl set-hostname <newhostname>

This command will delete the host name in the /etc/hostname file and then replace it with the new host name . Like the first method, we also need to update the /etc/hosts file. The essence of both methods is the same.

Method 3: Hostname command to make temporary changes

If you only need to temporarily change the host name, you can use the hostname command.

sudo hostname <new-hostname>

This command will not change the static hostname (static hostname) in the /etc/hostname file, it only changes the temporary hostname (transient hostname). So after restarting the computer it will go back to the old hostname.

Static hostnames are saved in the /etc/hostname file.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of How to change the host name in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn