How to edit your Mac hosts file (Mac OS X)

codesm - editing mac hosts fileHow to edit your Mac hosts file (Mac OS X)

Sometimes you need to view a site on a development server. It’s something that I use regularly to connect to a development server using the regular site domain but mapped to a development server’s IP address. This way you can maintain your DNS as it is, and work with the development site’s future domain in a real and meaningful way. Some CMS and applications work best this way when you’re doing active development, and you’re not ready to adjust your domain’s zone file quite yet.

It’s a pretty straight forward and easy process, but a word of caution: if you’re not familiar working with the command line, proceed with caution. You can screw stuff up in a quick hurry if you’re not careful!

Start Terminal on Your Mac

In order to change your hosts file on Mac OS X, you need to use the Terminal app. You can find it by going in to your LaunchPad and starting to type “terminal” and hit enter to launch it, or going to your applications folder, and then to your utilities folder, and it will be in there too.

Open Your Hosts File

Once you get terminal started you will want to run the following command:

sudo nano /private/etc/hosts

This will open up your hosts file in Terminal allowing you to make edits. Make sure you include the first slash before private to get you to the right directory.

Enter Your Password

Next, Terminal is going to ask you for your Mac password, so go ahead and enter that next. As you’re typing your password the cursor will not move, so hopefully you know your way around your keyboard. You can also copy and paste your password in.

Once you enter your password, you’ll now be in the edit screen for your hosts file.

Make Your Entry in Your Hosts File

The lines that start with # are “commented out” so that they’re not read by the system, but allow you to read any important info that the creator of the file wants you to know about. Learning how to comment things is a big help when you’re working with code, and if you need to toggle back and forth between a live site, and one that’s on a remote server, you can simply comment out the line that routes that domain to the development server, in order to get back to the original site.

It should look a little like this:

# Host Database
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##     localhost    yournewdomainentry.com

Use your arrows to scroll down to an empty line below the ones that are already there by default. Once you’re on a new line, add your new entry by adding the IP address of the server you want to point your domain to, then followed by your domain name. You may want to include both yournewdomainentry.com and www.yournewdomainentry.com. That way it won’t matter whether you include the www or not when trying to get to your new destination that you’re mapping – both the regular domain and the www domain will both resolve to the same place.

Save The Hosts File

Once you’ve made your adjustments by adding a new line to the hosts file you’ll need to save it so hit control+o to save the file. The system will prompt you if you want to write to the same file. Since you’re writing to the same place, just hit enter to save it as the existing file. Next to complete the edit and get out of the hosts file, hit control+x to exit and that will bring you back to your main command prompt.

Make note: it’s control not command.

Flush Your DNS Cache

You should now be good to go. If for some reason your new site mapping isn’t working correctly you may want to flush DNS cache and you can do that while you’re still in Terminal.

dscacheutil -flushcache

Once you’ve got everything done, go ahead and check your hosts file edits. Your new mapping should be in place! Once you’re done and you’ve pushed your site live, make sure to remove your domain mapping, either by commenting it out, or just completely removing it, so that it resolves naturally. You don’t want to be stuck looking at your development server while the rest of the world is looking at the live version.

See Also:

How to choose the best hosting service when starting a blog


"Want to know how to choose the best hosting service when you start a blog? I know it can sound intimidating and overwhelming, but it really sounds a lot worse than it actually is. But a good hosting company is going to make the entire process easy for you. So choosing a hosting company for your website […]"

Keep Reading...

Can I pay for ClickFunnels with PayPal?


"Unfortunately, as of February 2021, PayPal is not among the available payment options when you’re trying to sign up with PayPal. But there’s an exception! If you use a PayPal MasterCard debit card when you sign up, you can use your PayPal account that way. So while there isn’t a direct integration to PayPal on […]"

Keep Reading...

How To Write A Blog Post Fast


"Writing blog posts fast is something we all want to do. But for a lot of us, writing speed is not something we’ve got in our toolbox, at least at the beginning. If you’ve spent any time at all creating content then you know that it can take a lot of time to create a […]"

Keep Reading...

Setting up Google Analytics & Search Console | How To Make A WordPress Blog Step-by-Step 2020


" Feeling like you don’t know what’s going on with your website is a terrible feeling. The problem is you don’t have any idea about what’s going on and whether or not any of your creative efforts are generating results – or not. And if they’re not generating results, then what are we all doing […]"

Keep Reading...

Why do I need a sales funnel for my online business?


"Here’s the deal: An online sales funnels is like having the very best salesman you can imagine working for you online 24/7. It doesn’t ask for vacations or raises. It doesn’t call in sick. It’s simply working around the clock, 24/7 365 days a year to win customers for your business. And online sales funnels […]"

Keep Reading...

How to Generate Leads For a Medical Spa in 2021


"How to generate leads for a medical spa You’ve spent the years and years of hard work to become an expert who can literally transform the lives of your clients. You’ve refined your skills to a level that only fractions of one percent of people on the entire planet earth can perform. And you’re an […]"

Keep Reading...

Is ClickFunnels Legit?


"With all the hype surrounding the ClickFunnels platform, it’s a fair question to ask whether or not ClickFunnels is legit or not. I get it. One of the reasons you may have this question to begin with is the generosity of their affiliate program. And when there is an opportunity to earn serious affiliate commissions, […]"

Keep Reading...

Can I start a blog for free?


"You can totally start a blog for free. All you need to start a blog for free is a little inspiration, a lot of perspiration and to sign up for one of the numerous free websites that will allow you to start a blog for free. You can check this post here for several options […]"

Keep Reading...

What Type of Blogs Make the Most Money?


"What type of blogs make the most money? Starting a blog is one of the first things that people think about when they start thinking about trying to make money online. But for most beginners they don’t know what type of blogs make the most money, so they get stuck and end up wasting a […]"

Keep Reading...

6 Crucial Questions About Features & Functionality


"Let’s start with a seemingly simple question: What do I need to know to figure out how much a website is going to cost? I’m going to answer that question with a question. Actually, six different questions. I’ve built a lot of quotes and responded to a ton of RFPs to do website builds. In […]"

Keep Reading...

Join The

Leave a Reply

Your email address will not be published.

Name (required)