Hosts file: What is it, why is it useful & how to edit it?
Aug 4, 2021 17:00 · 817 words · 4 minute read
Operating systems like Linux, Windows, and macOS have a hosts
file. This file is used to manually map a domain name (e.g. example.com
, or hello.world.local
) to an IP address (e.g. 192.168.1.14
, or 127.0.0.1
). An individual entry looks like this:
192.168.1.14 google.com
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 hosts
file 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. app.yourdomain.com
and api.yourdomain.com
.
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 hosts
file.
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+S
and Ctrl+X
are the most important ones for editing the hosts
file).
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
hosts
file’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
), 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
hosts
file 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 thehosts
file 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
hosts
file 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
hosts
file: 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. ↩︎
- You are in control
- My Favorite Shortcuts for VS Code
- Customizing my shell: From bash to zsh to fish
- JSON5: JSON with comments (and more!)
- jq: Analyzing JSON data on the command line
- Get Total Directory Size via Command Line
- Changing DNS server for Huawei LTE router
- Notes on Job & Career Development
- Adding full-text search to a static site (= no backend needed)
- Generating a random string on Linux & macOS