Changing Default Hostname

Changing Default Hostname

The default hostname is the hostname you choose for your VPS once you purchased it. Which should look something like hostname.example.com. Should you want to change the default name after you set up your VPS then follow this simple tutorial.

The following command under BASH will show you the current hostname.

[user@localhost ~]$ uname -n
localhost.localdomain

Step 1: Add an A record to the DNS records

At your DNS control panel add an A record to your new subdomain and point it to the IP address of your VPS. Usually this is done where you purchased your domain name at. There are many free alternative DNS hosting, however, I recommend using Cloudflare Free DNS Hosting

Select A Record for Type and enter the Host you would like to point to an IP address:

[naurus] – you can insert any name of a subdomain that should be pointed to the IP address:
A Record | naurus | 11.22.33.44

Step 2: Changing the Hostname on the VPS

The following commands can be used for changing the hostname for both CentOS 7.x and Ubuntu 16.04. As root user in CLI edit file at /etc/hostname. Using nano, vi or pico text editor:

[user@localhost ~]$ sudo vim /etc/hostname

Only add the hostname here nothing else;

naurus.example.com

You can also use the hostnamectl command to check and change the hostname too.

[user@localhost ~]$ 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=[USER@]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
[user@localhost ~]$

To set the hostname:

[user@localhost ~]$ sudo hostnamectl set-hostname naurus.example.com

Edit /etc/hosts file and append a new entry to existing entries associated to your server’s main IP address i.e. 11.22.33.44 to the new hostname

11.22.33.44 naurus.example.com naurus

Finally add type in the command

[user@localhost ~]$ sudo hostname naurus.example.com

Then, restart network using the systemctl command:

sudo systemctl restart network

You can check the changes by typing this command again:

[user@localhost ~]$ uname -n

If all went well then you should see your new hostname:

naurus.example.com

Congratulations!