Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Change the Hostname on a CentOS Server

This guide shows you how to give your CentOS server a new hostname. It needs root access, so it fits a VPS or dedicated server.

How-To Guides2 min read8 steps

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

  1. Log in to your server with SSH. SSH is a safe way to type commands on a server from far away.
  2. Check the current name.
    hostnamectl
    This prints the hostname and some system details.
  3. Set the new name. Replace the example with your own.
    sudo hostnamectl set-hostname server1.example.com
  4. Open the hosts file in a text editor.
    sudo vi /etc/hosts
    This file maps names to addresses on your server.
  5. 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.
    203.0.113.10   server1.example.com   server1
    Use your own IP address. The one shown is only an example.
  6. Save the file. In vi, press Esc, type :wq and press Enter.
  7. Check the result.
    hostname
    hostname -f
    The first shows the short name. The second shows the full name.
  8. 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.