Achievable logo
Achievable blue logo on white background
  • Reading
  • /How to edit your hosts file on Windows, Mac, or Linux

How to edit your hosts file on Windows, Mac, or Linux

Step-by-step instructions for editing the hosts file on Windows, macOS and Linux to block websites, test a server or troubleshoot DNS, plus how to apply changes safely.
Courtney Dang's profile picture
Courtney Dang
16 Nov 2026, 5 min read
Achievable blue logo on white background
  • Reading
  • /How to edit your hosts file on Windows, Mac, or Linux

The hosts file is a plain-text file on your computer that maps hostnames to IP addresses. Editing it lets you block a website, point a domain at a test server or troubleshoot DNS problems, all without changing anything on your network. You need administrator access and a plain-text editor. The steps below cover Windows, macOS and Linux.

What is a hosts file?

Before your computer asks a DNS server where a website lives, it checks the hosts file. If the hostname is listed there, your computer uses that IP address and skips DNS for that name. Think of it as a short list of directions your computer already has written down. That makes it useful for:

  • Blocking sites, by pointing them at an address that goes nowhere
  • Testing a website on a new server before its DNS changes
  • Troubleshooting network and DNS problems
  • Naming devices on your local network

Each entry goes on its own line: an IP address, at least one space or tab, then one or more hostnames. Lines that start with # are comments. For example:

127.0.0.1 localhost

0.0.0.0 example.com www.example.com

192.168.1.50 staging.example.com

The second line blocks example.com: 0.0.0.0 is an address that goes nowhere, so the connection fails right away (127.0.0.1, your own computer, also works). The third line sends staging.example.com to a server on your local network.

Write hostnames only, with no http://, paths or port numbers. The hosts file doesn’t support wildcards, so example.com and www.example.com each need to be listed.

What you’ll need

  • Administrator access. The hosts file is a protected system file.
  • A plain-text editor: Notepad on Windows, or nano or vim on Mac and Linux. Avoid word processors, which add formatting.
  • A backup of the original file, so you can restore it if anything goes wrong.

How to edit the hosts file on Windows

These steps work on Windows 10 and Windows 11. The file is at C:\Windows\System32\drivers\etc\hosts.

  1. Open the Start menu, type Notepad, right-click it and choose Run as administrator. Approve the prompt.
  2. In Notepad, choose File > Open and go to C:\Windows\System32\drivers\etc.
  3. Change the file-type dropdown from Text Documents (*.txt) to All Files, then open hosts.
  4. Add your entries on new lines at the end of the file.
  5. Save with File > Save or Ctrl+S. The file must stay named hosts, with no extension. If Notepad offers to save hosts.txt instead, it wasn’t opened as an administrator.
  6. Flush the DNS cache so the change applies right away: open Command Prompt as an administrator and run ipconfig /flushdns (Microsoft’s ipconfig reference).

To back up the file first, copy it to a file named hosts.backup in the same folder. File Explorer will ask for administrator permission.

Microsoft Defender may flag or undo entries that redirect well-known domains, because malware uses the same trick. If an entry keeps disappearing, that’s the likely reason.

How to edit the hosts file on Mac

On macOS the file is at /etc/hosts, and the easiest way to edit it is in Terminal.

  1. Open Terminal from Applications > Utilities, or search for it with Spotlight.
  2. Back up the file: sudo cp /etc/hosts /etc/hosts.backup
  3. Open it in nano: sudo nano /etc/hosts. Enter your Mac password when asked; nothing appears on screen as you type.
  4. Use the arrow keys to move to the end of the file and add your entries.
  5. Press Control+O, then Return, to save, and Control+X to exit.
  6. Flush the DNS cache: sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

How to edit the hosts file on Linux

Linux also uses /etc/hosts. Its format is documented in the hosts(5) manual page.

  1. Open a terminal.
  2. Back up the file: sudo cp /etc/hosts /etc/hosts.backup
  3. Open it in a text editor: sudo nano /etc/hosts, or sudo vim /etc/hosts. New to nano? Its documentation covers the basics.
  4. Add your entries, then save with Control+O and exit with Control+X.
  5. Changes usually apply right away. If your distribution caches DNS with systemd-resolved, as Ubuntu and Fedora do by default, run resolvectl flush-caches.

On most Linux systems, the hosts file is checked before DNS because of the hosts: files dns line in /etc/nsswitch.conf.

If your change doesn’t work

  • Restart your browser. Browsers keep their own DNS cache, and closing every window clears it.
  • Check the format. One entry per line, IP address first, and no http://.
  • List both versions of the name. example.com and www.example.com are different hostnames.
  • Check for a proxy or VPN. If your traffic goes through a proxy or some VPN apps, name lookups can happen on a remote server, where your hosts file doesn’t apply.
  • Confirm the file saved. On Windows, make sure the file is still named hosts, not hosts.txt.

Security considerations

The hosts file is powerful, which is why malware sometimes edits it to send people to fake banking or update sites. A few habits keep it safe:

  • Back up the original before you change it, and restore the backup if something breaks.
  • Add only entries you understand, and don’t paste in block lists from sources you don’t trust.
  • Check the file now and then for entries you didn’t add.
  • Remove temporary entries, such as test servers, when you’re done, so you don’t land on a stale server later.

If you’re studying networking, editing the hosts file is a hands-on way to see name resolution at work, and it fits alongside the DNS topics in this CCNA study guide.

Key takeaways

  • The hosts file maps hostnames to IP addresses and is checked before DNS.
  • On Windows, edit C:\Windows\System32\drivers\etc\hosts in Notepad run as an administrator. On Mac and Linux, edit /etc/hosts with sudo nano.
  • Write each entry as an IP address followed by hostnames, with no http://, and list the www version separately.
  • Back up the file first, flush your DNS cache afterward and restart your browser if a change doesn’t take effect.
Courtney Dang's profile picture
Courtney Dang
16 Nov 2026, 5 min read
Achievable blue logo on white background

More reading

All reading

12 common dog breeds and their health issues

The health problems 12 common dog breeds are prone to, from hip dysplasia in Labradors to breathing trouble in pugs, and what owners can do to prevent or catch them early.

15 office design tricks that will increase your productivity at work

Fifteen office design changes, from natural light and ergonomic furniture to noise control and storage, that help you focus and get more done at work or at home.

15 office organization tips to help you get more done

Fifteen practical office organization tips for your desk, paperwork, digital files and daily routine, so you spend less time searching and more time getting work done.

Choosing sunglasses: Is UV protection important?

Why UV protection is the most important feature of sunglasses, how UV light damages your eyes, and what to look for on the label, from UV400 to lens size and polarization.

How online reviews influence sales: a guide for retailers

What research shows about how online reviews influence sales, from star ratings to the first five reviews, and how retailers can collect more reviews within the FTC’s rules.

How to manage stress and anxiety

Learn the signs of stress, healthy ways to cope with it day to day, when stress and anxiety call for professional help, and free resources you can use right now.

How to start mining cryptocurrency: a beginner’s guide

How cryptocurrency mining works, what it takes to mine Bitcoin, Litecoin, Dogecoin, Zcash and Monero today, why Ethereum can no longer be mined, and the costs and risks to weigh first.

The concise guide to writing SMART goals

What SMART stands for, the questions that test each letter, and a worked example that turns a vague goal into a specific, measurable plan with a deadline.

Yoga for anxiety and depression: what research shows and how to start

What research says about yoga for stress, anxiety and depression, why it may help, and a simple breathing technique plus eight beginner poses to get started safely.