Your Mac hosts file is important for a number of reasons. However, it’s particularly crucial when you want to override your Domain Name System (DNS) and map to a new Internet Protocol (IP) address. In that scenario, it’s essential to know how to locate and access this file.
The good news is that doing so is relatively quick and painless. You simply need to know which application to use and what commands to run. Then you can safely edit and update your hosts file to resolve domain names to IP addresses on your Mac device.
In this post, we’ll explain what the Mac hosts file is and the purpose it serves. Then we’ll walk you through how to find and edit it in four simple steps. Let’s jump right in!
What the Mac Hosts File Is (and Why It’s Important)
Typically, your website is found using the DNS in the nameservers your domain is pointed to. The DNS translates your URL (such as kinsta.com) into an IP address that the server can interpret.
However, your computer (in this case, a Mac) can use the hosts file to resolve your site to a different or specific IP address. In other words, you can manually configure your domain name to the IP address of your choosing.
For example, one of the reasons you may want to edit your hosts file is because you’re in the process of migrating your website to a new server.
Manually editing your hosts file to resolve the IP address and domain name will let you test your site on a different server. This rule applies even when the DNS hasn’t propagated yet.
In a nutshell, you can use your Mac hosts file to mimic and test everything. As such, you can ensure that all seems to look and function appropriately before you complete the migration process.
How To Find and Edit Your Mac Hosts File (In 4 Steps)
Now that you understand a little more about the Mac hosts file, it’s time to get to work. Below is how you can locate and edit this file in four easy steps.
Step 1: Open Your Terminal Application
To get started, you’ll first need to access and open your Terminal application. Also, note that you’ll need root user privileges to do this. If you don’t have these privileges, you can log in with an administrator account and enable the root user account.
You can find the Terminal application by navigating to Finder > Go > Utilities.
Doing this will bring up various tools that you can use to control your Mac device. Next, click on the Terminal application.
Alternatively, you can also hit F4 to open the launcher. Then type “terminal” in the search field, and select the Terminal icon when it appears.
Step 2: Access Your /etc/hosts Folder
Once the Terminal application opens, you’ll need to use a text editor in order to access the hosts file. We’ll use the Nano text editor for this tutorial because it’s versatile and user-friendly.
Start by installing the newest version of the Nano program. You can then enter the following command into the Terminal application.
sudo nano /etc/hosts
Next, press your Enter/Return key. You’ll then be asked to type in your administrator password:
Type in your password and hit the Enter/Return key once again.
Step 3: Begin Editing Your Mac Hosts File
After you type in your password and hit Enter/Return, you’ll be brought to the Nano text editor. At this point, your screen should look like this.
The following steps will depend on what exactly you’re planning to do within the hosts file. However, let’s imagine that you’re trying to add a new domain.
You can use your arrow keys to navigate to the bottom of the file. Then, on a new line, you can add the IP address of the domain you want to add:
As you can see from the above screenshot, we added “18.104.22.168” as our new domain. You will want to replace this, as well as “mydomain.com,” with whatever you’re trying to add.
Also, keep in mind that every entry you add should have its own line. For example, multiple entries should be in the following format:
If you want to cancel the changes you’re making, you can use your keyboard to delete the line(s) you’ve added. You can find a key menu list at the bottom of the editor window.
In addition to adding IP addresses, you can also block them to prevent your device from pointing a site their way. For that, you can use “127.0.0.1”.
When you’re done, you can press Ctrl + O to overwrite the existing file, followed by the Enter/Return key. You can exit by using Ctrl + X.
Step 4: Flush Your DNS Cache
If you’ve edited your Mac hosts file, but your changes aren’t going live, the problem may be with your DNS cache. Therefore, it’s not a bad idea to flush your cache when you finish editing the file.
Flushing your cache removes saved IP records. As such, this process will enable your browser to reflect the updated IP address from the edited hosts file.
To update your browser DNS records, you can enter the following command into the Terminal application:
sudo killall -HUP mDNSResponder
This step will ensure that your changes go live within your browser. You’ve now successfully edited your Mac hosts file!
Migrating your website to a new server can be a major task. Using a hosting provider such as Kinsta makes it a simple process. However, there may be situations when you want to mimic and test the site on your new server before completing the migration process. When that’s the case, you can do so via your Mac’s hosts file.
As we discussed in this post, you can find and edit your Mac hosts file by following these four simple steps:
- Open the Terminal application on your Mac computer.
- Access your /etc/hosts folder using a text editor such as Nano.
- Begin editing your hosts file to add the new IP addresses and domain names.
- Flush your DNS cache to apply the changes to your browser.
There are many advantages to manually editing your Mac hosts file. However, did you know that when you host your site with Kinsta, we provide seamless migration services as well as a temporary URL for testing purposes? Check out our plans today to learn more!
Save time, costs and maximize site performance with:
- Instant help from WordPress hosting experts, 24/7.
- Cloudflare Enterprise integration.
- Global audience reach with 34 data centers worldwide.
- Optimization with our built-in Application Performance Monitoring.
All of that and much more, in one plan with no long-term contracts, assisted migrations, and a 30-day-money-back-guarantee. Check out our plans or talk to sales to find the plan that’s right for you.