Title: How to correctly configure the Linux host name, specific code examples are required
In the Linux system, the host name is the unique name that identifies a computer on the network. Correctly configuring the host name not only helps us manage the host more conveniently, but also improves network security. The following will introduce how to correctly configure the Linux host name and give specific code examples.
View the current host name
In the Linux system, we can use the hostname
command to view the current host name. Enter the following command in the terminal:
hostname
Modify the host name
a. Temporarily modify the host name
If we just want to temporarily modify the host name, we can use the following command:
sudo hostname new_hostname
This command will immediately modify the host name, but will revert to the original host name after restarting.
b. Permanently modify the host name
To permanently modify the host name, we need to edit the /etc/hostname
file. Open the editor with the following command:
sudo nano /etc/hostname
In the opened file, modify the original hostname to the new desired hostname and save and exit.
Configuration host file
Host file/etc/hosts
is used to map IP addresses and host names. After modifying the hostname, we need to update the hosts file. Use the following command to edit the host file:
sudo nano /etc/hosts
After opening the file, add the following content on the first line:
127.0.0.1 new_hostname
Replace new_hostname
with your modified hostname, Then save the file and exit the editor.
Restart the network service
After modifying the host name, we need to restart the network service for the change to take effect. Use the following command to restart the network service:
sudo systemctl restart networking
or
sudo systemctl restart NetworkManager
hostname
command to check the host name Whether the modification has been successful. Through the above steps, we can correctly configure the Linux host name and implement it through specific code examples. Remember to back up relevant files before modifying the host name to avoid system failure caused by operational errors. Hope this article is helpful to you.
The above is the detailed content of How to correctly configure a Linux hostname. For more information, please follow other related articles on the PHP Chinese website!