Back to Article List

Where is the host file location on your computer?

Where is the host file location on your computer? - Where is the host file location on your computer?

The host file location on Windows is C:\Windows\System32\drivers\etc\hosts. On a Mac it sits in the /private/etc folder. Either way it is a plain text file called hosts, with no file extension, and it tells your computer which IP address to use for a domain name before it asks DNS.

That last part matters to anyone who runs a website. One line in this file lets you load your site from a new server while everyone else still sees the old one. Let's find the file on each system, edit it without breaking anything, use it to preview a site move, and then put everything back.

Host file location on Windows

On Windows, the hosts file is in this folder:

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

If Windows is installed on a drive other than C:, only the drive letter changes. The quickest way there is to open File Explorer, paste C:\Windows\System32\drivers\etc into the address bar and press Enter.

Inside you will see a file named hosts. Explorer may describe its type simply as "File", because it has no extension. A few other small files sit next to it; leave those alone.

People often ask where the etc folder is on Windows. It is this one, tucked inside drivers.

Host file location on macOS and Linux

On a Mac the file is in the /private/etc folder. Finder hides these system folders, so use Go, then Go to Folder, and type /private/etc. We cover editing it below.

On Linux the file is also called hosts and sits in a system folder; your distribution's documentation gives the exact path. A fresh file usually contains a line for localhost and often one for the computer's own name. Keep those lines; your system uses them.

SystemPathNeeds admin rights to save?
WindowsC:\Windows\System32\drivers\etc\hostsYes
macOSIn the /private/etc folderYes
LinuxA system folder; check your distribution's documentationYes

Editing the file safely

The hosts file is small and forgiving, but it is a system file, so a little care saves a confusing afternoon. Here is the routine we follow:

  1. Make a copy first. Copy the file to your desktop as hosts-backup. If anything goes wrong, you copy it back.
  2. Open it with admin rights. On Windows, search for Notepad in the Start menu, right-click it and choose Run as administrator. Then use File, Open, paste the folder path and switch the file filter from Text Documents to All Files, otherwise the hosts file stays invisible. On macOS or Linux, open the file in a text editor with administrator rights.
  3. Add your lines at the bottom. Each line is an IP address, a space or tab, then a domain name. Lines starting with # are comments and are ignored.
  4. Save under the same name. In Notepad, make sure the file is still called hosts and has not become hosts.txt. On a Mac or Linux, save the file and close the editor.
  5. Clear the DNS cache. Your computer may remember the old address for a while.

A typical entry looks like this, using an example address:

# Preview of new host - remove after DNS switch
203.0.113.10    example.com
203.0.113.10    www.example.com

To clear the cache afterwards, use your system's option for flushing the DNS cache, or simply restart the computer so it forgets the old address.

Some security software watches the hosts file and may ask before letting a change through. That is reasonable: malware has long used this file to send people to fake versions of real sites. Allow the change you just made, and if you ever find lines you did not add that point banks, email or search engines somewhere unexpected, treat that as a warning sign.

Is it safe to delete the hosts file? On most computers the default file holds little more than comments and a localhost entry, so deleting it rarely breaks things, but there is no reason to. Remove your own lines instead.

Previewing a site on a new host

This is the reason most site owners ever open the hosts file. When you move a website, the switch happens when you change the domain's DNS to point at the new server. Before you do that, you want to know the site actually works there. The hosts file lets your computer alone visit the new server under your real domain name, while customers keep using the old one.

The steps:

  1. Copy your site to the new host. Files, database, everything, exactly as you plan to run it.
  2. Find the new server's IP address. It is usually in the welcome email or the control panel of your new hosting account.
  3. Add hosts lines for both versions of the name, with and without www, as in the example above.
  4. Flush the DNS cache and restart your browser, or open a private window.
  5. Confirm you are really on the new server. Run ping example.com; it should show the new IP. A small test file uploaded only to the new server is another easy check.

Then click through what matters: the home page, a few inner pages, images, contact forms, logins, the admin area and, for a shop, the checkout up to the payment step. Because the domain name is the real one, the site behaves as it will after the move, which a temporary preview address cannot always show you.

Two things to expect. First, you may get a certificate warning over HTTPS, because many hosts can only issue a certificate once the domain really points at their server. During a preview that is often normal; just do not enter real payment details. Second, only the names you list are redirected. If your email runs on mail.example.com and you did not add it, your mail keeps going to the old server, which is usually what you want during a test.

If you are still choosing where the site goes, TPC Hosting's web hosting runs in GDPR-compliant data centres in the EU, and the same preview routine works there: point your domain at your account's server address in the hosts file, check everything, then change DNS. Support is around the clock, where AI answers immediately and engineers follow up, if you get stuck along the way.

Undoing the change afterwards

Once you have switched DNS and the site works for everyone, take your lines out. Delete them, or put a # at the start of each one so you can reuse them later, then save and flush the DNS cache again.

This step is easy to forget and annoying when you do. A leftover line quietly overrides DNS on your computer forever. The next time you change hosts, or the server's IP address changes, you will see an old or broken site that nobody else sees, and you may spend hours chasing a problem that only exists on your laptop.

A simple check tells you whether you are clean. Run ping example.com and make sure it shows the address your domain's DNS now points to, then confirm the site loads from the server you expect. If you edited the file on a colleague's or client's machine during the move, remind them to remove the lines too, or do it before you hand the laptop back.

FAQ

How do I edit the hosts file on Windows?

Open Notepad as administrator, open C:\Windows\System32\drivers\etc\hosts, add your lines and save. In Notepad's Open dialog, switch the filter from Text Documents to All Files so the hosts file shows up. Add each entry as an IP address, a space, then the domain name, save the file under the name hosts without a .txt extension, and then clear the DNS cache or restart the computer so Windows forgets the old address.

Why are my hosts file changes not working?

Usually the change was not saved where you think, or your computer is still using a cached address. Check that the file is still called hosts and not hosts.txt, that you edited it with admin rights so the save actually went through, and that there is no # at the start of your line. Add both example.com and www.example.com, flush the DNS cache and restart the browser. If it still fails, a VPN, proxy or the browser's own secure DNS setting may be sending lookups elsewhere; try turning it off briefly or test in another browser.