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.
Internet standards for protocols mandate that component local host names may contain, only:
- the ASCII letters
a
throughz
(in a case-insensitive manner); - the digits
0
through9
; and - the hyphen (
-
).
No other symbols, punctuation characters, or white space are permitted.
Practice Tip: In addition to the above technical requirements, the only practical requirement of a server's hostname, for your environment(s), is that it should be something unique to the other servers within a particular domain.
Hostnames are composed of series of labels concatenated with dots, as are all domain names. For example, let's break mail.google.com
into its component parts:
mail
is the host or local hostname; andgoogle.com
is the domain or parent domain name.
Each label must:
- be between 1 and 63 characters long; and
- the entire hostname (including the domain & delimiting dots) has a maximum of 255 characters.
This article assumes that you are familiar with Step Seven — Log In To Your Droplet
of How To Create Your First DigitalOcean Droplet Virtual Server.
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
.
So long as the above parameters are complied with, one can use just about any name as a hostname. Many server admins. use planets, places or loosely-labeled abbreviations of a particular server's basic purpose, e.g., pbx
, web1
, web2
, mail
, ns1
(for nameserver) and so on. Feel free to have fun with hostnames, if you'd like.
A particular computer's hostname can be changed at any time. To set the initial hostname or subsequently change it, execute the following commands in a terminal or shell session (obviously, you can use whichever text editor you wish; but this guide assumes that you have installed the vim text editor):
sudo vim /etc/hostname
sudo vim /etc/sysconfig/network
- Then, tap on the
i
key and use the arrow keys on your keyboard to navigate the text area; - Next, enter the hostname of your choice; and
- To save & exit, tap the
Esc
key, on your keyboard, followed by these keystrokes::
,w
,q
, and, finally,Enter
.
If it exists, edit the file
/etc/default/dhcpcd
and comment out theSET_HOSTNAME
directive, by executing:
sudo vim /etc/default/dhcpcd
Then, insert the
#
symbol at the beginning of the line that begins withSET_HOSTNAME=
, as shown, below:
#SET_HOSTNAME='yes'
Finally, execute:
sudo service hostname restart
You can verify that the hostname was properly set by, again, executing:
hostname
In a terminal or shell, execute:
sudo vim /etc/hosts
Then, tap on the i
key and use the arrow keys on your keyboard to navigate the text area. Modify your hosts
file so that it resembles the following (obviously, substituting the hostname
, yourdomain
, tld
, and YourIP
values with your own):
127.0.0.1 localhost.localdomain localhost
127.0.1.1 hostname.yourdomain.tld hostname
YourIP hostname.yourdomain.tld hostname
To save & exit, tap the Esc
key, on your keyboard, followed by these keystrokes: :
, w
, q
, and, finally, Enter
.
You can verify that the FQDN was properly set by, again, executing:
hostname -f
If you want your remote server to be reachable over the internet via its FQDN, then you need to create the relevant DNS records. See How To Set Up a Host Name with DigitalOcean.
- Initial Server Setup with Ubuntu 12.04 | DigitalOcean
- Ubuntu Server 12.04 LTS Guide
- Initial Server Setup with CentOS 6
- CentOS Wiki