Operating systems like Linux, Windows, and macOS have a
hosts file. This file is used to manually map a domain name (e.g.
hello.world.local) to an IP address (e.g.
127.0.0.1). An individual entry looks like this:
If you add this line to your
hosts file and then open google.com in your web browser, your browser won’t do a request to an actual Google server, but will send a request to the server behind
192.168.1.14 ⇒ entries in the
hostsfile override normal DNS (Domain Name Service) resolution.
Why is it useful?
One common use case: Suppose you are developing a web application that should run on a virtual machine (e.g. on AWS EC2, or on Hetzner Cloud). The web application is split in two microservices, an app (e.g. a frontend app using React) and an API (e.g. a backend using Node.js & Express). Each part should be accessible on its own subdomain, e.g.
Now, one challenge in software engineering is to keep your development and production environments very similar, so that bugs already show in development.
In production, you will likely rent a virtual machine with a public IP address & rent a domain. Then, you can modify the domain records & add an A-record for each subdomain, so each subdomain points to the virtual machine’s IP address.
One way to reproduce this setup on your local machine is using virtual machines.1 You can e.g. use VirtualBox, and set up a machine that resembles your production machine very much. The machine will then be available from your host system via an IP address.
And here comes the
hosts file: As in production, you want to access the services via a domain - and, instead of setting up a public domain to point to your local virtual machine’s IP address (this is possible, but this would be quite a waste), you can add the “domain ⇒ IP” mappings to your
Advice: While you can add any domain to your
hosts file, it is generally recommended to use domains with ending
.local for local development. This top level domain ending is guaranteed to be never available as a domain for sale. In comparison, when configuring a domain à la
.dev as your local development domain, you will lock yourself out from visiting the real site behind this domain.2
So, following up on our app & API example earlier, the two hosts file entries for local development could look like this:
192.168.1.14 app.yourdomain.local 192.168.1.14 api.yourdomain.local
How to edit it?
On Linux and macOS: The
hosts file is located at
/etc/hosts. The easiest way is to open up a terminal and then execute:
sudo nano /etc/hosts
This opens up the nano command line text editor & you can start editing the file. See e.g. this nano cheatsheet for how to use it (hint:
Ctrl+X are the most important ones for editing the
On Windows: Things are a little bit more complicated here. The
hosts file is located at
%windir%\system32\drivers\etc\hosts. You need to know two things:
- How to navigate to the
hostsfile’s location: Open up the file explorer (shortcut: “Windows key + E”), click in the explorer’s address bar (the rectangle left from the search field), paste the directory path (
%windir%\system32\drivers\etc\hosts), and press “Enter”.
- How to edit it: You need to edit the file with admin privileges, or else the file changes won’t be saved. To do that, open up a text editor with admin privileges (e.g. press the Windows button, then type “Notepad”, right click the program’s icon and click “Open as admin”). Then e.g. drag & drop the
hostsfile into your editor window, or open the file via the editor’s “Open” dialog. Advice: Notepad++ (a much better text editor than the standard Windows Notepad) has this nifty feature that you can simply open the
hostsfile in non-admin mode - and when you want to save the file, Notepad++ will suggest to restart itself in admin-mode, so the changes can be saved (no worries, the changes won’t get lost with the restart).
I hope this post has successfully shown what a
hosts file is, why it can be useful & how to edit it! 🤓
An even nicer way is to use Docker & Docker Compose, e.g. with the awesome image nginx-proxy as reverse proxy service. When using Docker for development, you want to use the
hostsfile the same way as described here with virtual machines. But this is out of scope for this post. ↩︎
Actually, that is a second nice use case for the
hostsfile: Ad-blocking. How? By adding the domains of common advertising & tracking services, and let them resolve against 0.0.0.0, which is a non-routable IP address and causes the request to fail immediately. You can find publicly available ad-blocking hosts lists, e.g. StevenBlack/hosts with over 18k stars on GitHub. ↩︎
- Linux (Debian/Ubuntu): Finding out whether a package is installed
- Windows: Adding programs to autostart
- Vagrant & VirtualBox shared folders: Getting symlinks to work on a Windows host
- SSH: Finding out ECDSA key fingerprint (both server-side & client-side)
- PowerPoint: Inserting Source Code with Syntax Highlighting
- Recommendation: Deep dive guide on web server fundamentals
- Where are globally-installed Go tools located & how to execute them (when using Go similarly to 'npm install -g')
- Compilation of modern CLI Tools (Unix, Shell, Terminal)
- (Linux) file system basics
- How-to: Enter your SSH key passphrase only once per terminal session