Article

Table of Contents
Theme:
Was this article helpful?
Try Vultr Today with

$50 Free on Us!

Want to contribute?

You could earn up to $300 by adding new articles!

Change Hostname on Ubuntu

Last Updated: Thu, Apr 23, 2020
Best Practices System Admin Ubuntu

Supported Versions

This guide applies to:

  • Ubuntu 20.04 LTS
  • Ubuntu 19.10
  • Ubuntu 18.04 LTS
  • Ubuntu 16.04 LTS

Introduction

When creating a new Vultr VPS, you can set the Server Hostname before deployment. If you skip this step or decide to rename your server later, you'll discover that changing the server hostname from the Customer Portal also performs a fresh reinstall of the entire server.

Change Hostname Example Screenshot

Follow this guide if you need to change the server hostname without reinstalling.

Examples

This guide uses olddog for the old hostname and newtricks for the new hostname.

1. Check the Current Hostname

Check the hostname with hostname.

$ hostname
olddog

You can also use hostnamectl.

$ hostnamectl 
   Static hostname: olddog
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 00c6efc1008d46aabeed06a3b8a459ec
           Boot ID: 539da7ee88a1499db77d3ef8b82b7a72
    Virtualization: kvm
  Operating System: Ubuntu 20.04 LTS
            Kernel: Linux 5.4.0-26-generic
      Architecture: x86-64

2. Change Hostname

Change the hostname to newtricks.

$ sudo hostnamectl set-hostname newtricks

3. Update /etc/hosts

Use your favorite text editor to change any instances of your old hostname to the new hostname in /etc/hosts. If you have a DNS name pointed to this instance, it's a best practice to also set that name here.

$ sudo nano /etc/hosts

From:

127.0.0.1 localhost
127.0.1.1 olddog

To:

127.0.0.1 localhost
127.0.1.1 newtricks.example.com newtricks

4. Confirm the Change

Reboot the server.

Test your change with hostnamectl and hostname.

$ hostnamectl 
   Static hostname: newtricks
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 00c6efc1008d46aabeed06a3b8a459ec
           Boot ID: 539da7ee88a1499db77d3ef8b82b7a72
    Virtualization: kvm
  Operating System: Ubuntu 20.04 LTS
            Kernel: Linux 5.4.0-26-generic
      Architecture: x86-64

$ hostname
newtricks
$ hostname -f
newtricks.example.com

Notes for cloud-init

Note: If you deployed one of Vultr's standard images, you can stop here. Our standard images do not need this update because they do not have cloud-init installed.

If you installed from your own Ubuntu ISO, you might have cloud-init installed. The cloud-init service may override your hostname on the next reboot. To correct this, edit the /etc/cloud/cloud.cfg file on your server instance. If cloud.cfg doesn't exist, then cloud-init is not installed; you do not need to create it.

$ sudo nano /etc/cloud/cloud.cfg

Add the following string to the bottom of /etc/cloud/cloud.cfg.

preserve_hostname: true

Save and exit the file. Reboot the server and verify the hostname is set correctly.

Update DNS

Your server hostname is changed and ready to use. This procedure does not change your DNS name, and you'll need to perform those steps at your domain registrar.

Want to contribute?

You could earn up to $300 by adding new articles