Hosts File Location: Find and Edit It on Any System
The hosts file, sometimes called the localhost file, is a plain text list mapping names to IP addresses. On Windows it sits at C:\Windows\System32\drivers\etc\hosts, while macOS and Linux keep it at /etc/hosts. Editing needs administrator rights, and a DNS cache flush makes the change apply immediately.
| System | Path | Flush the cache with |
|---|---|---|
| Windows 10 and 11 | C:\Windows\System32\drivers\etc\hosts | ipconfig /flushdns |
| macOS | /etc/hosts (a link to /private/etc/hosts) | sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder |
| Linux with systemd-resolved | /etc/hosts | resolvectl flush-caches |
| Android and iOS | Not editable without root or jailbreak | Use a private DNS or VPN app instead |
Updated
Before your computer asks a DNS server where a domain lives, it checks the hosts file. A line there wins over the internet, which makes it handy for previewing a site on a new server before you move the domain, blocking a host, or giving a local project a friendly name like myapp.test.
Edit the hosts file from the command line
Mac and Linux
Add the mapping, then flush the cache for your system.
sudo nano /etc/hosts
# add a line such as:
127.0.0.1 myapp.test
# macOS
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
# Linux (systemd-resolved)
resolvectl flush-cachesWindows (PowerShell as administrator)
Opens the file in Notepad with the rights needed to save it.
notepad C:\Windows\System32\drivers\etc\hosts
ipconfig /flushdns
What a good entry looks like
Each line takes an address followed by one or more names, such as 127.0.0.1 myapp.test api.myapp.test. Wildcards like *.myapp.test are not supported; list every subdomain or run a small local DNS tool if you need many.
Choose names ending in .test or .localhost for local work. Made-up endings that later become real top-level domains, as .dev did, cause certificate warnings and confusion.
Previewing a site move
When you connect a custom domain to a site built on We.Inc you point DNS records at us instead, so no hosts file edits are needed on visitors' machines.
How it works
- Open a text editor as administrator (Windows) or use sudo (Mac and Linux).
- Open the hosts file at the path for your system from the table.
- Add one line per mapping: the IP address, a space or tab, then the host name.
- Save, flush the DNS cache, then open the name in a fresh browser tab.
Tips
- On Windows, Notepad opened normally cannot save the file. Right-click Notepad, choose Run as administrator, then use File, Open and switch the filter to All Files.
- Windows Defender can flag hosts entries that redirect well-known domains. Mappings for your own test names are not affected.
- Browsers keep their own DNS cache. Chrome's can be cleared at chrome://net-internals/#dns if a change still does not show.
- Lines starting with # are comments. Keep the default 127.0.0.1 localhost and ::1 localhost lines, because removing them breaks local tools.
Frequently asked questions
Where is the localhost file on Windows?
At C:\Windows\System32\drivers\etc\hosts. The file has no extension. Paste that folder path into File Explorer's address bar to jump there directly.
Where is the hosts file on a Mac?
At /etc/hosts, which macOS links to /private/etc/hosts. Edit it in Terminal with sudo nano /etc/hosts, or reveal it in Finder with Go, Go to Folder and the path /private/etc.
Why is my hosts file change not working?
The usual causes are a cached DNS answer, a file saved as hosts.txt by the editor, or a browser using DNS over HTTPS, which skips the system resolver in some setups. Flush the cache, check the file name and test with ping.
Can I use the hosts file to preview a site before changing DNS?
Yes. Map your domain to the new server's IP, for example 203.0.113.10 example.com, and only your computer will load the new server. Remove the line once DNS is switched.
Related: What is localhost, DNS server not responding, DNS record types, Website hosting migration.
Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.
More in Guides
- How Much to Charge for Website Maintenance: A Worked Pricing Method | How-to Guide
- How to Add a Contact Form to Your Website | How-to Guide
- How to Add an Announcement Bar to Your Website
- How to Add a Back to Top Button
- How to Add a Blog to Your Website | How-to Guide
- How to Add a Cookie Banner to Your Website
- How to Add Crisp Chat to Your Website
- How to Add a Custom Domain to Your Website | How-to Guide
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App