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.
The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
First we use the hostname command to view the current host name.
hostname
You can also use the hostnamctl command
hostnamectl
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.
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
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.
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!