A server's hostname is its name on the network, such as server1.example.com. It appears in your shell prompt, in logs, and in the headers of every email the server sends. This guide shows how to change it on a normal Linux server and, separately, on a cPanel server, where the method is different.
Quick answer
-
Linux server without cPanel (AlmaLinux, Rocky Linux, Debian, Ubuntu):
hostnamectl set-hostname server1.example.com -
cPanel server: use WHM › Networking Setup › Change Hostname, or as root:
/usr/local/cpanel/bin/set_hostname server1.example.comDo not use
hostnamectlalone on a cPanel server: cPanel has to update its own services too. cPanel
What you need: root access, and a DNS A record for the new hostname pointing to the server's IP address.
Choose a good hostname
Use a fully qualified domain name: a name inside a domain you own, such as server1.example.com. On cPanel servers, the hostname must also: cPanel
- be 60 characters or fewer, in lowercase Latin letters,
- not start with
wwwor a number, and not end with a hyphen, - not be a domain that a hosting account on the server uses,
- not be a service name such as
cpanel.example.comorwhm.example.com.
These are good rules on any server, because the hostname is visible in email headers.
On a Linux server without cPanel
Step 1: Check the current hostname
hostnamectl
The Static hostname line shows the current name.
Step 2: Set the new hostname
hostnamectl set-hostname server1.example.com
This changes the name immediately and saves it in /etc/hostname, so it survives a reboot. hostnamectl You do not need the --static option that older guides use: without options, the command changes the static, running (transient) and "pretty" names together. hostnamectl Newer systemd versions also accept hostnamectl hostname server1.example.com, which does the same.
Step 3: Update /etc/hosts
Some programs look the name up locally. Open /etc/hosts with vi and make sure the server's IP address is followed by the new name, for example:
203.0.113.10 server1.example.com server1
Replace any line that still shows the old hostname.
Step 4: Check it
hostnamectl
hostname -f
Both should show server1.example.com. Log out and back in to see it in your shell prompt.
On a cPanel server
cPanel stores the hostname in several places (the mail server, SSL certificates, service settings). Changing it with hostnamectl alone leaves those behind. Use one of cPanel's methods: cPanel
Option A: In WHM
- Log in to WHM as root.
- Go to Networking Setup › Change Hostname.
- Enter the new hostname in New Hostname and click Change.
- When it finishes, click Add An A Entry for your hostname at the bottom, check the details, and click Add Entry. This creates the DNS record if the server hosts DNS for that domain.
Option B: From the command line
/usr/local/cpanel/bin/set_hostname server1.example.com
This makes the same changes as the WHM page. cPanel
What happens during the change
cPanel restarts the services that need it straight away. While the change runs, users briefly cannot use Calendars and Contacts. cPanel
Software that cPanel does not manage keeps the old name until you update it yourself. If you sign in to WHM through an external login provider, update the redirect address there as well. cPanel
If your server has an automatic hostname ending in cprapid.com, cPanel recommends replacing it with a name in your own domain. cPanel
The hostname changes back after a reboot
On cloud servers and VPSs, a tool called cloud-init may set the hostname from the provider's settings at every boot, undoing your change. Either:
- change the server's name in your provider's control panel as well, or
- tell cloud-init to keep your hostname: open
/etc/cloud/cloud.cfgwithviand setpreserve_hostname: true.
On cPanel servers at cloud providers, cPanel has its own guidance in its "Cloud-Hosted Hostname Solution" documentation. cPanel
Troubleshooting
Email from the server lands in spam after the change. Receiving servers check that the hostname and the server's IP address match. Add the A record for the new hostname, and ask your provider to set the IP address's reverse DNS (PTR) to the new hostname.
hostname -f shows the old name or an error. /etc/hosts still has the old name, or there is no line for the server's IP address. Fix it as in Step 3.