To change the hostname on an Amazon Linux 2 system without rebooting, you can use the hostnamectl
command. hostnamectl
is a utility for managing the hostname on Linux systems.
To change the hostname, follow these steps:
Open a terminal window.
Use the hostnamectl
command to set the hostname. To do this, use the following command, replacing new_hostname
with the desired hostname:
sudo hostnamectl set-hostname new_hostname
This will change the hostname to new_hostname
without rebooting the system.
/etc/hosts
file to reflect the new hostname. To do this, open the /etc/hosts
file in a text editor and update the hostname in the first line. For example:sudo nano /etc/hosts
Change the first line from:
127.0.0.1 localhost localhost.localdomain old_hostname
to:
127.0.0.1 localhost localhost.localdomain new_hostname
Save the /etc/hosts
file and exit the text editor.
sudo systemctl restart network
After the network has been restarted, the hostname will be changed to the new value.