1. Overview

It’s often useful to associate a hostname with an IP address. While DNS does this for us for internet hosts, we may also need to do it for specific IP addresses in our local network, including IP addresses assigned to our machine.

Our machine’s IP address must be associated with the hostname. Otherwise, tools and processes that reference the machine using the hostname will return an error while attempting a name resolution.

In this tutorial, we’ll discuss how to associate an IP with a hostname.

2. Adding an IP Address to a Hostname Through the /etc/hosts File

The /etc/hosts file is a record of local IP addresses and their aliases. So, when we want to name an IP address, we edit the /etc/hosts file.

Let’s look at a typical hosts file:

$ sudo cat /etc/hosts
127.0.0.1       localhost
192.168.56.4    linux

2.1. Associating a Hostname With an IP Address

To associate our IP address – 192.168.56.4 – with our hostname – baeldung – we can edit the hosts file:

127.0.0.1 localhost
192.168.56.4 linux
192.168.56.4 baeldung

The hosts file allows duplicate entries, but it also allows us to put multiple host names on the same line:

127.0.0.1 localhost
192.168.56.4 linux baeldung

2.2. Associating a Remote Machine’s IP Address to an Alias

The hosts file also allows us to name the IP of a remote machine within the same private network.

Say we had a remote computer on the network whose IP address is 192.168.56.5. We alias it as computer2:

127.0.0.1 localhost
192.168.56.4 linux baeldung
192.168.56.5 computer2

So, we can now use computer2 in place of 192.168.56.5.

2.3. Mapping an IP Address to a Fully Qualified Domain Name

The hosts file can act as an alternative to a DNS server for us.

For example, we can map an IP address to the name remote.baeldung.com:

127.0.0.1 localhost
192.168.56.4 linux baeldung
192.168.56.5 computer2 remote.baeldung.com

With that, we’ve configured our machine to use computer2 as remote.baeldung.com.

2.4. Other Uses of the hosts File

Besides serving as a name record for processes and tools on a machine, we can use the /etc/hosts file for:

  • Testing a server in development – for one, it could come in handy when testing an SSL certificate
  • Testing DNS changes, especially when cached DNS records are getting in the way – it is an alternative to flushing the DNS cache
  • Assessing a website in a local environment when DNS propagation is taking time
  • Checking for security breaches
  • Testing and debugging servers when we cannot use localhost as the test URL

3. Checking Our Hostname’s IP Association

On a Debian system, running any command with sudo will show us an error message if our hostname has no IP:

$ sudo date
sudo: unable to resolve host baeldung: Name or service not known
Tue 28 Feb 2023 22:45:39 WAT

Here, we get an error – “unable to resolve host baeldung: Name or service not known“.

Using hostname -i is an easier way to check if our hostname is associated with an IP on a Debian system :

$ hostname -i
hostname: Name or service not known

The -i flag should return the IP address. But since no IP is assigned, we get the output above.

On Red Hat systems, the hostname may be assigned the eth0 interface’s IP by default. So, running hostname -i may not give us the same output as above. Instead, we may have to check the hosts file manually.

We can also check the IP addresses of all our host’s network interfaces:

$ hostname -I
10.0.2.15 192.168.56.4 172.17.0.1

4. Conclusion

In this article, we saw various ways to add IP addresses and hostnames to our hosts file.

We also saw how to check our host’s IP associations.