Linux system to modify host name
In Linux systems, modifying the host name (hostname) can be done in the following ways:
1. Temporary modification
The temporarily modified host name is only valid in the current session and will be restored to its original name after the system restarts.
sudo hostname new_hostname
2. Permanent modification
Permanently modifying the host name requires editing the system configuration file so that it also maintains a new name after the system restarts.
For usesystemd
Systems (such as Ubuntu 16.04 and later, CentOS 7 and later)
- 1. Use
hostnamectl
Order:
sudo hostnamectl set-hostname new_hostname
This will update/etc/hostname
File and configure appropriatelysystemd
。
- 2.Edit
/etc/hosts
document:
Make sure to be in/etc/hosts
Update the host name in the file to remain consistent.
For example, replace the old hostname with the new one:
127.0.0.1 new_hostname
- 3. Restart the system (or use
hostnamectl
Reload):
sudo systemctl restart systemd-hostnamed
For older systems (such as Ubuntu 14.04 or CentOS 6)
- 1.Edit
/etc/sysconfig/network
File (CentOS):
Modify or addHOSTNAME
variable:
HOSTNAME=new_hostname
- 2.Edit
/etc/hostname
File (Ubuntu):
Change the contents in the file to a new hostname:
new_hostname
- 3. Update
/etc/hosts
File (same as above). - 4. Restart the system:
sudo reboot
These steps can help you modify the host name according to different Linux distributions and system environments
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.