How to Change the Hostname on Linux
On Linux, you can change the hostname by using the hostnamectl command.
You’ll typically need to do this right after provisioning a new server, since cloud providers often assign random hostnames like ip-172-31-22-45. Having a meaningful hostname makes it much easier to identify which machine you’re on when SSH’d into multiple servers.
How to Set the Hostname on Linux
sudo hostnamectl set-hostname <new-hostname>
For example:
sudo hostnamectl set-hostname server1
At this point, you will need to logout and back in to see the changes have taken effect.
The change persists across reboots. If you also want the hostname to resolve locally, add it to /etc/hosts alongside 127.0.0.1. On older distros without hostnamectl, you’d edit /etc/hostname directly instead.