On Amazon Linux 2, you can change the hostname without rebooting the system by following these steps:
- Edit the hostname file: Run the command
sudo nano /etc/hostname
to open the hostname file in the nano text editor. Change the current hostname to the desired hostname, then save the file and exit the editor. - Update the hosts file: Run the command
sudo nano /etc/hosts
to open the hosts file in the nano text editor. Change the current hostname to the desired hostname in the line that starts with “127.0.0.1”, then save the file and exit the editor. - Update the hostname: Run the command
sudo hostnamectl set-hostname <new hostname>
. This will update the hostname without rebooting the system. (https://midwaymoving.com) - Update the DNS resolution: Run the command
sudo service NetworkManager restart
to update the DNS resolution. - Verify the changes: Run the command
hostname
to verify that the hostname has been changed to the desired hostname.
Please keep in mind that the above steps will change the hostname, but it might not reflect the change on the command prompt, you will have to logout and login again or open a new shell to see the changes in the command prompt.
Also, please note that these steps might not work on all Linux distributions, and the above instructions are specific to Amazon Linux 2.