How To Change Hostname In Ubuntu 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.

Follow the below steps to Change Hostname In Ubuntu Without Restart:

Change Temporarily Hostname In Ubuntu:

It will work until you reboot your system.

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

sudo hostname NEW_NAME_HERE

Example:

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

hostname

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

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

sudo hostname SuperUser

Change Permanently Hostname In Ubuntu:

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 the 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 map IP addresses to hostnames, 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 the 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.

That’s all, In this article, we have explained How To Change the system Hostname In Ubuntu Without Restart. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.

Share this:

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.