Change Hostname on Debian/Ubuntu

Sometimes happen you want to change the hostname for your machine. Debian based systems (Debian/Ubuntu) have the hostname information saved in this file /etc/hostname . This is the file location from where the system at the boot time is reading and set it up using the init script that is located in /etc/init.d/hostname.sh .

You can edit the file /etc/hostname very easy and apply your desired change for hostname on the system

Steps:

  1. sudo gedit /etc/hostname
  2. make the change of hostname and save the file
  3. sudo /etc/init.d/hostname.sh start
All changes are now applied and you should have now a new hostname on the system. To verify that the changes really have taken place run these commands:
  1. sudo hostname   {shows the current hostname on the system}
  2. sudo hostname -f   {shows you hostname.localdomain information}
  3. sudo hostname -d  {shows you localdomain information }
  4. sudo hostname -i {shows ip address for the hostname}
hosts
If any of the information for localdomain does not match you can edit/correct this information in this file /etc/hosts

To make changes in /etc/hosts:
  1. cd /etc
  2. sudo gedit hosts
  3. make the correct changes {127.0.0.1 hostname  and 127.0.0.1 hostname.localdomain}
  4. save the changes
Additional switches for hostname are:
  • -s = short hostname
  • -a = alias name
  • -i = ip address for the hostname
  • -I = all ip addresses for the hostname
  • -d = domain name for the hostname
  • -f = FQDN, all long hostname
Now you re-run the commands: hostname, hostname -i, hostname -d and hostname -f   — you should have now the correct hostname and localdomain on your system