How to change hostname on CentOS and Ubuntu

The hostname is used to identify the server when it is connected to the network and configured during the initial server setup. Changing the initial hostname of a server is a task that you might do when using a server and in this tutorial we will show you how to change the hostname on a Linux VPS running CentOS 7 or Ubuntu 16.04 as the operating system.

What is a hostname?

Hostname is a label used to identify a machine and will help you easily distinguish a server from another. The hostname must be a fully qualified domain name (FQDN) and must point to the IP address of the server so that you can access the server using it.

Check the current hostname

Changing the hostname is a fairly simple process, and only takes a few minutes of your time. You will need to connect to the server via SSH to be able to make this change. If you don’t know how to connect to a server via SSH, you can use our guide that is available here.

After you login, you need to determine the current hostname. If you are using CentOS 7 VPS or Ubuntu 16.04 VPS you can use command hostnamectl

# hostnamectl status

The output should be similar to the output below:

 Static hostname: vps.server.ru
       Icon name: computer-container
         Chassis: container
      Machine ID: * * * 
         Boot ID: * * * 
Operating System: CentOS Linux 7 (Core)
     CPE OS Name: cpe:/o:centos:centos:7
          Kernel: Linux 2.6.32-042stab120.3
    Architecture: x86-64

Value static hostname this is the value you should be looking at.

Changing the current hostname

To change the hostname on your CentOS or Ubuntu machine, you must run the following command:

# hostnamectl set-hostname virtual.server.ru

Please note that you need to replace virtual.server.com with the new hostname you want to use.

Now when you check the current state of the hostname with the command hostnamectl status, you will see the new hostname virtual.server.ru instead of the old vps.server.ru… This means that you have successfully changed the hostname on the server.

Additional command line parameters

For more command options, you can add the flag --help at the end.

# hostnamectl --help
hostnamectl [OPTIONS...] COMMAND ...

Query or change system hostname.

  -h --help              Show this help
     --version           Show package version
     --no-ask-password   Do not prompt for password
  -H --host=[[email protected]]HOST  Operate on remote host
  -M --machine=CONTAINER Operate on local container
     --transient         Only set transient hostname
     --static            Only set static hostname
     --pretty            Only set pretty hostname

Commands:
  status                 Show current hostname settings
  set-hostname NAME      Set system hostname
  set-icon-name NAME     Set icon name for host
  set-chassis NAME       Set chassis type for host
  set-deployment NAME    Set deployment environment for host
  set-location NAME      Set location for host

Sidebar