changing hostname | aws ubuntu 16.04

5 Steps to Changing the hostname on Linux

To change the hostname you must . After that on linux you must run one command and make a change in two files. Then you must reboot and do your checks.

  1. know which characters are allowed (and where)
  2. run a command called “hostname” with sudo priveleges
  3. make a change within the /etc/host file
  4. change the /etc/hostname file
  5. reboot your machine and perform your checks

1. Which Characters are Allowed (and where)?

It pays to know the rules that all hostnames should be bound by. The hostname

  • must be a string no less than 3 and no more than 24 characters
  • characters can be from the alphabet (A-Z), digits (0-9) and hyphen (-)
  • hyphens can only delimit alphabet characters (a to z and 0 to 9)
  • hyphens cannot therefore be next to each other (consecutive) and also
  • hyphens cannot delimit, nor lead, nor trail digits
  • the first character must be (a to z) – not even a digit
  • the last character can be either (a to z) or a digit
  • characters have no case – so A to Z and a to z are the same

2. Run the “hostname” command

You need sudo priveleges.

sudo hostname assets4u

Note that this hostname change will not survive a reboot. That’s the job of the next two file change actions.
Also just after you change the hostname your shell prompt will not reflect the new hostname.
But grab yourself a new shell and it will.

3. Edit the /etc/hosts file.

Add a line like the below at the top of the file.       assets4u        assets4u

4. Edit the /etc/hostname file.

Simple replace the solitary hostname with the new one.


5. Perform a reboot

Surprisingly (for Linux), you need to reboot in order to propagate the changes across the system and the network.

Frequently Asked Questions

What if pings to new host name don’t work

ping assets4u

What if pings from another machine to the <new-host-name> do not work. This is because your DHCP server (Dynamic Host Configuration Protocol) has not received a request for an IP address from a host matching that description. A network restart is in order.

sudo network restart

Pinging the new host name will start to work when you do a “network restart” on the machine with the new name. This effectively sends that machine to the dhcp server (cap in hand), saying two things – “Please can I register my new name? and 2. Which IP address are you going to give me?”.

Virtual Machine (VM) pings to new host name still don’t work

This can happen even after rebooting the virtual machine.

VirtualBox (the mother of virtual machines) is pulling the networking strings in the background. So a ping to a virtual machine that you have changed the hostname of still do not work – even after rebooting the virtual machine.

Restart VirtualBox and your hostname change on the virtual machine will be visible from the rest of your networking through your DHCP server. So ping will finally produce the familiar, reassuring replies.

(This example assumes you are using VirtualBox and its bridged networking mode for the virtual machine you are changing the hostname for).

Leave a Reply

Your email address will not be published. Required fields are marked *