The localhost file is a text document on your computer that tells your system how to find itself on the network

Your computer has a file called hosts (no file extension on Mac and Linux, or hosts.txt on Windows) that sits in a specific system folder. This file contains a straightforward list of names and numbers — it maps domain names like "localhost" to IP addresses like "127.0.0.1". When you type "localhost" into a web browser, your computer checks this file first before asking the internet for directions.

The localhost file is not something you need to edit for normal computer use. It comes pre-installed with your operating system and already contains the one line that matters: the mapping that sends "localhost" back to your own machine. You might need to look at it or change it only if you are running a web server on your computer, testing a website locally, or troubleshooting a network problem.

Key Takeaways

  • The hosts file lives in a specific system folder that is hidden by default: C:\Windows\System32\drivers\etc\hosts on Windows, or /etc/hosts on Mac and Linux.
  • The file is a plain text document that your computer reads before it searches the internet for a domain name.
  • You need administrator or root permissions to edit the hosts file, and editing it incorrectly can break your internet connection to certain websites.
  • The default localhost entry (127.0.0.1 localhost) is already in the file and should not be removed.

How to find the localhost file on Windows

On Windows, the hosts file is stored at C:\Windows\System32\drivers\etc\hosts. The folder is hidden by default, so you cannot see it by opening File Explorer and clicking through folders the normal way.

The fastest route is to open Notepad as an administrator, then use File > Open to navigate to that path. Click the Start button, type "Notepad" in the search box, right-click Notepad, and select "Run as administrator". Once Notepad opens, go to File > Open. In the file browser, paste this path into the location bar: C:\Windows\System32\drivers\etc\ Then look for the file named hosts (with no extension) and open it.

If you see only files with extensions like .txt or .exe, your File Explorer is hiding file extensions. You can still open the hosts file — it will be the one with no visible extension in that folder. If the folder appears empty, click the View menu in File Explorer and check "Hidden items" to show system files.

How to find the localhost file on Mac and Linux

On Mac and Linux, the hosts file is at /etc/hosts. This is a system folder that requires administrator (root) permissions to edit.

On Mac, open Terminal (search for it in Spotlight), then type: sudo nano /etc/hosts and press Enter. You will be asked for your password. Type it (the cursor will not move, but the password is being entered) and press Enter again. The file will open in the nano text editor. When you are done viewing or editing, press Control+X, then Y, then Enter to save and close.

On Linux, the process is the same: open a terminal and type sudo nano /etc/hosts. The nano editor works identically on both systems.

What the localhost file contains and what it does

The hosts file is a plain text list with two columns: an IP address on the left and a domain name on the right. The most important line is already there: 127.0.0.1 localhost. This tells your computer that the address "localhost" points to 127.0.0.1, which is a special IP address that means "this computer".

Your computer checks the hosts file before it sends a request out to the internet. If it finds the domain name in the hosts file, it uses the IP address listed there and never asks the internet. If it does not find the name, it sends a request to your internet service provider's DNS servers, which look up the real address.

This is why the hosts file can be used to block websites: if you add a line like 127.0.0.1 facebook.com, your computer will try to connect to itself instead of to Facebook, and the connection will fail. Some people use this method to block distracting sites on their own computer, though it is not a strong security tool — anyone with access to the file can undo it.

When you might need to edit the localhost file

Most people never need to edit the hosts file. It comes with the right settings for normal internet use. You might need to change it in these situations: you are running a web server on your computer and want to test it by visiting a local domain name, you are a developer testing a website before it goes live, or you are troubleshooting a network problem and a technician has asked you to check or modify an entry.

If you are running a local web server — for example, using software like Apache, Nginx, or a development tool like Docker — you may add a line to the hosts file so you can visit your test site by typing a readable name instead of an IP address. For example, a developer might add 127.0.0.1 mysite.local so they can type "mysite.local" in the browser instead of "127.0.0.1:8000".

Why you should be careful editing the localhost file

The hosts file is read by your operating system before it does anything else with a domain name. If you make a mistake — a typo, a missing space, or a wrong IP address — you can break your connection to websites. For example, if you accidentally type 127.0.0.1 google.com, your computer will try to connect to itself when you type google.com, and the page will not load.

Always make a backup before you edit the file. On Windows, right-click the hosts file, select Copy, and paste it in the same folder with a name like "hosts.backup". On Mac and Linux, you can type sudo cp /etc/hosts /etc/hosts.backup in Terminal. If something breaks, you can restore the original file by copying the backup back.

Edit the file slowly and carefully. Add only one line at a time, save, and test in your browser before adding another. If a website suddenly stops loading and you recently edited the hosts file, that is usually the cause — open the file again and look for typos or entries you do not recognize.

How to restore the localhost file to its default state

If you have edited the hosts file and want to return it to the original state, the simplest way is to restore from your backup. If you did not make a backup, you can delete all the lines you added and leave only the default entries.

The default localhost file contains these lines on all systems:

127.0.0.1 localhost ::1 localhost

The second line (::1 localhost) is for IPv6, the newer version of the internet protocol. Both lines should be present. If your file has only the first line, you can add the second one. If your file has many other entries you did not add, and you want to clean it, you can delete everything except these two lines. Save the file and restart your browser.

Frequently Asked Questions

Can I delete the localhost file?

No. Your operating system needs this file to function properly. If you delete it, your computer will not be able to resolve "localhost" and some programs may stop working. If you have accidentally deleted it, restart your computer — most systems will regenerate a default version on startup. If that does not work, you can recreate it by opening Notepad or nano as described above, typing the two default lines, and saving it in the correct location.

What is the difference between localhost and 127.0.0.1?

They are the same thing. "Localhost" is a name, and "127.0.0.1" is the IP address it points to. The hosts file is what makes your computer understand that when you type "localhost", you mean "127.0.0.1". You can type either one into your browser and reach your own computer.

Why would I see "localhost refused to connect" in my browser?

This usually means your computer found the localhost address (127.0.0.1) correctly, but there is no web server running on that address and port. If you are trying to test a local website, make sure the web server software is running. If you are not running a web server, you will see this error — it is normal and not a problem with the hosts file.

Is editing the hosts file a security risk?

Editing your own hosts file is not a security risk if you know what you are adding. The risk comes if malware or an attacker modifies your hosts file without your knowledge — they could redirect you to fake websites that look real. Check your hosts file occasionally if you are concerned, especially if websites suddenly stop loading or redirect to unexpected pages.

Can I use the hosts file to block ads or malicious websites?

Yes, but it is not the best tool for it. You can add lines that redirect ad domains or malicious sites to 127.0.0.1, which will prevent your browser from loading them. However, this method requires you to maintain a long list of domains manually, and it only works on your own computer. A browser extension or a DNS-level filter is more practical for most people.