Change Hostname in Ubuntu 16.04 Without Restart

June 13, 2016 — 2 Comments

computer name ubuntu 16.04

Quick tutorial for those who want to change the Hostname (Computer name) in Ubuntu 16.04 Server or Desktop without a restart.

1. To change hostname temporarily so it works until you reboot the machine:

In terminal / command console, run the command:

sudo hostname NEW_NAME_HERE

2. To permanently change your computer name, you may edit the “/etc/hostname” and “/etc/hosts” files.

Open terminal and run command:

gksudo gedit /etc/hostname /etc/hosts

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

For Ubuntu Server, use two nano 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 your want.
  • /etc/hosts maps IP addresses to host names, change the name in second line and make it SAME to the name in /etc/hostname.

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

systemctl restart systemd-logind.service

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

hostnamectl set-hostname NEW_NAME_HERE

ISSUE: After changing hostname, gedit and other graphical apps use X11/Mir protocol won’t launch from 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 it:

run xauth list command 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


I'm a freelance blogger who started using Ubuntu 5+ years ago and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me on Google Plus or email to

2 responses to Change Hostname in Ubuntu 16.04 Without Restart

  1. Hi Ji,

    following command not working to take effect of changed hostname :

    systemctl restart systemd-logind.service

    Can you please update?


    • @Ravi

      Hi, it is the right command. If you’re not root you’ll need to use it with sudo though. Like so:.

      # sudo systemctl restart systemd-logind.service


      Thanks for the nice tutorial. Clear & concise

Leave a Reply


Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>