Minor Obsessions

In my tech bootcamp days, I tended to get sidetracked with things that weren’t strictly development-related. And one of those things was creating a fake “domain name” that everyone on the same WiFi network could access*. (I didn’t know for absolute sure that you could do it — I was just really, really sure it was possible.)

*This interesting conversation on Superuser.com seemed promising, but I did not want to set up dnsmasq.

So…. Is It Possible??

Sort of!

For this small post, I want to tell you about your /etc/hosts file. If you open up a terminal, you can see what’s in the file by typing cat /etc/hosts.

To demonstrate what it does, let us say you had set up an EC2 instance on AWS and gave it an elastic IP address, but didn’t feel like buying a domain name. At the same time, though, you also didn’t feel like typing every single time — and you definitely didn’t want to force your friends to! But is there really no other option?

(Spoiler: there is!)

If you use OSX or Linux, open up your /etc/hosts file in your preferred text editor and add this line:    comfortcat.meow

Now, open up a browser and go to comfortcat.meow/ (yes, add the slash at the end, or your browser might think you’re trying to do a web search).

Heyyyyy look! It’s your website!

Why do we have an /etc/hosts file?

The /etc/hosts file goes all the way back to the ancient ancestor of the Internet — ARPANET. This network lived from about 1969 to 1990, and was the very first to use the TCP/IP protocol suite we know and love today. It was mainly used by major universities to share and access research.

In a given local network (e.g. a university), a special HOSTS.TXT file was used to associate IP addresses with names that were shorter to remember. This file was usually maintained by a system administrator and shared to other members of the network. As more and more network nodes were added to ARPANET, however, the hosts file became quite cumbersome and difficult to maintain. In short, maintaining a gigantic text file did not scale very well with the rapid growth of ARPANET.

But in 1983, Paul Mockapetris invented the Domain Name System (DNS!), a distributed and dynamic naming system (which is what translates an address like “www.google.com” into the corresponding IP address). In other words, DNS is not centrally maintained like the old hosts file, and can grow and update itself autonomously — that’s what makes it so scalable.

While we don’t typically use hosts files anymore, however, it never actually went away. Most modern computers have a vestigial /etc/hosts file, which works basically the same way. Most of them have at least this line: localhost

So there you have it: Now you know what/etc/hosts does!

Epilogue: Can I make all my friends update their hosts files?

Technically, most people with a *nix operating system or a Mac can add a line to their /etc/hosts file. If you’re like me, you may be tempted to tell your friends to add a line to their hosts file so you don’t have to bother buying a domain name for your unwieldy IPv4 address — but beware! It may scare them. That’s alright, though — at least now you know you can do it on your own computer. :)