What replacing your host file means and why you might do it

Your host file is a text document on your computer that tells your system which web addresses go to which internet addresses. When you type a website name into your browser, your computer checks the host file first before asking the internet. Replacing it means overwriting the current file with a new version — usually because the old one is corrupted, infected with malware that redirects websites, or because you want to start fresh with a clean copy.

Most people never touch their host file. But if malware has hijacked it to send you to fake websites, or if you've made manual changes that broke something, replacing it with a clean copy from your operating system restores normal web browsing. This is different from editing the file to block ads or redirect a single website — that's modification. Replacement means deleting what's there and putting back what your system shipped with.

Key Takeaways

  • The host file location differs by operating system: C:\Windows\System32\drivers\etc\hosts on Windows, /etc/hosts on Mac and Linux.
  • You need administrator or root access to replace the file, and you must disable read-only protection before saving changes.
  • The safest approach is to back up your current host file first, then replace it with the default version your system came with.
  • After replacement, restart your browser or flush your DNS cache so your computer stops using the old cached addresses.
  • If you cannot find or edit the file, check that you are using an administrator command prompt or terminal window.

How to replace the host file on Windows

Open Notepad as administrator. Click the Windows Start button, type Notepad, right-click the result, and select Run as administrator. A window will appear asking for permission — click Yes.

In Notepad, go to File > Open. Navigate to C:\Windows\System32\drivers\etc. You may need to change the file type dropdown from Text Documents to All Files to see the hosts file. Click it and open it.

Select all the text in the file (Ctrl+A) and delete it. Then paste in the default Windows host file content. The default file contains only two lines:

127.0.0.1 localhost ::1 localhost

Go to File > Save. If you get an error saying the file is read-only, close Notepad. Right-click the hosts file in File Explorer, select Properties, uncheck Read-only, and click explore. Then open it again in Notepad as administrator and save.

How to replace the host file on Mac

Open Terminal. You can find it in Applications > Utilities, or press Command+Space, type Terminal, and press Enter.

Type the following command and press Enter:

sudo nano /etc/hosts

You will be asked for your password. Type it (you will not see the characters) and press Enter. The nano text editor will open with your host file.

Select all text (Command+A), then delete it. Type the default Mac host file content:

## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost

Press Control+O to save, then press Enter to confirm the filename. Press Control+X to exit nano.

How to replace the host file on Linux

Open a terminal window. The method varies by desktop environment, but usually you can right-click the desktop and select Open Terminal Here, or search your applications menu for Terminal.

Type the following command and press Enter:

sudo nano /etc/hosts

Enter your password when prompted. The nano editor will open your host file.

Select all text and delete it. Paste the default Linux host file:

127.0.0.1 localhost ::1 localhost

Press Control+O to save, press Enter to confirm the filename, then Control+X to exit. Some Linux distributions include additional default entries — if you want the exact original file, check your system documentation or the file in a fresh virtual machine.

Back up your current host file before replacing it

Before you delete anything, make a copy of your current host file so you can restore it if something goes wrong. On Windows, right-click the hosts file in C:\Windows\System32\drivers\etc, select Copy, then right-click in the same folder and select Paste. Rename the copy to hosts.backup.

On Mac or Linux, open Terminal and type:

sudo cp /etc/hosts /etc/hosts.backup

This creates a backup file called hosts.backup in the same directory. If you need to restore it later, type:

sudo cp /etc/hosts.backup /etc/hosts

Flush your DNS cache after replacing the host file

After you replace the host file, your browser may still remember old website addresses from before the change. Flushing the DNS cache clears that memory so your computer uses the new host file when ready.

On Windows, open Command Prompt as administrator and type:

ipconfig /flushdns

On Mac, open Terminal and type:

sudo dscacheutil -flushcache

On Linux, the command depends on your system. If you use systemd-resolved, type:

sudo systemctl restart systemd-resolved

After running the command, close your browser completely and reopen it. Your computer will now read the new host file.

Troubleshooting: what to do if replacement fails

If you cannot save the file, you likely do not have administrator or root access. On Windows, make sure you opened Notepad by right-clicking and selecting Run as administrator — opening it normally will not work. On Mac and Linux, make sure you typed sudo before the nano command.

If the file still shows as read-only on Windows after you unchecked the property, the file may be locked by another program. Close your browser, any VPN software, and any antivirus program, then try again. If a program keeps locking the file, you may need to boot into Safe Mode to replace it.

If websites still redirect to the wrong place after replacement, the problem may not be your host file — it could be malware elsewhere on your system, or your DNS server itself could be compromised. Run a full antivirus scan and consider changing your DNS settings to a public DNS like 8.8.8.8 (Google) or 1.1.1.1 (Cloudflare).

Frequently Asked Questions

Can I replace the host file without administrator access?

No. The host file controls how your entire system resolves website addresses, so the operating system restricts changes to administrator or root users only. If you do not have administrator access on your computer, you will need to contact your system administrator or the person who set up the machine.

What if I accidentally deleted important lines from my host file?

If you made a backup before editing, restore it using the backup file you created. If you did not back up, the default content for your operating system is short enough to retype — Windows and Linux need only two lines, and Mac needs eight. Refer to the default content shown in the replacement steps above.

Do I need to restart my computer after replacing the host file?

No. Restarting your browser and flushing the DNS cache is usually enough. A full computer restart is not necessary unless a program is still locking the file or caching old addresses.

Will replacing the host file remove malware?

Replacing the host file will stop malware from redirecting your websites through that file, but it will not remove the malware itself from your system. After replacement, run a full antivirus or anti-malware scan to find and remove the infection.

Can I edit the host file instead of replacing it?

Yes. If you only want to remove a few bad entries rather than start fresh, you can open the file and delete just those lines. Use the same steps as replacement, but select and delete only the lines you want to remove instead of all the text.