How To Change Hostname In Ubuntu 18.04 Without Restart

This tutorial for those who want to change hostname in Ubuntu server or desktop without restarting the system. In computer networking, a hostname is a label that is assigned to a device connected to a computer network and that is used to identify the device in various forms of electronic communication, such as the World Wide Web (WWW). Hostnames may be simple names consisting of a single word or phrase, or they may be structured.

How To Change Hostname In Ubuntu 18.04 Without Restart

How To Change Hostname In Ubuntu 18.04 Without Restart

Change Hostname In Ubuntu Temporarily

It will work until you reboot your system.

To do so, in terminal/command console, execute the following command

sudo hostname NEW_NAME_HERE


My current hostname in Ubuntu is itsmarttricks, to check your hostname in Ubuntu execute following command in terminal.


Let’s change the hostname now, as I already told my current hostname in Ubuntu is itsmarttricks and I will change it as “SuperUser“.

To do so, in terminal I will execute the following commands:

sudo hostname SuperUser

Change Hostname In Ubuntu Permanently

We can do this by editing hosts and hostname file which can be found here “/etc/hosts” and “/etc/hostname”.

Open the terminal and execute the following commands:

gksudo gedit /etc/hostname /etc/hosts

You might need to install gksu first via command sudo apt install gksu.

For Ubuntu Server, use following commands instead (Press Ctrl+X, followed by Y, and then Enter to save changes):

sudo nano /etc/hostname
sudo nano /etc/hosts

When the files open, set the new hostname:

  • /etc/hostname is a simple one-line file, change the name to whatever you want.
  • /etc/hosts maps IP addresses to host names, change the name in the second line and make it SAME to the name in /etc/hostname.

Without restarting your machine, just run the command below to restart the hostname service to apply changes:

systemctl restart systemd-logind.service

There is also hostnamectl command with the set-hostname flag, which will alter the pretty, the static, and the transient hostname alike:

hostnamectl set-hostname NEW_NAME_HERE

Problem: After changing the hostname in Ubuntu, gedit and other graphical apps use X11/Mir protocol won’t launch from the terminal via,gksudo you’ll get something like below:
Failed to connect to Mir: Failed to connect to server socket: No such file or directory
Unable to init server: Could not connect: Connection refused
(gedit:20511): Gtk-WARNING **: cannot open display: :0

To fix this error:

Run commandxauth list to check authentication cookie.
add a matching cookie for the new hostname:

xauth add "NEW_HOSTNAME/unix:0" MIT-MAGIC-COOKIE-1 cookie-id-here

Also Read-

»Top Basic Linux Commands for Beginners.

»Step By Step Installation Of Ubuntu 17.04 With Screenshots.

This is how we can change the hostname in Ubuntu without restarting. If you found this tutorial helpful then Like and Share. If you got something else then feel free to comment.

Mangesh Dhulap

This is Mangesh Dhulap the Founder and Editor of IT SMART TRICKS have 6+ years of Industrial Experience. We expect from our visitors to like, share, and comment on our posts.

You may also like...

Leave a Reply

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

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