Set or Change a Linux Hostname

Introduction

A hostname is a label or nickname that is assigned to a computer connected to a network and that is used to identify the machine in various forms of electronic communication within an internal network. Hostnames are also important because they form part of a computer's Fully Qualified Domain Name (FQDN). Assigning a FQDN to a computer makes it reachable via the public Domain Name System (DNS), i.e. the Internet.

Current Hostname

To check your hostname, open a terminal or shell session and execute:

hostname

The current hostname, if any, will be displayed. Then, to check the existing FQDN, if any, execute:

hostname -f

which should yield a result such as localhost (which signifies that no FQDN is set) or pbx.yourdomain.tld. (Where pbx = the computername, yourdomain = your domain name, and tld = the top level domain i.e.“.com”)

Setting the Hostname

To change the computer name (hostname) on Ubuntu linux edit the file /etc/hostname. This is typically a single line text file holding the local computer name.

Next restart the hostname daemon:

sudo service hostname restart

Setting the Fully Qualified Domain Name (FQDN)

To set the FQDN edit the file /etc/hosts. This is a multi-line text file holding IP addresses and computernames to supplement or alter the computer's DNS information. Kind of like an “add-on phone book.” The format is:

  • IP address
  • FQDN
  • hostname

each separated by a TAB character.

127.0.0.1   localhost.localdomain   localhost
127.0.1.1   hostname.yourdomain.tld hostname
YourIP      hostname.yourdomain.tld hostname

In the example above yourIP would be replaced by the static IP address for your computer if it has one. If you do not have a static IP address do not include that line.

Last modified:: 2016/03/19 16:03