How To Change Hostname In Ubuntu 17.10 Without Restart

Introduction

This tutorial for those who want to change the 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.

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

Example:

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

hostname

Lets change the host name now, as I already told my current hostname in Ubuntu is eTechRoom and I will change it as “SuperUser“.

Hostname In Ubuntu

Hostname

To do so, in terminal I will execute 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 following commands:

gksudo gedit /etc/hostname /etc/hosts

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

Recommended for you: How To Use Or What Is The Use Of .Bashrc File In Linux

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 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

There is 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

Problem: After changing the hostname in Ubuntu, 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 this error:

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

This is how we can change 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.

Leave a Comment