How To Change Hostname on Ubuntu

Hostname is a name associated to computer which is connected to network. Hostname is used to identify this computer over the network. During setup process of Ubuntu you are asked to specify hostname. But later on you can change it. Here is how.

Change a hostname with restart

Follow next steps to change host name of your computer. You need to edit those two files – /etc/hostname and /etc/hosts. First file is a simple one line file that just contains name of the host. Edit it with your favorite editor – vi or gedit.

howopensource@esprimo:~$ sudo gedit /etc/hostname

Ubuntu Change Hostname GEdit

Second file /etc/hosts maps IP addresses to host names. So you need to edit it also, because some strange errors will start to appear.

howopensource@esprimo:~$ sudo gedit /etc/hosts

Ubuntu change etc hosts

Finally you should restart your Ubuntu box.

sudo reboot

Change a hostname without a reboot

If it is a server and you cannot reboot it – you can use command hostname. Without parameter command simply prints hostname to the terminal. With parameter command sets specified parameter to a new hostname. Note that command does not change file /etc/hostname and /etc/hosts. You still need to edit both files manually.

NOTE: You need to perform the same steps as above but instead to restart just use command hostname

howopensource@esprimo:~$ hostname
howopensource@esprimo:~$ sudo hostname esprimo2
howopensource@esprimo:~$ hostname

Command to change hostname

Here is the output of those two file:

howopensource@esprimo:~$ more /etc/hostname 
howopensource@esprimo:~$ more /etc/hosts	localhost	esprimo

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Ubuntu Print and Change Hostname

Ubuntu special command hostnamectl

From Ubuntu 13.04 there is a new command that can be used together with the old one and does the similar job. Once Ubuntu migrate to Systemd – most probably the old command will disappear and the new one will replace it.

howopensource@esprimo:~$ hostnamectl --help
hostnamectl [OPTIONS...] COMMAND ...

Query or change system hostname.

  -h --help              Show this help
     --version           Show package version
     --transient         Only set transient hostname
     --static            Only set static hostname
     --pretty            Only set pretty hostname
  -P --privileged        Acquire privileges before execution
     --no-ask-password   Do not prompt for password
  -H --host=[USER@]HOST  Operate on remote host

  status                 Show current hostname settings
  set-hostname NAME      Set system hostname
  set-icon-name NAME     Set icon name for host
  set-chassis NAME       Set chassis type for host

Here is how to see the hostname using new command hostnamectl. Command also displays some other useful information.

howopensource@esprimo:~$ hostnamectl status
   Static hostname: esprimo
         Icon name: computer-desktop
           Chassis: desktop
        Machine ID: 813e56ef1c4f171bda95b46b5448007c
           Boot ID: c719696ace224abf91fc52d6d9f83016
  Operating System: Ubuntu 14.10
            Kernel: Linux 3.16.0-29-generic
      Architecture: x86_64

Here is how to set a hostname without to restart server using new command.

howopensource@esprimo:~$ hostnamectl set-hostname esprimo2

And here is again how to display status and to check that update happens but using new and old command

howopensource@esprimo:~$ hostnamectl status
   Static hostname: esprimo2

howopensource@esprimo:~$ hostname

What happens if you forgot to change /ect/hosts

File /ets/hosts maps IP addresses and names of hosts. If you forgot to change hostname in that file then it cannot be resolved and strange errors will start to appear and main error will be unable to resolve host hostname.

howopensource@esprimo:~$ hostname
howopensource@esprimo:~$ sudo hostname esprimo2
howopensource@esprimo:~$ sudo gedit /etc/hostname 
sudo: unable to resolve host esprimo2
No protocol specified

** (gedit:3266): WARNING **: Could not open X display
No protocol specified
Failed to connect to Mir: Failed to connect to server socket: No such file or directory

(gedit:3266): Gtk-WARNING **: cannot open display: :0

Hostnames are human-readable names of computers that correspond to the address of a device connected to a network. They are used by various naming schemes and systems such as DNS (Domain Name System) and the meaning of hostname will vary according to the naming system used. Hostnames are typically used in an administrative capacity and may appear in computer browser lists, active directory lists, IP address to hostname resolutions, email headers, etc. More you can read from this article about hostnames from Wikipedia.