By the end of this article you should be able to answer the following questions:
$ hostname -s
$ hostname -f
$ hostname -I
$ hostnamectl status
$ hostnamectl set-hostname puppet01.local
All machines in a network have two ways to identify itself to the other machines on the network, they are the machine’s ip address and it’s name (aka hostname). The hostname is your machine’s name. You can view this name, using the hostname command:
$ hostname -s puppetmaster
This is machine’s short name. It’s full name is:
$ hostname -f puppetmaster.local
This full name is more commonly known as the machine’s Fully Qualified Domain Name, (aka FQDN).
To view a machine’s ip address we can do:
$ hostname -I 192.168.1.124
Another way to view your machine’s hostname is by using the hostnamectl command:
$ hostnamectl status Static hostname: puppetmaster.local Icon name: computer-vm Chassis: vm Machine ID: bb2262658ee64941afea091071b78f45 Boot ID: c59311743d9940b1981d065179eeccf3 Virtualization: oracle Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-229.14.1.el7.x86_64 Architecture: x86_64
You can change your machine’s hostname by editing the following file:
$ cat /etc/hostname puppetmaster.local
Or you can use the hostnamectl command:
$ hostnamectl set-hostname puppetmaster.local.test $ cat /etc/hostname puppetmaster.local.test
As you can see the hostnamectl command just edits the same file behind the scene.
Domain Name Servers (DNS)
Hostnames are human friendly names for a machine. However machines can’t identify themselves on a network using hostnames. Instead they have to identify themselves using an IP number. Machines use IP numbers pretty much the same way that phones connect to each other through phone numbers.
If we try to connect to a machine, e.g. via ssh, then we normally use it’s fqdn:
That’s because ip numbers are too difficult to remember. However before a connection to puppetagent01.local can be established, the fqdn will first need to be resolved to an ip number. This resolution can be done locally on your machine by adding an entry to the
$ cat /etc/hosts 127.0.0.1 localhost 127.0.1.1 puppetmaster.local puppetmaster 192.168.50.10 puppetmaster puppetmaster.local 192.168.50.11 puppetagent01 puppetagent01.local 192.168.50.12 puppetagent02 puppetagent02.local
If your machine can’t find a matching entry here, then your machine will contact the dns server to resolve it instead.
A dns server is a server that receives requests for an ip address for a given a fqdn. The dns server then sends back the ip number.
The dns server’s details is specified in the
$ cat /etc/resolv.conf # Generated by NetworkManager search local nameserver 10.0.2.3 nameserver 184.108.40.206
This file is actually not used anymore and is only used internally be the NetworkManager. So please don’t make any changes to this file, otherwise it will get overwritten by NetworkManager service.
This file is actually combined list of all the dns entries specified in the individual ifcfg-* config files. We will cover these files later.
Finally you can query a dns server using nslookup command.
$ nslookup google.com Server: 10.0.2.3 Address: 10.0.2.3#53 Non-authoritative answer: Name: google.com Address: 220.127.116.11
Also, here is an interesting setting:
$ cat /etc/nsswitch.conf | grep "^hosts:" hosts: files dns myhostname
This shows the order in which dns lookup occurs, first “files” which refers to /etc/hosts, then it is the dns server.