Which command sets a hostname on a new Linux server?
Setting the name
The hostname identifies the machine on your network and in logs. On systemd-based systems, hostnamectl stores the name persistently, so it survives a reboot. Choose a short, descriptive name such as the role and the region rather than a random string.
- Run hostnamectl set-hostname with the new name
- Confirm with hostnamectl status
- Update the 127.0.1.1 line in /etc/hosts
Keeping the hosts file in sync
Some tools read the local hosts file to resolve the machine's own name, and a mismatch can slow sudo or cause odd warnings. Edit the file with sudo and make sure the old name is gone. A quick reboot afterward confirms the change sticks.
Checking older systems
Older systems without systemd may store the name in a plain file or a different command. Check your distribution's documentation before changing it. Matching the name in your cloud console can also help with reporting.
Record the new name in your server inventory at the same time, so later troubleshooting does not start from a wrong label. If the server runs a web app, check that its config files use the new name before you restart services.
Common mistakes
- Changing the name only in the hosts file and not the system setting.
- Using a name with spaces or capital letters that some tools reject.
- Forgetting that the cloud dashboard label and the system hostname can differ.
