How to Edit Hosts File in Windows, macOS, and Linux

Mohammed Noufal
Tutorials

You can use the hosts file to map a domain to an IP address when you want to test your website without altering the domain DNS settings. This article will describe what is a hosts file, how it’s used, and how to edit hosts file in Linux, Windows, and macOS.

What is a Hosts File and What Does it Do?

A hosts file is a locally stored computer file that lists domain names and the IP addresses that correspond to them. The operating system uses it as a database of domain names to identify and locate hosts in an IP network. All commonly used operating systems include hosts files, which map a connection between an IP address and domain names. Each time you access a website, your computer first checks the hosts file to find the correct IP address to resolve the requested domain. If the domain is not listed, your internet service provider will check the DNS to find the necessary resources to load the website.

Why Modify the Hosts File?

By modifying your hosts file, you can override the DNS for a domain on a particular computer. It informs your computer which IP address should be associated with a domain name. This is frequently done before your domain name is modified to point to a particular IP address. This procedure is helpful if you want to change your domain after migration to see how it will appear on the new server. The hosts file is in plain text. As a result, you can edit hosts file using any text editor depending on your operating system, such as Notepad or the vi editor. Please keep in mind that the changes will only affect the system in which the hosts file is located.

How to Edit the Hosts File in Windows

You can use any text editor to complete the steps for editing the hosts file in Windows. This tutorial uses Notepad.

Step 1: 

Click Start, then select Notepad. Click and choose Run as administrator.

Step 2:

Once in Notepad, select File > Open. To locate the hosts file, navigate to C:\Windows\System32\Drivers\etc and choose All Files.

Step 3:

Add the custom IP address followed by a space and domain name to the host file.

Step 4:

Save the changes.

How to Edit the Hosts File in macOS

The instructions below will show you how to edit hosts file in macOS.

Step 1:

Open the Terminal Application. You can access the Terminal app by going to Applications, selecting the Utilities folder, or typing Terminal into the search box.

Step 2:

To open the hosts file, use the following command.

sudo nano /private/etc/hosts

On newer macOS devices, try this command.

sudo nano /etc/hosts

Step 3:

Enter your Mac administrator password.

Step 4:

Enter the server IP address at the bottom of the hosts file, followed by a space, and then the domain name.

1.2.3.4 www.testdomain.com
1.2.3.4 testdomain.com

Here is the output.

noufal@Mac:~$ cat /etc/hosts
127.0.0.1	localhost

1.2.3.4 www.testdomain.com
1.2.3.4 testdomain.com

Step 5:

Save the changes. 

How to Edit the Hosts File in Linux

You will learn how to edit hosts file in Linux by using the instructions in this section. The hosts file can be edited using any text editor, including vi and nano.

Step 1:

Press Ctrl+Alt+T to open the Terminal.

Step 2:

To open the hosts file in the vi text editor, type the following command.

sudo vi /etc/hosts

Step 3:

Enter your administrator password.

Here is a sample output.

noufal@noufal:~$ sudo vi /etc/hosts
[sudo] password for noufal: **************

Step 4:

Enter the server IP address at the bottom of the hosts file, followed by a space, and then the domain name.

1.2.3.4 www.sampledomain.com
1.2.3.4 sampledomain.com

Here is the output.

noufal@noufal:~$ cat /etc/hosts
127.0.0.1	localhost
ff00::0 ip6-mcastprefix

1.2.3.4 www.sampledomain.com
1.2.3.4 sampledomain.com

Step 5:

Save the changes. 

Common Problems and Solutions

Duplicate Bindings Problem

The duplicate bindings problem occurs when multiple entries have the same IP/hostname binding. This problem can be resolved by checking entries and deleting duplicate ones.

Conflict Bindings Problem

The hosts file contains entries for one domain that point to multiple IP addresses, which results in the conflict bindings issue. To fix the problem, you must check the hosts file and remove any incorrect entries.

What Causes Changes to Hosts Files Not to Take Effect?

The updated hosts should be used right away, but since Windows caches name resolution information, the previous records might be used for a while. Open a command prompt (Windows+R, cmd, Enter) and enter the following command to drop the old data.

ipconfig /flushdns

Use the following command to see if it works. For example, www.sampledomain.com.

ping www.sampledomain.com -n 1

Check to see if it is using the correct IP address. If so, the issue is not with your hosts file.

You can also try the following command (open the console as an administrator, or it won’t work) to reset the NetBios cache.

nbtstat -R

To view the current data in the DNS cache, use the following command.

ipconfig /displaydns | more

Hosts File Permissions Issues

There are occasionally problems with the hosts file’s permissions, file attributes, and other similar issues. By taking the steps listed below, you can quickly resolve this problem.

Step 1: 

On your computer, make a new text file.

Step 2:

In this file in Notepad, copy and paste the content of your current host’s file.

Step 3:

Save the modified text file with the new name hosts.

Step 4:

Replace the existing hosts file in your drivers/etc directory by copying it there rather than moving it. Moving the hosts file does not work, but copying it does.

To resolve permission issues, try copying the file’s contents, deleting the entire file, creating a new file (with the name “File” rather than an extension), and pasting the contents into it. That may resolve some of the more unusual permissions problems.

Hosts File in macOS Not Working

The hosts file is most likely not working on macOS because it has been corrupted or is no longer in an ASCII file format. This issue can occasionally occur when you edit hosts file using any text editor or third-party application or if the incorrect file type is saved using vim, vi, nano, etc. 

The steps listed below will help you solve the problem quickly.

Step 1:

Take a backup or rename the old hosts file to allow you to undo the change if necessary.

sudo mv /etc/hosts /etc/hosts_bak

Step 2:

Using the cat command to select the text and then copy it to your clipboard will make it simple to copy the contents of the hosts backup file to your clipboard.

cat /etc/hosts_bak

Step 3:

Using the vi text editor, create a new hosts file, and then paste the content of the existing hosts file into it.

sudo vi /etc/hosts

Step 4:

For changes to be effective, save the file and flush the DNS cache using the following command.

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

You shouldn’t need to restart any browsers or internet-connected applications because that should be enough for the host change to take effect.

Host file FAQs

On Windows, the hosts file is located at:

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

To open or edit it, you usually need administrator permissions.

The easiest way to access it is to:

  1. Open Notepad (or another text editor) as Administrator.
  2. Click File > Open.
  3. Navigate to:
    C:\Windows\System32\drivers\etc
  4. Change the file filter from Text Documents (.txt) to All Files.
  5. Select hosts and open it.

The Windows hosts file is a plain text file with no file extension.

Editing the hosts file on macOS changes how your Mac resolves domain names (like example.com) into IP addresses.

Normally, macOS uses DNS servers to determine what IP address a domain points to. When you edit the hosts file, you can manually override DNS and force your Mac to send traffic to a specific IP address instead.

For example, if you add this entry:

203.0.113.10 example.com

your Mac will load example.com from 203.0.113.10, even if the real DNS records point somewhere else.

This is commonly used for testing website migrations, previewing a site on a new server, or accessing a staging environment without changing public DNS.

On Ubuntu (and most Linux distributions), the hosts file is located at:

/etc/hosts

You can view it with:

cat /etc/hosts

Or edit it with administrator privileges using:

sudo nano /etc/hosts

The Ubuntu hosts file works the same way as on Windows and macOS: it maps domain names to IP addresses locally, overriding DNS.

You would edit your hosts file when you want your computer to load a website from a specific server without changing the domain’s real DNS records.

This is useful when you want to:

  • Test a website on a new hosting server before a migration goes live
  • Preview changes on a staging site while keeping the live site public
  • Verify SSL certificates on a new server before DNS updates
  • Troubleshoot DNS problems or confirm a domain is pointing to the right IP
  • Access a development server that is not publicly available

Editing the hosts file only affects your own computer (or the computer you edit it on). It does not change the website for anyone else.

Yes, it is generally safe to modify the hosts file on a Mac, as long as you only add valid entries and you understand what you are changing.

The hosts file is a standard system file used for local DNS overrides. Editing it will not damage macOS by itself, but incorrect entries can cause websites to load incorrectly or stop loading entirely.

The biggest risks are:

  • Accidentally blocking a legitimate domain by pointing it to the wrong IP
  • Forgetting to remove old entries after a migration or test
  • Being tricked by malware into redirecting common websites to malicious IP addresses

If you are making a temporary change, it is best practice to back up the original hosts file and remove your custom entries when testing is finished.

A WordPress staging site and editing your hosts file are two different ways to test changes before going live, but they serve different purposes.

A WordPress staging site is a separate copy of your site, usually hosted on a subdomain like staging.example.com. It allows you to test plugins, theme updates, design changes, and code edits without affecting the live website.

Editing your hosts file is a local testing method where you force your computer to load your domain (like example.com) from a different server IP. This lets you preview a migrated site as if it were live, without updating DNS publicly.

Use a staging site when:

  • You want to test plugins, themes, or WordPress updates safely
  • You need a sandbox environment for development work
  • Multiple people need access to preview changes

Use hosts file editing when:

  • You are migrating WordPress to a new server
  • You want to test the live domain on a new IP before DNS changes
  • You need to verify site behavior, SSL, or caching on the destination server

In most cases, a staging site is better for ongoing WordPress development. Editing the hosts file is best for short-term migration testing and DNS-related verification.

Conclusion

The hosts file can be helpful when you want to create a domain name mapping for your computer only without affecting other visitors. It’s easy to edit the hosts file in Windows, macOS, and Linux operating systems. No matter which version you’re using, it’s incredibly convenient that the file looks the same.

Liquid Web’s fully managed VPS hosting, VPS hosting solutions — including RDP and ASP.NET virtual private serversmanaged cloud servers, and dedicated servers come with around-the-clock support on Windows and Linux platforms. Contact our sales team today for a consultation.

Related articles

Wait! Get exclusive hosting insights

Subscribe to our newsletter and stay ahead of the competition with expert advice from our hosting pros.

Loading form…