Hosts file: definition, location, and syntax (2026 guide)
Master the hosts file in 5 minutes: location on Mac/Windows/Linux, syntax, use cases (local dev, ad blocking) and common mistakes to avoid. Updated January 2026.
Locahl Team
Table of Contents
- What is the hosts file?
- A bit of history
- How does DNS resolution work?
- Hosts file location by system
- On macOS
- On Windows
- On Linux
- Hosts file syntax and format
- Essential syntax rules
- Concrete examples
- IPv4 vs IPv6
- Common hosts file use cases
- Local web development
- Testing before DNS migration
- Blocking unwanted sites
- Multi-environment management
- Common errors and solutions
- Error: "Permission denied"
- Error: Changes not taking effect
- Error: Invalid syntax
- Error: .local domains slow on macOS
- Tools to simplify management
- Best practices
- Organize your entries
- Back up regularly
- Use appropriate TLDs
- Conclusion
The hosts file is one of the oldest and most powerful tools in network computing. Present on all modern operating systems, it allows you to control DNS resolution locally on your machine. In this comprehensive guide, we'll explore everything you need to know about the hosts file in 2026.
What is the hosts file?
The hosts file is a plain text file that maps domain names (like google.com or mysite.local) to IP addresses. It's the ancestor of the DNS system as we know it today.
A bit of history
Before the invention of DNS in 1983, all computers connected to ARPANET (the precursor to the Internet) used a centralized hosts file to resolve names to IP addresses. This file was manually maintained by the Stanford Research Institute and distributed to all hosts on the network.
Today, although DNS has replaced this centralized system, the hosts file remains present on all operating systems to allow local overrides of DNS resolution.
How does DNS resolution work?
When you type a web address in your browser, here's what happens:
- Your system first checks the local hosts file
- If the domain isn't found, it queries the local DNS cache
- Then it contacts the configured DNS server (usually your ISP's or a public DNS like 8.8.8.8)
- The DNS server returns the corresponding IP address
The hosts file has absolute priority over all other DNS resolution mechanisms. This is what makes it so powerful for local development and testing.
Simplify your hosts file management
Locahl lets you manage your hosts file visually, without touching the terminal. Automatic DNS flush, multiple environments, and backups included.
Hosts file location by system
The hosts file location varies depending on your operating system:
On macOS
/etc/hostsTo edit it, you'll need administrator rights:
sudo nano /etc/hostsOn Windows
C:\Windows\System32\drivers\etc\hostsOpen Notepad as administrator, then navigate to this file.
On Linux
/etc/hostsLike on macOS, use sudo to edit it:
sudo nano /etc/hostsTip: On all these systems, the hosts file is write-protected for security reasons. You must always have administrator rights to modify it.
Hosts file syntax and format
The hosts file format is simple but strict. Each line follows this structure:
IP_address domain_name [aliases...]Essential syntax rules
- One entry per line: each IP/domain association occupies its own line
- Separator: use spaces or tabs between the IP and domain
- Comments: lines starting with # are ignored
- Aliases: you can add multiple domains on the same line
Concrete examples
# Standard localhost
127.0.0.1 localhost
::1 localhost
# Local development projects
127.0.0.1 myproject.local
127.0.0.1 api.myproject.local
127.0.0.1 admin.myproject.local
# Block a site
0.0.0.0 distracting-site.com
0.0.0.0 www.distracting-site.com
# Redirect to a staging server
192.168.1.100 staging.mycompany.comIPv4 vs IPv6
The hosts file supports both IP address formats:
- IPv4: classic format like 127.0.0.1 or 192.168.1.1
- IPv6: modern format like ::1 (localhost) or 2001:db8::1
For maximum compatibility, often declare both:
127.0.0.1 mysite.local
::1 mysite.localCommon hosts file use cases
Local web development
This is the most common use of the hosts file for developers. Instead of accessing your projects via localhost:3000 or 127.0.0.1, you can create more realistic local domains:
127.0.0.1 myapp.local
127.0.0.1 api.myapp.local
127.0.0.1 cdn.myapp.localAdvantages:
- More readable and memorable URLs
- Simulation of multi-domain environments
- Cross-domain cookie testing
- More realistic CORS configuration
Testing before DNS migration
When migrating a site to a new server, you can test by temporarily pointing the domain to the new IP:
# Test the new server before changing official DNS
203.0.113.50 www.mysite.com
203.0.113.50 mysite.comOnly you will see the new server, while the rest of the world continues to access the old one.
Blocking unwanted sites
By redirecting domains to a non-existent address, you can block access to certain sites:
# Block distractions
0.0.0.0 facebook.com
0.0.0.0 www.facebook.com
0.0.0.0 twitter.com
# Block advertising
0.0.0.0 ads.doubleclick.netFor more on blocking, check our complete guide to blocking ads with the hosts file.
Multi-environment management
Freelancers and teams managing multiple projects can organize their entries by client:
# Client A
127.0.0.1 client-a.local
127.0.0.1 api.client-a.local
# Client B
127.0.0.1 client-b.localCommon errors and solutions
Error: "Permission denied"
Symptom: Unable to save the hosts file.
Cause: The file is write-protected.
Solution: Use sudo on macOS/Linux, or open your editor as administrator on Windows. For a detailed Mac guide, check our article How to edit the hosts file on Mac.
Error: Changes not taking effect
Symptom: You modified the file, but the browser still shows the old IP.
Cause: DNS cache wasn't flushed.
Solution: Flush your system's DNS cache.
On macOS:
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponderOn Windows:
ipconfig /flushdnsError: Invalid syntax
Symptom: Some entries work, others don't.
Solutions:
- Rewrite entries manually (avoid copy-paste)
- Check for invisible spaces
- Follow the format: IP then domain
Error: .local domains slow on macOS
Cause: macOS uses Bonjour/mDNS for .local domains.
Solution: Use .test or .localhost instead of .local.
Tools to simplify management
Manual hosts file management via terminal works but has drawbacks:
- Risk of syntax errors
- No automatic validation
- Hard to find entries in large files
Applications like Locahl offer a modern alternative with a visual interface, automatic validation, and one-click DNS flush.
Best practices
Organize your entries
Use comments to structure your file:
# ===================
# LOCALHOST
# ===================
127.0.0.1 localhost
# ===================
# PERSONAL PROJECTS
# ===================
127.0.0.1 blog.local
127.0.0.1 portfolio.localBack up regularly
Before any major modification:
sudo cp /etc/hosts /etc/hosts.backupUse appropriate TLDs
- .test: reserved by IETF, recommended
- .localhost: always local
- .local: can conflict with Bonjour on Mac
- .dev: real domain, requires HTTPS
Conclusion
The hosts file remains a fundamental tool for every developer in 2026. Its simplicity and effectiveness make it a valuable ally for local development, migration testing, and site blocking. Mastering the hosts file will save you precious time every day.
Ready to simplify your workflow?
Stop wasting time with the terminal. Locahl lets you manage your hosts file in a few clicks, with automatic validation and no risk of errors.
- Intuitive visual interface
- Automatic DNS flush
- Multi-environment management
- Automatic backups
- JSON Import/Export
Reader Reviews
"Finally a clear and complete guide on the hosts file. I understood everything in 10 minutes, even the IPv6 subtleties."
November 20, 2025
"Great article, very well structured. The section on common errors saved me a lot of time."
December 5, 2025
"Excellent reference guide. I consult it regularly for my local development projects."
January 8, 2026
Frequently Asked Questions
What exactly is the hosts file?
The hosts file is a system text file that maps domain names to IP addresses. It is checked before external DNS servers and allows you to locally redirect domains to specific IP addresses.
Where is the hosts file located on my computer?
On macOS and Linux: /etc/hosts. On Windows: C:\Windows\System32\drivers\etc\hosts. These locations are protected and require administrator rights to modify.
Why aren't my hosts file changes working?
The most common cause is DNS cache. After modifying the hosts file, you need to flush your system's DNS cache. On macOS: sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Can the hosts file block ads?
Yes, by redirecting advertising domains to 0.0.0.0 or 127.0.0.1, you can block ads at the system level. Lists like Steven Black's contain thousands of domains to block.
Does the hosts file work with IPv6?
Yes, the hosts file supports IPv6 addresses. The IPv6 equivalent of localhost is ::1. You can mix IPv4 and IPv6 entries in the same file.
How do I test if my hosts file changes worked?
Use the ping or nslookup command with the modified domain. For example: ping mysite.local should return the IP address you configured.
Does the hosts file take priority over DNS?
Yes, the hosts file is checked first during DNS resolution. If a domain is present in the hosts file, external DNS servers will not be queried for that domain.
Can I use wildcards in the hosts file?
No, the hosts file does not support wildcards (*.domain.com). Each subdomain must be declared explicitly. For wildcards, use dnsmasq or a similar tool.
Related Articles
Edit the hosts file on Mac: Terminal vs GUI (2026)
How to edit /etc/hosts on macOS without errors? Terminal (sudo nano) vs GUI comparison. Fix permission denied and DNS cache issues in 2 minutes.
Locahl Team
Using Hosts Files for Docker Development on Mac
Learn how to configure hosts files for Docker, docker-compose, and container networking. Map container services to local domains and streamline your Docker development workflow.
Locahl Team
Hosts File Setup for Laravel/WordPress Local Development
Complete guide to configuring hosts files for Laravel Valet, Herd, and WordPress local development. Learn custom domains (.test, .local), multisite configurations, and best practices.
Locahl Team