Skip to content

Instantly share code, notes, and snippets.

@usworked
Created January 21, 2021 14:38
Show Gist options
  • Save usworked/ca7e152c49baad556300423d960d309f to your computer and use it in GitHub Desktop.
Save usworked/ca7e152c49baad556300423d960d309f to your computer and use it in GitHub Desktop.
How to quickly setup Debian /etc/hostname and /etc/hosts
/etc/hostname and /etc/hosts are simple, but also not so simple...
How to quickly setup Debian /etc/hostname and /etc/hosts
1) Edit /etc/hostname.
There are 2 valid ways to set this up.
Either make it:
machine, i.e. a simple, one word name for this processor.
For example, 'joe' might be your self-chosen machine name.
Note this will require an alias in /etc/hosts, further described below.
OR
machine.domain.net, i.e. a Fully Qualified Domain Name (FQDN).
For example, 'joe.xyz.com'.
(This is assuming you have a DNS Host 'A' record pointing from joe.xyz.com to your joe's IP address, i.e. at your machine named joe. Normally you set this record up where you purchased your domain name. More on that in a moment.)
Once /etc/hostname is set, then the next step is to get the commands hostname --all-fqdn and dnsdomainname to work properly, in that order, as follows:
2) Edit /etc/hosts.
In it have a line for this host's FQDN, e.g. 'machine.domain.net', prefixed with its this machine's IP address (possibly from a dynamic IP address server) like this:
# IP FQDN (CANONICAL_HOSTNAME) ALIASES
#-------------- --------------------------- ------------------------
192.168.1.14 Joe.xyz.com joe
First, note that upper or lower case don't matter here. Tip: Having some caps on, however, allows us to notice where things come from in the next steps. So I have capitalized Joe.
Also note that for the simple hostname (i.e. /etc/hostname = 'machine') to correctly generate a FQDN, an alias named 'machine' must be present in /etc/hosts. That's why the alias joe is there. (BTW, it's ok to have that alias even if you're not using it.) Otherwise this alias isn't needed, nor used.
Also note that 'domain.net' must be resolvable, i.e. there must exist an A record for it in DNS. This is what is called 'resolvable' (i.e. by the DNS resolver).
3) Now install these new setting from /etc/hostname and /etc/hosts into the kernel, by running:
$ /etc/init.d/hostname.sh # Or by rebooting
4) Test hostname
$ hostname # Reports the hostname previously read from in /etc/hostname.
joe
OR
$ hostname
joe.xyz.com
Tips:
Notice that this is not what /etc/hostname currently is, like if you edit it again right now, but rather what was read previously by the /etc/init.d/hostname.sh script, possibly as a result of a reboot.
Also notice that the 'j' is lower case, so this suggests it is coming from /etc/hostname, not /etc/hosts.
5) Test FQDN
$ hostname --all-fqdn #reports the FQDN(s) matched in /etc/hosts.
Joe.xyz.com # It searches /etc/hosts for the joe in /etc/hostname.
# Notice that the 'J' is uppercase.
# This suggest it came from /etc/hosts.
6) Test DNS domain name
$ dnsdomainname #reports the computed DNS doman name
xyz.com # (i.e. the right most part of the FQDN;
# the part to the right of the first, left most, dot)
# This is coming from Joe.xyz.com in /hosts,
# after 'Joe.' is removed.
Now this should make setting up a web and email server easier.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment