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’re going to have to map an IP address to a hostname in Linux – this might happen when you’re deploying a Kubernetes cluster or a Docker Swarm. Or maybe you want to make it easier for you to point a web browser to an internal site or service.
Instead of typing 192.168.1.11, you could type the hostname you’ve mapped in /etc/hosts. Although this is a pretty basic idea, it’s one that you’ll use quite a bit as an admin or a developer. Let me show you how it’s done. Note: You’ll need a running instance of Linux and a user with sudo privileges.
The first thing you’ll do is log into Linux and then open the necessary file for editing with the command sudo nano /etc/hosts. When prompted, type your sudo password.
You’ll already see a few entries in place. The two at the top are for IPv4 addresses that map the loopback address to localhost and your hostname; the entries below that are the same but for IPv6. At the bottom of the file is where you’ll add new entries.
Let’s add an entry that maps IP address 192.168.1.11 to the invoiceplane name. That entry will look like 192.168.1.11 invoiceplane. What we’ve done is mapped the IP address to a hostname. 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 need. Save and close the file.
You can verify it’s working by issuing the command ping invoiceplane, which should ping the address 192.168.1.11. You can even use the nickname mapped in /etc/hosts in your web browser by pointing it to http://invoiceplane or, in my case, http://invoiceplane/invoiceplane.
And that’s all there is to editing the Linux hosts file and mapping IP addresses to names that wouldn’t otherwise have been visible via regular DNS.
- Karlston
- 1
Recommended Comments
There are no comments to display.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.