Home / Localhost / Hosts file

localhost vs 127.0.0.1, and how to edit the hosts file

What localhost means, why 127.0.0.1 sometimes works when localhost does not, and how to make names like myapp.test.

The hosts file maps the name myapp.test to 127.0.0.1 so the browser opens your local site

localhost is a name that points to your own computer. You can add your own names, like myapp.test, by editing the hosts file.

What runs on Hosts file

Make your own local name

  1. Open the hosts file as administrator. Windows: C:\Windows\System32\drivers\etc\hosts. Mac/Linux: /etc/hosts.
  2. Add a line: 127.0.0.1 myapp.test.
  3. Save, then flush the DNS cache (commands below).
  4. Open http://myapp.test. Your web server must have a site for that name.

Commands you can copy

Mac/Linux: edit the file
sudo nano /etc/hosts
Windows: flush DNS
ipconfig /flushdns
macOS: flush DNS
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Linux (systemd): flush DNS
sudo resolvectl flush-caches

If it still does not work

Name ends in .local and loads slowly on Mac
.local is used by Bonjour. Use .test or .localhost instead.
Changes have no effect
Flush DNS and restart the browser. Chrome can also cache: open chrome://net-internals/#dns and clear it.
Can’t save the file on Windows
Open Notepad as administrator, then open the hosts file from there.

Questions about Hosts file

What is the difference between localhost and 127.0.0.1?
localhost is a name that points to 127.0.0.1 (IPv4) and ::1 (IPv6). 127.0.0.1 is the IPv4 address itself.
Which domain ending should I use for local sites?
Use .test or .localhost. Avoid .local on macOS because it clashes with Bonjour.

Updated 2026-10-02

Free tools that help

Config generators for this

Related pages