The hosts file is one of the simplest parts of a computer‘s networking system, but it can be surprisingly useful. It allows a device to associate a domain name with an IP address without relying on a DNS server.
You can use it to test a website before changing DNS records, point a domain to a particular server, block access to selected domains, or troubleshoot DNS-related problems.
The important part is knowing how the file works and editing it correctly. A small formatting mistake can prevent the intended mapping from working.

This guide explains what the hosts file is, how hostname resolution works, where the file is located on major operating systems, and how to safely edit and test it.
Table of Contents
What Is the Hosts File?
The hosts file is a local text file that maps hostnames to IP addresses.
For example, a hosts file entry might look like this:
192.168.1.50 example.localThis tells the computer that example.local should resolve to 192.168.1.50.
Unlike a DNS record, this mapping exists only on the device where the hosts file was modified. Other computers will not automatically use the same entry.
The hosts file is also useful because it can override normal DNS resolution in situations where the operating system checks the local file before querying DNS.
How the Hosts File Works
When you enter a hostname such as:
example.comyour computer needs to determine which IP address belongs to that hostname before it can establish a network connection.
Depending on the operating system and its configured name-resolution rules, the system may check local sources such as the hosts file before consulting DNS.
If a matching hosts entry is found, the operating system can use the specified IP address.
For example:
203.0.113.25 example.comThe computer can use 203.0.113.25 for example.com rather than obtaining a different address from DNS.
This makes the hosts file particularly useful for local testing and troubleshooting.
Hosts File vs DNS
The hosts file and DNS both help translate hostnames into IP addresses, but they work very differently.
| Feature | Hosts File | DNS |
|---|---|---|
| Location | Local device | DNS infrastructure |
| Scope | Usually one device | Can affect many devices |
| Requires DNS server | No | Yes |
| Editing | Manual | Usually managed through DNS services |
| Common use | Testing and local overrides | Normal domain resolution |
| Propagation | None | May involve caching and TTLs |
A hosts file change normally takes effect only on the computer where the change was made.
Where Is the Hosts File Located?
The location depends on the operating system.
Windows
On Windows, the hosts file is located at:
C:\Windows\System32\drivers\etc\hostsThere is no .txt extension.
Linux
On most Linux distributions, including Ubuntu, the hosts file is:
/etc/hostsmacOS
On macOS, the standard location is also:
/etc/hostsAlthough /etc/hosts is the common location, hostname-resolution behavior can also depend on the operating system’s configuration.
What Does a Hosts File Look Like?
A typical hosts file may contain entries similar to:
127.0.0.1 localhost
127.0.1.1 my-computer
192.168.1.20 server.localYou may also see IPv6 entries such as:
::1 localhostLines beginning with # are comments and are ignored.
For example:
# Local development server
192.168.1.50 dev.example.comThe comment helps explain why the entry exists without affecting hostname resolution.
Hosts File Syntax
A basic hosts file entry follows this structure:
IP_address hostnameYou can also associate multiple hostnames with the same IP address:
192.168.1.50 server.example.com api.example.comWhitespace between the IP address and hostname is normally sufficient to separate the fields.
For clarity, many administrators use spaces or tabs to keep entries easy to read.
Example of a Correct Entry
192.168.1.100 test.example.comExample of a Comment
# Testing the new web server
192.168.1.100 test.example.comExample of an IPv6 Entry
2001:db8::10 example.comThe exact address you use should, of course, be a valid address for the service you are trying to reach.
How to Edit the Hosts File on Windows
Windows protects the hosts file because changing it can affect network connections.
Therefore, you generally need administrator privileges to save modifications.
Edit Hosts File Using Notepad
- Open the Start menu.
- Search for Notepad.
- Right-click Notepad.
- Select Run as administrator.
- In Notepad, choose File > Open.
- Navigate to:
C:\Windows\System32\drivers\etc\- Change the file filter from text documents to All Files.
- Select
hosts. - Add or modify the required entry.
- Save the file.
For example:
192.168.1.50 example.localDo not accidentally save the file as hosts.txt. Windows must use the file named hosts.
Edit Hosts File From an Administrator Terminal
You can also open the file using an elevated command prompt:
notepad C:\Windows\System32\drivers\etc\hostsMake sure the Command Prompt or terminal has been opened with administrator privileges.
How to Edit the Hosts File on Ubuntu and Linux
On Linux, you normally need root privileges to modify /etc/hosts.
You can use:
sudo nano /etc/hostsThen add your entry, for example:
192.168.1.50 example.localSave the file and exit the editor.
If you are using Vim, you could instead run:
sudo vim /etc/hostsThe exact editor does not matter. What matters is preserving the existing file structure and adding valid entries.
How to Edit the Hosts File on macOS
On macOS, you can edit the file from Terminal with:
sudo nano /etc/hostsEnter your administrator password when prompted, then add the required mapping.
For example:
192.168.1.50 example.localSave the file and close the editor.
How to Use the Hosts File for Website Testing
One of the most useful applications of the hosts file is testing a website on a new server before changing its public DNS records.
Suppose your domain is:
example.comand the new server has the IP address:
203.0.113.50You can temporarily add:
203.0.113.50 example.comYour computer can then attempt to reach the website using the new server while the public DNS records remain unchanged.
This is particularly useful when migrating websites or testing a new hosting environment.
Testing a Website Migration
A typical workflow might be:
- Set up the website on the new server.
- Confirm the new server is working.
- Add a temporary hosts entry on your computer.
- Open the domain in a browser.
- Test pages, images, forms, login functionality, and other features.
- Remove the hosts entry.
- Change the public DNS records when the migration is ready.
This lets you test the new environment without immediately sending all visitors to it.
How to Block a Domain Using the Hosts File
The hosts file can also be used to prevent a hostname from resolving to its normal address.
A commonly used example is:
0.0.0.0 example.comor:
127.0.0.1 example.comThe first approach sends traffic toward the non-routable 0.0.0.0 address, while the second points it back to the local computer.
However, this is not a complete security or content-filtering solution. Applications may use alternative DNS mechanisms, hard-coded IP addresses, proxies, VPNs, or other methods that bypass a simple hosts-file rule.
How to Check Whether the Hosts File Is Working
After editing the file, you should test the result instead of assuming that the change worked.
Test With Ping
On Windows, Linux, or macOS, you can try:
ping example.comIf the hostname resolves to the IP address you specified, the output should show that address.
For example:
PING example.com (192.168.1.50)Keep in mind that a successful hostname resolution does not necessarily mean that the server will respond to ping. Some servers block ICMP traffic.
Test With nslookup
You can also run:
nslookup example.comHowever, be careful when interpreting nslookup results. Depending on the operating system and tool behavior, it may query DNS directly rather than showing exactly how the application’s normal resolver path handled the hosts file.
For troubleshooting the actual local resolution path, other tools can sometimes provide better information.
Test With the getent Command on Linux
On Linux systems using the standard Name Service Switch configuration, try:
getent hosts example.comIf the hosts file is being consulted through the configured resolution process, this can be a useful way to check the result.
Test With a Browser
Finally, open the hostname in your browser.
If you are testing a website migration, check important functionality such as:
- Homepage
- Internal pages
- Images
- CSS and JavaScript
- Login forms
- Contact forms
- APIs
- Redirects
- HTTPS
- Cookies and sessions
What to Do When the Hosts File Change Does Not Work
Sometimes you make a change but the computer continues using the previous IP address.
Several things can cause this.
Check the Entry for Typos
Make sure the IP address and hostname are correct:
192.168.1.50 example.comAvoid accidentally adding characters or punctuation that do not belong there.
Check the File Name
On Windows, make sure the file is actually named:
hostsand not:
hosts.txtThis is a common problem when editing the file through a graphical text editor.
Clear DNS Cache
Operating systems and applications can cache DNS information.
On Windows, you can flush the DNS resolver cache with:
ipconfig /flushdnsOn Ubuntu systems using systemd-resolved, you can commonly use:
sudo resolvectl flush-cachesThe exact cache mechanism can vary depending on the Linux distribution and configuration.
On macOS, DNS cache behavior and commands can vary between releases, so use the command appropriate for your installed version.
Restart the Application
Browsers and other applications can maintain their own DNS or connection caches.
If the change is not reflected immediately, close and reopen the browser or application and test again.
Check for Multiple Hostname Entries
Look through the hosts file for another entry involving the same hostname.
For example:
192.168.1.50 example.com
192.168.1.75 example.comHaving conflicting entries can make troubleshooting confusing.
Keep only the mapping you actually intend to use.
Common Hosts File Mistakes
Editing the hosts file is simple, but a few mistakes occur frequently.
Adding http:// or https://
Do not write:
192.168.1.50 https://example.comThe hosts file maps hostnames, not complete URLs.
Use:
192.168.1.50 example.comAdding a Port Number
Do not write:
192.168.1.50 example.com:8080Ports are not part of a hosts file mapping.
Instead, the hosts file should contain:
192.168.1.50 example.comThen access the desired port separately:
http://example.com:8080Saving the File With the Wrong Extension
On Windows, saving the file as hosts.txt instead of hosts means the operating system will not use it as the hosts file.
Editing Without Administrator Privileges
Windows and Linux normally restrict modifications to the hosts file.
If you cannot save your changes, make sure you are using an administrator account or an elevated command.
Using the Wrong IP Address
If the IP address does not belong to the intended server, your browser may connect to the wrong machine.
Always verify the destination before making the change.
How to Temporarily Disable a Hosts Entry
You do not necessarily need to delete an entry to disable it.
Simply place a # at the beginning of the line:
# 192.168.1.50 example.comThe system will treat the line as a comment.
This is useful when testing different configurations because you can easily restore the entry later.
How to Safely Edit the Hosts File
Because the hosts file can affect network connectivity, it is a good idea to make a backup before making significant changes.
On Linux or macOS:
sudo cp /etc/hosts /etc/hosts.backupYou can then restore the original file if necessary.
On Windows, you can make a copy of the existing hosts file before editing it.
It is also helpful to add comments explaining temporary entries:
# Temporary website migration test
203.0.113.50 example.comThis makes it much easier to understand the reason for the entry later.
Is Editing the Hosts File Safe?
Yes, editing the hosts file is generally safe when you know exactly what you are changing.
However, an incorrect entry can redirect applications to an unintended server.
For example:
203.0.113.99 bank-example.comcould cause your computer to connect to the wrong destination whenever that hostname is requested.
For this reason, do not copy hosts-file entries from untrusted sources without understanding what they do.
On managed computers and production servers, unauthorized changes to the hosts file should also be treated as a potential security concern.
Hosts File and HTTPS
The hosts file only controls hostname-to-IP resolution. It does not create an SSL/TLS certificate.
For example, if you add:
192.168.1.50 example.comyour browser will attempt to connect to 192.168.1.50 when you visit https://example.com.
The server still needs to provide a certificate valid for example.com.
This is why hosts-file testing is especially useful during website migrations: you can test the new server using the real domain name while keeping the public DNS unchanged.
When Should You Use the Hosts File?
The hosts file is useful for several practical situations:
- Testing a website before a DNS change
- Testing a server migration
- Developing websites locally
- Mapping internal server names
- Troubleshooting DNS issues
- Temporarily overriding DNS
- Blocking selected hostnames in simple environments
- Testing different server configurations
It is best suited for local or temporary changes rather than managing large-scale hostname resolution.
Final Thoughts
The hosts file may be a small text file, but it gives administrators direct control over hostname resolution on an individual computer. Understanding how it works can save time when testing websites, troubleshooting DNS problems, or moving services between servers.
The key is to use the correct format: IP address followed by the hostname. Always edit the correct file, use administrator privileges when required, and test the result after making changes.
For temporary website testing, a hosts-file entry can be especially valuable because it allows you to point your computer toward a new server without immediately changing public DNS records. Once testing is complete, remember to remove or comment out temporary entries so they do not cause confusion later.
Note: Product details, pricing, and availability may change over time. While we try to keep information accurate, please verify details from the official website before purchasing. Some content may be assisted by AI tools like ChatGPT, and some articles may contain affiliate links that may earn us a small commission at no extra cost to you. Reviews, opinions, and feature highlights are based on official specifications and publicly available information at the time of writing.
Explore our
News Section
for daily updates and the latest tech news.Join our
WhatsApp Channel
for instant updates.






