How to Change Hostname / Computer Name in Ubuntu 15.04

Change hostname Computer name in Ubuntu

This is a quick tutorial for Ubuntu Desktop and Server users who want to temporarily or permanently change their hostnames or computer names.

Change Hostname temporarily:

Log in to command console, or open terminal from the Dash/Menu or by pressing Ctrl+Alt+T, and run:

sudo hostname NEW_NAME_HERE


Above command changes the hostname until next reboot. The change won’t be visible immediately until your re-open the console/terminal.

Change Hostname permanently:

Edit both /etc/hosts and /etc/hostname files, and change their names to another.

For Desktop users, run command (install gksu from software center if need):

gksudo gedit /etc/hosts /etc/hostname

It opens both config files in Gedit Text Editor, set your NEW Hostname in both tabs by replacing existing ones (must be same).


For Server edition, run command sudo nano /etc/hosts and sudo nano /etc/hostname one by one to edit the files. To save changes, press Ctrl+X -> Type y -> and finally hit Enter.

Finally restart computer or run sudo service hostname restart to apply changes.


For Ubuntu Desktop, after changing the Hostname/Computer name, gedit or other apps that use the X11 protocol won’t open from terminal and output below error:

** (gedit:27373): WARNING **: Could not open X display
No protocol specified
error: XDG_RUNTIME_DIR not set in the environment.

This can be fixed by below steps:

1. Run command to get authentication cookie:

xauth list

It outputs something like this:

trusty/unix:0 MIT-MAGIC-COOKIE-1 b60c44ca77d2bc8c7656142175fd1130

2. Add a matching cookie for new hostname via command:

xauth add "NEW_HOST_NAME/unix:0" MIT-MAGIC-COOKIE-1 b60c44ca77d2bc8c7656142175fd1130

So that the xauth list command prints something below:

trusty/unix:0 MIT-MAGIC-COOKIE-1 b60c44ca77d2bc8c7656142175fd1130
NEW_HOSTNAME/unix:0 MIT-MAGIC-COOKIE-1 b60c44ca77d2bc8c7656142175fd1130

About ml

ml is a part time stay-at-home dad who've been using Ubuntu Desktop for a few years. He writes in the free time and wishes to share some useful tips with Ubuntu beginners and lovers.