How to change the hostname of your Linux machine without needing to restart

Image of a terminal prompt

hostnamectl method

Since Ubuntu 16.04 (and most recent Linux distros) use Systemd you can simply use the hostnamectl command to change hostname. To see current setting, run your Terminal just type the following command:
$ hostnamectl
Sample output:

   Static hostname: lappy686
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: 52ff3d3eefa19d99891c3cf4b70a138b
           Boot ID: fefe3aaf139855243a837001fedc7713
  Operating System: Ubuntu 18.04
            Kernel: Linux 4.16.0-33-generic
      Architecture: x86-64

To change hostname from lappy686 to ulng-laptop, enter:
$ hostnamectl set-hostname ulng-laptop

Type hostnamectl again to see that the hostname has updated.

Classic Method

If you don’t have such a new machine, then you can perform the following steps:

Type the following commands:
$ sudo hostname new-hostname-here

Next edit the /etc/hostname file and update hostname:
$ sudo nano /etc/hostname

Finally, edit the /etc/hosts file and update the lines that contain your old hostname:
$ sudo nano /etc/hosts

From:
127.0.1.1 old-host-name

To:
127.0.1.1 new-hostname-here

Save and close the file.

Related Post

RELATED:   Using sed to find and replace text files in Linux

One thought on “How to change the hostname of your Linux machine without needing to restart”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.