What is a hostname?
A hostname is the name of your server. It works like a name on a door. It is often written like server1.example.com. Mail programs and control panels use it, so a good hostname matters. Root is the all-powerful admin account on Linux. You need root, or sudo, for these steps.
This guide is written for CentOS 7 and newer systems that use systemd. CentOS 7 is old. Newer versions such as AlmaLinux and Rocky Linux work in a very similar way.
Warning: Some software, such as control panels and mail servers, depends on the hostname. Take a backup before you change it. Ask Hostvento support if you run a control panel.
Steps
- Log in to your server with SSH. SSH is a safe way to type commands on a server from far away.
- Check the current name.
This prints the hostname and some system details.hostnamectl - Set the new name. Replace the example with your own.
sudo hostnamectl set-hostname server1.example.com - Open the hosts file in a text editor.
This file maps names to addresses on your server.sudo vi /etc/hosts - Add or edit a line with your server's public IP address and the new name. An IP address is the number that identifies a computer on the internet.
Use your own IP address. The one shown is only an example.203.0.113.10 server1.example.com server1 - Save the file. In vi, press
Esc, type:wqand pressEnter. - Check the result.
The first shows the short name. The second shows the full name.hostname hostname -f - Restart any service that uses the hostname, or reboot the server. To reboot, type
sudo reboot.
On very old CentOS 6
CentOS 6 stores the name in /etc/sysconfig/network. Change the line that starts with HOSTNAME=. Then run hostname newname and reboot.
Good to know
- Use only letters, numbers and dashes.
- Point a DNS record to your server so the name works from outside. DNS is the internet's phone book.
Quick recap
- Use
hostnamectl set-hostname. - Update
/etc/hosts. - Check with
hostname -f, then reboot.