How to Change the Hostname in Linux

The Linux system hostname is important.  It will  help identify a given system within a larger network. It can also be used to set local and external DNS entries.

To find out the current hostname of your linux system, simply type the hostname command:

test@test-VirtualBox:~$ hostname
test-VirtualBox


In this particular example, the current hostname is “test-VirtualBox.

To change the hostname on your local system, two changes are requird.

  1.  The /etc/hostname file.
  2.  The /etc/hosts file

Changing the hostname in /etc/hostname

You will need superuser (root) access.  However you should never login as root directly.  Rather, use the su command.

test@test-VirtualBox:~$ whoami
test
test@test-VirtualBox:~$ sudo su -
root@test-VirtualBox:~# whoami
root

Now you can use vi or nano editors to change the /etc/hostname file.

vi /etc/hostname

 

Chane the hostname to the desired name, in this case serverV-10!

Save and exit!

Changing the hostname in /etc/hosts

While still logged in as root, edit the /etc/hosts file.

Here change all instances of the old hostname.  In this case, the second line should be changed from test-VirutualBox to serverV-10.

Once these two changes are made, you can reboot the system.

Reboot and Verify

Now reboot the system.  Once the system is back up, simply issue the hostname command again.

test@serverV-10:~$ hostname
serverV-10

 

You can also do a ping by hostname:

test@serverV-10:~$ ping serverV-10
PING serverV-10 (127.0.1.1) 56(84) bytes of data.
64 bytes from serverV-10 (127.0.1.1): icmp_seq=1 ttl=64 time=0.026 ms
64 bytes from serverV-10 (127.0.1.1): icmp_seq=2 ttl=64 time=0.048 ms
64 bytes from serverV-10 (127.0.1.1): icmp_seq=3 ttl=64 time=0.070 ms
64 bytes from serverV-10 (127.0.1.1): icmp_seq=4 ttl=64 time=0.066 ms
64 bytes from serverV-10 (127.0.1.1): icmp_seq=5 ttl=64 time=0.034 ms

 

If these two tests work, then your hostname has been correctly changed on your  system.

 

Loading