Run hostnamectl set-hostname server.example.com as root and update /etc/hosts. On a cPanel server, change it in WHM or with set_hostname instead.
Choose a good hostname
- Use a fully qualified domain name under a domain you own, for example
server.example.com. - Don't use a domain that one of your websites uses (so not
example.comorwww.example.com). - Use lowercase letters, numbers and hyphens. Don't start with a number or
www, and stay under 60 characters. - Create an A record for the hostname that points to the server's main IP address. Mail servers and SSL certificates rely on it.
Change the hostname (servers without cPanel)
Connect to the server as root (see Connect to your server with SSH) and set the new name:
hostnamectl set-hostname server.example.comOpen
/etc/hosts:nano /etc/hostsIf the old name appears in it, replace it with the new one. On Ubuntu the line usually starts with
127.0.1.1:127.0.1.1 server.example.com serverSave with Ctrl+O, Enter, and close with Ctrl+X.
Check the result:
hostnamectl hostname -fStatic hostname should show the new name, and
hostname -fshould printserver.example.com.
Your SSH prompt still shows the old name until you log out and in again.
Change the hostname on a cPanel & WHM server
On a cPanel server, don't use hostnamectl alone. cPanel also has to update its own services and settings, and it only does that through its own tools.
In WHM: go to Networking Setup » Change Hostname, type the name in New Hostname and click Change. Then add the A record: click the Add An A Entry link that WHM shows (or open DNS Functions » Add an A Entry for Your Hostname) and click Add Entry. That works when this server hosts the DNS for your domain; otherwise create the A record wherever your domain's DNS is managed.
From the command line:
/usr/local/cpanel/bin/set_hostname server.example.com
Check it with hostname. cPanel restarts the services that need it. A free SSL certificate for the new name follows: see Free SSL for the server hostname and sites in WHM (AutoSSL).
Before installing cPanel on a new server, hostnamectl set-hostname is the right tool, as shown in Install cPanel & WHM on AlmaLinux.
Common problems
The old hostname comes back after a reboot
On many cloud and VPS images, cloud-init sets the hostname at every boot. Tell it to keep yours:
echo "preserve_hostname: true" > /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg
Reboot and check with hostnamectl. If /etc/hosts starts with a note that cloud-init manages the file, your edits there are overwritten too: change the template the note names instead.
"sudo: unable to resolve host"
The new name is missing from /etc/hosts. Add it to the 127.0.1.1 line as shown above.
Emails from the server land in spam or bounce
Check that the hostname has an A record (dig +short server.example.com should print the server's IP) and ask your server provider to set the reverse DNS (PTR) of the IP to the same name.
Related articles
- Connect to your server with SSH
- Install cPanel & WHM on AlmaLinux
- Free SSL for the server hostname and sites in WHM (AutoSSL)
Still stuck? Open a support ticket and we'll help.