How to Edit your Hosts File on a Mac

Your computer uses the HOSTS file to look up/resolve domain names to an IP address. This file can force your computer to resolve your website to a specific IP address rather than using the DNS entries indicated by the domain’s name servers.

Manually configuring your domain with the IP address of your InMotion Hosting server will allow you to point a domain name to a different server location using an IP address. A HOSTS file change is intended to be temporary and should not be used with the published website.

Video – How to Edit Your HOSTS File on a Mac

Edit HOSTS File in a Terminal Client

You can use a terminal client application, or the one Apple provides in its operating system when editing your HOSTS file.

  1. Open the Terminal client of your choice.
  2. Edit the HOSTS file in the terminal window using one of the commands below.
    sudo nano /private/etc/hosts

    For newer versions of Mac OS, use this command:
    sudo nano /etc/hosts
  3. Enter your Apple login password for your device.
  4. Edit the HOSTS file by adding these two lines.
    SERVER_IP_ADDRESS domain.com
    SERVER_IP_ADDRESS www.domain.com

    This is an example of how it would appear in the HOSTS file:
    86.75.30.9 domain.com
    86.75.30.9 www.domain.com

    NOTE: Replace “domain.com” with your actual domain name. If you are unsure which IP address to use, please contact our Support Department for further assistance.

  5. Save your changes by pressing Control-O on your keyboard, then hit the enter key to accept the filename.
  6. Exit the editor by pressing Control-X.

How to Flush the DNS Cache

You may need to flush your Mac’s DNS cache for the HOSTS file change to work. Use the following command to flush the cache:

  1. Open your Terminal client.
  2. Type in the following command: dscacheutil -flushcache
  3. Hit enter to run the command.

This completes the HOSTS file modification. You can now see your website using a domain name pointing to the specific IP address you added to the HOSTS file. If you no longer need the changes in the HOSTS file you can repeat the steps above and remove the changes you added.

If you are using Windows, please refer to our guide on how to Modify Your Hosts File Using Windows.

Learn more about DNS management with our DNS and Nameserver Changes Education Channel.

Secure VPS HostingEnjoy high-performance, lightning-fast servers with increased security and maximum up-time with our Secure VPS Hosting!

check markLinux VPS check markcPanel or Control Web Panel check markScalable check markWebsite Migration Assistance

Linux VPS Hosting

AC
Arnel Custodio Content Writer I

As a writer for InMotion Hosting, Arnel has always aimed to share helpful information and provide knowledge that will help solve problems and aid in achieving goals. He's also been active with WordPress local community groups and events since 2004.

More Articles by Arnel

63 thoughts on “How to Edit your Hosts File on a Mac

    1. Hello Felipe – Ctrl+X will not generally work in the terminal. You would need to be in the editor to use it.

    1. Hi Ana, the hosts file is something on your computer that overrides the normal process of looking up internet addresses. The main practical application for this is letting you work on a different version of a website than the one the domain actually points to. This is useful for working on websites that will replace the current version of a site, but aren’t yet ready to be publicly accessible. The location varies, on Macs it’s been located at /etc/hosts lately and on Windows it’s usually c:\windows\system32\drivers\etc. The above article for Macs, or other article on Windows hosts files, should help you set one up if you want to try it out. Hope that helps!

  1. I’m not able to get past the first command: 

    sudo: /etc/sudoers is world writable

    sudo: no valid sudoers sources found, quitting

     

    sudo: unable to initialize policy plugin

    Please advise.

  2. Hey, do you happen to have any other suggestions? I have done this previously on this mac… different version of OSX. Now I am running High Sierra 10.13.4 on an early 2011 macbook. I am the only user with the only account. I have confirmed that I am on an admin account.

    When I open terminal and enter sudo nano /private/ect/hosts I am taken to a version of the terminal that looks almost correct its the GNU Nano 2.0.6 page but there is no information in the main terminal window. No addresses…ect. When I control+O I do also see this “Error writing /private/ect/hosts: No such file or directory”.

    Im glad to know that this will work on High Sierra, just not sure what else I could be missing. Appreciate any help anyone might be able to provide.

  3. I just got the confirmation email that my website was transfered from weebly to in motion hosting. I ran all the code through my terminal (im on mac), looked up my website in my browser and it still show that the website is from weebly. (I know this because there is a weebly logo in the search bar at the top.) Is this because my old weebly domain is still pointing to the old weebly site? or do I need to point the domain to the in motion site for this to work?

    1. Yes, you need to point the domain to InMotion Hosting servers in order for the domain to retrieve the contents that are hosted at InMotion. You have the option of pointing to the InMotion nameservers or using A records at your current domain registrar (if they provide DNS options). Your best bet may be to use the nameservers.

  4. Hey, 

    I’ve got OS Sierra, and it won’t let me change the hosts file. When I press CTRl O and press ENTER to save to /etc/hosts/ it says there is no such file or directory? I’ve restored the original hosts file, but still won’t let me make any changes. Can anyone help? Thanks!

    1. Check your user and make sure that you are logged in as an Administrator. You need to go into Users, then check this option:

      https://www.screencast.com/t/4CxKFUKfa

      I am running HIgh Sierra on my Mac and we are able to change the HOSTS file. 2 of us here use Macs and we have confirmed that the instructions do work. Please check your permissions and then try it again.

  5. Is it possible to use the address given from eg

     

    ssh MACBOOKNAME.local

    on the remote showing as

    SSH_CLIENT=fa20::907:1051:3cc9:0a33%en0 49867 22
    SSH_CONNECTION=fa20::907:1051:3cc9:0a33%en0 49867 fa20::907:1051:3cc9:0a33%en0 22

    (in the environment, using eg “env | grep SSH” on macOS High Sierra, and surely many other platforms for that matter, incl beyond BSD, Darwin, macOS etc; ALTHOUGH the Bonjour serviices maybe are not available? On QNAP NAS, for example it is though, under the name avahi, if my memory serves…)

    So, with ping CHOSENBONJOURNAME.local specifically on Mac OS X and macOS we can fetch the IP address.

    However, with “ssh CHOSENBONJOURNAME.local” working too, I am ASKING A QUESTION here which seems beyond trivial Google search research.

     

    Q: is it possible to somehow bind fa20::907:1051:3cc9:0a33 or other, presumably so-called MAC-axresses aka hardware addresees per interface. MAC here of course does not at all refer to Mac as in Apple etc, but to networking since the 60ies or so, an acronoymfor Machine Address C…? etc.

    In the host file we can obviously deal with higher-level of TCP/IP. Viz. 10.10.10.10 or 192.158.255.1 or whatever your local network or even WAN is using.

    We can also run various DNS servers, bind and whatnot.

    THE QUESTION IS HOWEVER if there is some easy way of FORCING such hardware adresses to cause resolution of FQDN, hostnames such as indeed id.local in OTHER contexts than eg macOS or Homebrew SSH which accidentally does support Bonjour but connects, as environment on the remote host shows (the MAC addresses, I think?, of the Mac that makes the connection, the environment shown is for the host one connects to, whereby such variables are set, but not with IP for Bonjour, but for hardware addresses).

    Basically, it would be suoerb, especially in combination with DHCP, to have a lightwegith way of resolving hardware addresses to IP addresses and also FQDN. The hostfile – is it capable of that IN FACT?

    Or must one go deep here, into routes etc or even fiddle with the DHCP server to ensure a static LAN address or whatnot is assigned whereby the problem obviously disappears. However, a computer may well have several ways of connecting to whatever LAN in question. Perhaps using an USB hub or Tunderbolt dock, sometimes, and these are then other interfaces. DHCP ID can handle this b ut it becomes VERY cumbersome. THERE SHOULD BE a very trivial way to get around this.

    And I therefore ask – HOW can this be done? Surely thetre is a way…

    1. I am unaware of a method that you can use to map Hardware MAC addresses to FQDN/Hostnames via the hosts file. The hosts file merely handles (local) DNS resolution for FQDN to IP addresses.

      I believe you would need to review your router capabilities to configure such a network that would force a particular hardware MAC address to be used for a specific IP address. I understand this can be done but varies depending on your router’s manufacturer.

      Apple MacOS High Sierra does support forcing DNS resolution based on the interface (WiFi, Thunderbolt Bridge, etc.) being used, but I do not see where you can handle DNS resolution for Hardware (MAC addresses), just FQDN to IP. I hope this addresses all your concerns. Feel free to leave a comment if you have any other questions.

  6. Thansk for help and instructions!

    I’ve done the tesging by editing the hosts file as per above. Looks good.
    How do I know for sure it’s pointing to the inmotion server though? Is there any other indicator showing the host while the website is displayed?

    I then went on to undo the changes as instructed above. Everytime I open terminal and do steps 1, 2 (and skip step 3), 4 and 5, the inmotion server name/number is still appearing within the terminal. Wouldn’t that mean it still forces it to point to the inmotion server? Is it really undone – did I miss something?

    Thanks!

    1. If the IP address and hostname are in your “hosts” file, then it is resolving to the IP. You can verify this with a ping/traceroute (it should list “INMOTION” on the later hops), or a phpinfo page (it will list the server name on top).

      Thank you,
      John-Paul

  7. If I am migrating a site from one server to another using UpDraftPlus backups, and I want to test it before going live on the new server, is it better to update my host file or end the wp-config.php file?  Which method will cause fewer (ideally, zero) problems migrating files and database?

    Thank you,

    Ray

    1. The hosts file modification is best as it will require no configuration modification on the site if it will be using the same domain name.

  8. I performed everything fine..I think. I go to the site and it opens up like normal. Is this how it’ll look on your servers? How else can I check incase something went wrong with this host file change process and it’s just normal

    1. A host file mod essentially is showing you what it looks like on the server. All you are doing is telling your computer to go directly to the server to show the site and skip looking up the DNS records to do it.

  9. Hi. I changed the hosts file on my laptop to test a site and it worked fine. I’ve now come back to do it again and it’s changed all the computers in the office going to the original site and they now see:

    Apache 2 Test Page

    how can I get all the other computers to see the original site?

    1. David, if you made a host file modification on your laptop, that should not affect any other computers in your office. If you updated your DNS records for your domain, and the other computers in your office now see the ‘new site’ you may need to either revert your DNS records, or make host file modifications on the other computers of your office.

    1. Gabriele, you should find a link near the very bottom of this page that says “Email Notifications”. Click on that, then select ‘No’, and click ‘Update’.

  10. Geez!!! there must be an easier way to test this! OMG. It does not work. This is for hard core developers. Why is this even an option??? This is mega complicated – I need help here. does the name servers be pointed to InMotion or not?

    send me an easier way to handle this issue here. that would be nice. Otherwise, I have to cancel this hosting plan. Seems this is only good for developers.

    1. Sorry to hear you are having trouble. If you want to host your site with InMotion, yes you should point to our nameservers.

      The above guide is only if you want to test your site before pointing the nameservers.

      We would be happy to help check your site, but will need some additional information such as the domain name.

      Thank you,
      John-Paul

  11. When I’m testing the website, is there a way to check if it’s testing from the new webhost or still using the old one?

  12. I followed all the instructions and all worked well. Tested the site and it worked. Then I was concerned that I was seeing the old site and not the one I transferred to your hosting. So, I checked the IP of both, with the host file edited and cache flushed and with the host file lines commented out and cache flushed. The IPs were the same both ways. I guess this is not a good site test for me. Is there any other way to do this? I don’t want to pull the plug on the old hosting site until I am positive everything is working perfectly here.

    I have also tried the temporary URL with not much success, which is why I wanted to be sure of the testing from the host file.

    Thanks for your help!

    1. Hello D’Ann,

      I’m not quite sure what you’re trying do. I think that you’re confused about how a hosts file change works. When you make a change and assign to the IP address, then whatever URL that you have placed in your HOSTS file will be used when you try to view that particular URL. And it is specific to the URL. For example, if you added www.example.com to your HOSTS file and assigned to the IP address, then when you typed in that URL in your computer it would go to that IP address and only that IP addres per your hosts file change.

      This is a procedure used by many developers. It basically allows them to setup a particular domain name and use it on a computer for development without having to change the existing URL while it that location is being developed. So, yes, the IP for the OLD site would not be changed until you change the DNS. In the meantime, on the development computer, the specific URL that you modified in the HOSTS file woulud be used for development. For example, let’s go back to example.com. IF the domain name of the site is “example.com”, but your hosts file change is set to “www.example.com”, then when you type in “www.example.com” on the development computer, it would be going to the IP address that you specified in the HOSTS file. This IP could be the location of the new site, where you have you installed the software using the normal domain name. Nobody BUT the developer would see the development site because they are pointing to the development server through the IP change in the hosts file.

      I hope this clarifies the issue. If you have any further questions, please let us know.

      Kindest regards,
      Arnel C.

    1. Follow the same steps again to access the file. Then you can erase the line you added. This will deactivate the modification. Make sure to save the file. Also, I recommend you clear your browser cache to make sure you have reloaded the active site.

    1. Hello Tania,

      It sounds like you have it resolved. Remember that “nano” is an editor function within the terminal. The changes you make in the file don’t apply until AFTER you have saved the file. You can always open up a second terminal window and type nano -help, then click hit enter. This will bring up the commands you can find on using nano. Hope that helps! If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

    2. I can’t get past the control O part — a navigation window appears. Am I missing something? My machine is currently running OS 10.11.3 El Capitan

    3. Hello Chrismay99,

      Holding down control and hitting o should save the file then control + x should exit the file. Are you getting an error trying to do this?

      Best Regards,
      TJ Edens

  13. Hi,

    I used your instructions which worked are are really great, however now that I want to remove the info in the terminal it I am faced with the issue of when pressing ctrl x nothing happens and also, do I try delete the info or do I just re enter all instuctions, leaving out instruction 3?

    Thanks

    1. You can simply edit the file, and place a # symbol in front of the line. This will render it inoperable as the system will read the line as a comment.

  14. Hi,

    I have somehow become stuck in the terminal when trying to remove the information.  When pressing ctrl x nothing happens.

    Also am I supposed to remove the information or do I just repeat the sequence leaving out point 3?

    Thanks

  15. I did this the other day and it worked fine. Today, when I went back in to do some more work on the site, I couldn’t get to the wp-login/admin page. 

    I “undid” everything, and then re-entered it all, but still no luck. Any suggestions?

    1. Hello Bike2n,

      What domain/IP are you trying to access so we can test it from our side to make sure it is not something with the server hosting your website. Have you made any changes to the .htaccess or the URL settings within WordPress?

      Best Regards,
      TJ Edens

  16. Thanks, and yes, the folder /artist does contain the files. I don’t recall creating a folder called artist, so not sure where that came from. That folder doesn’t appear anywhere among the files and folders I uploaded via FTP. Hmmm.

    Is there an easy way to move the files from /artist to the /public_html directory?

  17. I edited my hostname successfully, but now only see:

    index of/

    • artist/
    • cgi-bin/

    on the test home page. What am I missing?

    1. Hello Pamela,

      Thank you for your question. This seems like you are resolving to your server, but there is no website in the root folder.

      Instead, it looks like your root folder has a folder named artist, that may contain the files. Most likely this folder needs to be moved up a level. In other words, move the content of the /artist folder up 1 folder.

      We have a helpful article that explains What directory you should put your files in.

      Thank you,
      John-Paul

  18. brilliant thanks for a well detailed easy to understand process for a newbie to this backend stuff much appreciated

  19. I followed instructions. but How can I see the website????  You did not explain it here.  Is it locahost/domainname.com.  Please advise.

    1. Hello Muhammad,

      Thank you for your helpful suggestion. I have added this to the article as recommended.

      We welcome any additional suggestions you may have.

      Thank you,
      John-Paul

    2. Hello Rachel,

      Thank you for your question. After changing your hosts file, you will then navigate to your domain.

      For example, if you create:
      67.199.146.25 domain.com
      67.199.146.25 www.domain.com

      You will test by navigating to:
      https://domain.com
      or,
      https://www.domain.com

      Be sure to replace domain.com with your actual domain name, and replace the 67.199.146.25 with your actual shared IP address.

      Thank you,
      John-Paul

    1. It would help if u added sudo in front of the dscacheutil -flushcache command for flushing the DNS since it wont work without sudo added to it. some people are new to linux commands so it would help if it is added.

  20. I tried the control (+/=) bottom and c. And still do nothing. I guess what should i be looking for because All i’m seeing is the host database. 

    1. Based on your reply, it sounds like you’re trying to press 3 different buttons at one. To clarify, press the control and c buttons at the same time, and not any additional keys.

  21. I am stuck on step 4 as well. I pushed both control-x and control+x and it wont go back. So I’m not sure it saved. 

    1. Within the nano text editor, ctrl+x will indeed quit the exitor that is running inside the terminal window. Be sure that you are pressing ctrl+x and not command+x.

Was this article helpful? Join the conversation!