How do i change hostname?

Posted May 6, 2015 66.4k views
Linux BasicsUbuntuLinux Commands

How do i change the hostname of a running server?


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
5 answers
KFSys May 26, 2020
Pinned Answer

Hi all,

To change the hostname of your server, you’ll need to follow 4 quick and easy steps. Let’s begin:

Enter your Droplet

Firstly, you’ll need to SSH to your Droplet with either root or a sudo user. You can do that using the command

ssh root@YourDropletIP

Another way would be using the droplet’s console from your Control Panel with DigitalOcean.

Change the hostname using hostnamectl

The first step once you have entered the droplet would be to use the hostnamectl command. Let’s say we want to name our droplet ‘ExampleName’. To do so, we need to execute the command like so

sudo hostnamectl set-hostname ExampleName

The hostnamectl command does not produce output. On success, 0 is returned, a non-zero failure code otherwise.

Update the /etc/hosts file

Next on our list would be to update the hosts file located in the etc folder. You can use any editor you want from nano, vi, vim.

sudo nano /etc/hosts

In there you’ll see something similar to

# Your system has configured 'manage_etc_hosts' as True.
# As a result, if you wish for changes to this file to persist
# then you will need to either
# a.) make changes to the master file in /etc/cloud/templates/hosts.debian.tmpl
# b.) change or remove the value of 'manage_etc_hosts' in
#     /etc/cloud/cloud.cfg or cloud-config from user-data
# PreviousNameHere localhost

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

Change the line which says the following PreviousNameHere

to be ExampleName

Save the file and exit.

Here I want to do a disclaimer, the line PreviousNameHere Will actually contain your previous hostname and not PreviousNameHere.

Edit the cloud.cfg file

It’s possible you have the cloud-init package installed. In such a case, you’ll need to update one last file - /etc/cloud/cloud.cfg.

First, check if the file exists

ls -lah /etc/cloud/cloud.cfg

If the package is installed the output will look like the following:

-rw-r--r-- 1 root root 3.5K May 26 07:01 /etc/cloud/cloud.cfg

If it isn’t installed

ls: cannot access '/etc/cloud/cloud.cfg': No such file or directory

In the case it’s installed, you’ll need to edit it as well. Open the file,

sudo nano /etc/cloud/cloud.cfg 

Find the line

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: false

and change it to be

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true

Save the file and exit.

That’s it, you are ready now!!

To make sure everything is working properly, execute the command



Do the following:

  • Step 1. sudo nano /etc/hosts

  • Step 2. change your-current-hostname to your-new-hostname. Then save the file.

  • Step 3. sudo nano /etc/hostsname

  • Step 4. change currently entered hostname in that file and enter your new hostname. Save the file after.

@thefliik - Sorry for that typo.
/etc/hostsname …should be /etc/hostname.

On CentOS6 I found the following files:
/etc/network : HOSTNAME=ABCD
/etc/hosts : added ABCD to the IPv4 (at the end of the line starting with

After a reboot, all good