Software & OS

How to Edit the Hosts File on Windows, macOS and Linux

The hosts file maps host names to IP addresses on your own computer, and programs that use the operating system's resolver get those answers without asking a DNS server. Open it with administrator rights (C:\Windows\System32\drivers\etc\hosts on Windows, /etc/hosts on macOS and Linux), add a line such as 127.0.0.1 myapp.test, save, and the name resolves to that address straight away.

# Windows: open an elevated PowerShell, then
notepad C:\Windows\System32\drivers\etc\hosts

# macOS and Linux
sudo nano /etc/hosts

Make a copy of the file before you change it. Everything below about the hosts file on Windows was checked read-only on Windows 11; nothing in this article requires changing the real file to follow along until you are ready.

Where the hosts file is

OSPathWho can write it
Windows 10 and 11%WinDir%\System32\drivers\etc\hosts (usually C:\Windows\System32\drivers\etc\hosts)Administrators
macOS/etc/hostsroot, via sudo
Linux/etc/hostsroot, via sudo

The Windows file is called just hosts, with no extension. Microsoft's support article stresses this: "A hosts file can't have a file extension." If Notepad saves it as hosts.txt, Windows ignores it. You can see the permissions with icacls:

icacls C:\Windows\System32\drivers\etc\hosts

Example output (Windows 11):

C:\Windows\System32\drivers\etc\hosts NT AUTHORITY\SYSTEM:(I)(F)
                                      BUILTIN\Administrators:(I)(F)
                                      BUILTIN\Users:(I)(RX)

(F) is full control and (RX) is read and execute. Ordinary users can read the file but not save it, which is why editing it needs an elevated editor.

How to edit the hosts file on Windows

  1. Press the Windows key, type Notepad, right-click it and choose Run as administrator. Accept the User Account Control prompt.
  2. In Notepad choose File > Open, go to C:\Windows\System32\drivers\etc, and change the file-type filter from Text Documents (*.txt) to All Files (*.*), otherwise hosts is not shown.
  3. Open hosts, add your line at the end, and save with Ctrl+S.

If you opened Notepad normally, saving fails with a permission error. Close it and start again from step 1.

From an elevated PowerShell you can append a line without an editor:

Add-Content -Path "$env:WinDir\System32\drivers\etc\hosts" -Value "127.0.0.1 myapp.test"

A fresh Windows install has no active entries at all. Every line in the default file starts with #, including the localhost examples, which sit under the comment # localhost name resolution is handled within DNS itself. Windows answers for localhost on its own, so don't add or "fix" those lines.

How to edit the hosts file on macOS

Open Terminal and run:

sudo nano /etc/hosts

Type your login password (nothing appears as you type). Add your line, press Ctrl+O then Enter to save, and Ctrl+X to exit. Then clear the DNS cache with the command Apple gives for OS X 10.10.4 and later:

sudo killall -HUP mDNSResponder

How to edit the hosts file on Linux

sudo nano /etc/hosts      # or: sudoedit /etc/hosts

On Linux the hosts file is one of several sources the resolver can use, and /etc/nsswitch.conf sets the order. Check it:

grep '^hosts' /etc/nsswitch.conf

Example output (Ubuntu under WSL):

hosts:          files dns

Sources are tried left to right, and files means /etc/hosts, so here the hosts file is consulted before DNS. Some distributions list additional services on this line, so read yours rather than assuming.

Under WSL, the first lines of /etc/hosts say # This file was automatically generated by WSL. WSL rebuilds it from the Windows hosts file, so either edit the Windows file or follow the instruction in that comment (generateHosts = false in /etc/wsl.conf) before making changes inside Linux.

Hosts file format

From hosts(5): one entry per line, an IP address, then a canonical host name, then optional aliases, separated by spaces or tabs. A # starts a comment that runs to the end of the line.

# IP address    host name          aliases
127.0.0.1       myapp.test         api.myapp.test
192.168.1.50    nas.home.test      nas
0.0.0.0         ads.example.com
::1             myapp.test
  • Exact names only. There is no wildcard syntax, so example.com and www.example.com need separate entries.
  • No ports. The file maps names to addresses. 127.0.0.1:3000 myapp.test is not valid; put the port in the URL instead (http://myapp.test:3000).
  • Pick safe test names. RFC 6761 lists .test as a special-use name that registrars must not register, and reserves .localhost for loopback, so a name like myapp.test cannot clash with a real website. What localhost is explains the loopback addresses.

Common uses

  • Local development: point myapp.test at 127.0.0.1 so a local server answers on a realistic name.
  • Testing a site migration: point the real domain at the new server's IP on your machine only, check the site, then remove the line before switching DNS for everyone.
  • Naming devices on your LAN: give a NAS or printer a name instead of typing its IP.
  • Blocking a domain: map it to 0.0.0.0 so the name stops resolving to the real server on your machine.

How to check the change worked

Use a tool that goes through the system resolver. On Windows, ping and Resolve-DnsName both read the hosts file; nslookup does not. Example output for a name that exists only in the hosts file (name and router anonymised):

> ping -n 1 myapp.test
Pinging myapp.test [127.0.0.1] with 32 bytes of data:

> Resolve-DnsName myapp.test
Name        IPAddress Section
----        --------- -------
myapp.test  127.0.0.1 Answer

> nslookup myapp.test
Server:  router.lan
Address:  192.168.1.1
*** router.lan can't find myapp.test: Non-existent domain

nslookup sends the query straight to your DNS server, which has never heard of the name. That is the correct behaviour, not a sign the edit failed. Resolve-DnsName has a -NoHostsFile switch that, according to Microsoft's reference, "skips the hosts file", which is useful to compare the two answers. On macOS and Linux, ping also uses the system resolver, and on Linux getent hosts myapp.test prints the answer the system resolver gives, following the nsswitch.conf order; dig, like nslookup, queries DNS directly.

If the change has no effect

  • The browser still goes to the old address. Browsers keep their own DNS cache and reuse open connections. Flush the system cache and the browser's (how to flush the DNS cache), or fully restart the browser.
  • The file saved under the wrong name. On Windows, check for hosts.txt in the folder (in Windows 11 File Explorer, View > Show > File name extensions makes the extension visible).
  • A typo in the line. A missing space between address and name, or a stray character at the start, makes the line invalid. Comment out everything you added and re-add one line at a time.

Frequently asked questions

Does the hosts file override DNS?
For programs that use the operating system resolver, such as browsers and ping, an entry in the hosts file is used for that name. On Linux the order is set by the hosts: line in /etc/nsswitch.conf; with files listed before dns, the hosts file is consulted first. Tools that query DNS servers directly, such as nslookup and dig, skip the hosts file entirely.
Can I use wildcards like *.example.com in the hosts file?
No. Each line maps one IP address to one or more exact host names, as described in hosts(5). To redirect example.com and www.example.com you need both names listed. For wildcard behaviour, run a local DNS resolver such as dnsmasq instead.
Do I need to restart after editing the hosts file?
No. The hosts(5) man page says changes normally take effect immediately, except where an application caches the result. Flushing the system DNS cache and restarting the browser clears those cached answers.
How do I undo a hosts file change?
Delete the line you added, or put a # at the start of it to comment it out, and save. If the file is badly damaged on Windows, Microsoft's support article gives the default contents and steps to replace it.

Sources