How to Edit the Linux Hosts File

Moderator: Forum moderators

Post Reply
User avatar
Flash
Moderator
Posts: 897
Joined: Tue Dec 03, 2019 3:13 pm
Location: Arizona, U.S.
Has thanked: 46 times
Been thanked: 105 times

How to Edit the Linux Hosts File

Post by Flash »

https://www.techrepublic.com/article/ho ... osts-file/

by Jack Wallen in Developer
on July 12, 2023

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. [No need for sudo in Puppy Linux]

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. [Just go to /etc in Rox, right-click on /etc/hosts and select open as text. No sudo.]

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.....

Chaos coordinator :?
dancytron
Posts: 653
Joined: Fri Dec 13, 2019 6:26 pm
Has thanked: 422 times
Been thanked: 190 times

Re: How to Edit the Linux Hosts File

Post by dancytron »

Here is a github page with many host lists and a script that generates them.

https://github.com/StevenBlack/hosts

Post Reply

Return to “Network/Server”