In this step-by-step guide from Jack Wallen, learn how to access and modify the Linux hosts file to control the mapping of hostnames to IP addresses.
At some point in your career as a developer, you’ll need to map an IP address to a hostname in Linux – this may happen when you’re deploying a Kubernetes cluster or Docker Swarm. Or maybe you want to make it easy to point the web browser to an internal site or service.
Instead of typing 192.168.1.11, you can type the hostname that you have mapped in /etc/hosts. Although this is a very basic idea, it is an idea that you will use a lot as an administrator or developer. Let me show you how it’s done. Comment: You will need a running instance of Linux and a user with sudo privileges.
The first thing you will do is log in to Linux and then open the required file for editing with the command sudo nano /etc/hosts, When prompted, type your sudo password.
You will already see some entries. The two at the top are for the IPv4 addresses that map the loopback address to localhost and your hostname; The entries below are the same but for IPv6. At the bottom of the file you’ll add new entries.
Let’s add an entry that maps the IP address 192.168.1.11 to the name InvoicePlane. That entry would look like 192.168.1.11 invoiceplain. What we have done is mapped IP addresses to hostnames. You can also add a fully qualified domain name after the hostname such as 192.168.1.11 invoiceplane invoiceplane.monkeypantz.lan or whatever FQDN you want. Save and close the file.
You can verify that it is working by issuing the command ping invoiceplain, which should ping the address 192.168.1.11. You can also use an alias mapped in /etc/hosts in your web browser by pointing it to /invoiceplane , in my case.
And that’s all it takes to edit the Linux hosts file and map IP addresses to names that wouldn’t otherwise be visible through regular DNS.
Subscribe to TechRepublic How to do technical work on YouTube For all the latest tech advice from Jack Wallen for business pros.











