SoFunction
Updated on 2025-03-03

How to modify the host name in Linux system

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 usesystemdSystems (such as Ubuntu 16.04 and later, CentOS 7 and later)

  • 1. UsehostnamectlOrder:
sudo hostnamectl set-hostname new_hostname

This will update/etc/hostnameFile and configure appropriatelysystemd

  • 2.Edit/etc/hostsdocument:

Make sure to be in/etc/hostsUpdate 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 usehostnamectlReload):
sudo systemctl restart systemd-hostnamed

For older systems (such as Ubuntu 14.04 or CentOS 6)

  • 1.Edit/etc/sysconfig/networkFile (CentOS):

Modify or addHOSTNAMEvariable:

HOSTNAME=new_hostname
  • 2.Edit/etc/hostnameFile (Ubuntu):

Change the contents in the file to a new hostname:

new_hostname
  • 3. Update/etc/hostsFile (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.