Networking: DNS and hostnames

The course is part of this learning path

Linux Administration Bootcamp
Networking: DNS and hostnames

This course covers the essentials of networking with Linux. This course is part of the Linux Administration Bootcamp learning path, designed to get you up and running with Linux.

Learning Objectives

  • Understand the TCP/IP protocol and the most important aspects of IP networking
  • Learn how DNS, hostnames, and IP addresses are used in networking on Linux
  • Learn about DHCP, as well as status and dynamic addressing
  • Learn some of the most common tools you can use to perform network diagnostics

Intended Audience

  • Anyone with basic knowledge of Linux who wants to learn more
  • System administrators or IT professionals who want to learn more about networking on Linux


This is an intermediate-level course so some knowledge of Linux is expected. If you're just starting out, then try our Linux Fundamentals course first.


In this lesson, you will learn how to determine the current IP address or addresses in use on a Linux system. How to use the IP and ifconfig tools to configure and control the network interfaces on a Linux system. How to get and set the hostname of a Linux server. You'll also learn what DNS is and how to use the host and dig tools to resolve hostnames and IP addresses. You'll also learn how to associate names with IP addresses by making entries in the /etc/hosts file. Finally, you'll learn about the /etc/nsswitch.conf file and exactly what it's used for.

To show your current IP address, or to get a list of all the IP addresses in use on your system, run the IP command with the argument of address. With the IP command, you can use abbreviations. So instead of running IP address, you can run IP, space, addr or even IP, space, a. You can also be more explicit by running IP address show. Here are some sample output from the IP address command. You can see two devices listed, lo and eth0. The lo device is called the loopback device. This is a special virtual network interface that a Linux system uses to communicate with itself. The loopback device has an IP address of The other network device on this system is the eth0 device. This is an actual hardware device and it has an IP address of

Here's another command that you can use to display IP address information. It's the ifconfig tool. At this point, the ifconfig utility is considered to be deprecated, however, this little utility hasn't quite yet disappeared on modern Linux systems and may be around for quite some time to come. If this is a refresher for you or you're coming from a Unix background, you may already be familiar with the ifconfig command, but not it's newer replacement, the IP command.

In this lesson, you'll learn how to use both the IP and ifconfig commands. To display the IP addresses in use with the ifconfig command, execute it without any arguments. Here's some sample output from the ifonfig command. It lists two interfaces, eth0 and the loopback device. You'll notice that the output is slightly different from the IP command. However, it gets the job done by displaying the IP address, netmask and more. Just like with the IP command, you can see that the eth0 device has an IP address of and the loopback device has an IP of

A host is a device connected to a network. Since we are talking about TCP IP networking, a host in this case, is a device with an IP address. A hostname is simply a human readable name that corresponds to an IP address. Let's say we have a Linux server that will act as a web server in production. We can give that server a hostname, webprod01, for example, and refer to it by that hostname instead of its IP address, which might be something like A one-word hostname like this is sometimes called the short hostname or the unqualified hostname.

The primary purpose of DNS, which stands for domain name system, is to translate human readable names into IP addresses. Of course, DNS does the reverse as well. It can translate an IP address into a hostname. The fully qualified domain name, or FQDN, of a host also contains a domain name and a top level domain name. Each section of the FQDN is separated by a period or a dot. TLD stands for top level domain. And it's the right most portion of a DNS name. Common top level domains include .com, .net but there are actually hundreds of other top level domains.

A domain appears just to the left of a top level domain. This is often a company name, an organization name, or even a brand name. The FQDN or long hostname of our Linux server, would contain at least three strings separated by a period. For example, its FQDN could be However, domains can be further divided into sub-domains.

Let's say my company wants to use sub-domains to identify where a server is located. It could use a country domain such as and maybe even a state domain, something like If our web server were in New York, it's FQDN might actually be Sub-domains do not have to correspond to geographical regions, they can be anything the DNS administrator has configured. You can display the current hostname by using the hostname command or running uname, space, -n.

In this example, the host name is webprod01. If you want to display the fully qualified domain name, run hostname, space, -f. You can also temporarily change the hostname of a system by supplying it as an argument to the hostname command. However, to make this persist between reboots, you'll need to update the hostname configuration. This configuration varies slightly from distribution to distribution. For Ubuntu and Red Hat systems, edit the /etc/hostname file and place your desired hostname there. For earlier versions of Red Hat, you can edit the /etc/sysconfig/network file and set the hostname variable to the desired value.

If you want to look up or resolve a DNS name or an IP address, you can use the host or dig tools. In their most simplest forms, you specify the IP address or DNS name you want to look up as an argument to the command. On this screen is some sample output from the host command. The /etc/hosts file contains a list of IP addresses and hostnames. You can create entries in the host file by starting a line with an IP address, and then following it with a name or names you want to translate that IP address to.

The example on the screen uses multiple names but if you don't need or want to access a system by multiple names, you can simply list one name. After you have created an entry in the /etc/hosts file, you can start communicating with that IP address by using the name listed in the host file. This can be useful if you want to access computers that do not have DNS hostnames, for example. Also it's common to use /etc/hosts entries to override the DNS entry of a system.

For example, if you have a cluster of web servers, you could have a private network that only the web cluster members can access. You can create an entry for the members of the cluster in /etc/hosts and use their private address. Thus forcing network communications to go through the private network. It's important to note that the /etc/hosts is local to the system. Adding an entry to the /etc/hosts file does not add an entry into DNS. Here's an example /etc/hosts file.

You'll see the first line contains an entry for localhost. Remember that this is used by the loopback device for internal communications. The next line contains the public IP address of the system followed by the FQDN and then the short name. The third line contains a non-ratable IP address for webprod02. In this example, it's the private IP address of that system. There's another similar entry for webprod03 on the next line. The last line is an example that only contains one name, in this case, dbcluster.

Typically the /etc/hosts file is checked first before a DNS server is queried, but you can change this behavior by editing the /etc/nsswitch.conf file. NSS stands for name service switch, and it controls the order in which lookups are performed. The hosts line determines the order for name resolution. For example, if you have hosts, colon, files, DNS in the nsswitch.conf file, the /etc/hosts file will be searched first and then DNS. For example, if you have hosts, colon, files, DNS in the nsswitch.conf file, the /etc/hosts file will be searched first. If an IP address is found there, that IP address is used and the search stops. If it is not found, then DNS is queried. There are other services that can resolve hostnames as well. If you want to use NIS for name resolution, you can add it to the host line in the /etc/nsswitch.conf file.

In this lesson, you learned how you can see what IP addresses are assigned to the network interfaces on your Linux system. You also learned how to manually add IP addresses using the IP and ifconfig utilities. Next you learned how to set and get the hostname of a system. We also talked about DNS and using the host and dig utilities to resolve names and IP addresses. You learned how to make entries in the /etc/hosts file and how to control the order that name resolutions take place by using the /etc/nsswitch.conf file.

About the Author
Jason Cannon
Founder, Linux Training Academy
Learning Paths

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.

Covered Topics