How to edit the hosts file on a Mac
Articles,  Blog

How to edit the hosts file on a Mac


Hi, this is Andrew at 20i and today we’re going
to be showing you how to perform a host file amendment on a Mac OS device.
Knowing how to do a host file override is a great tool to have in your arsenal for
any web designer or provider of hosting services. It essentially allows you to force
a host name – for example a domain name – to point to a particular IP address for
your local machine so if you wish to test a website out that you’ve just
migrated – perhaps to our platform – and you don’t want wish to make any fiddly DNS
changes, then you can simply perform a host file override for your local
machine. No one else in the world will see it and you can essentially see how
that domain name or hostname that you’re testing will look on our platform,
whether it has migrated over successfully, etc., etc. It’s also great
knowing how to perform a override if you are unable to or for some reason, you don’t
wish to use the temporary URL that’s available in the control panel and
simply wish to test over the live domain, whether you’re building a site, or, as I
said, just testing it if you’ve migrated over. So in order to perform a host file
amendment we need a host name and an IP address. This is because a host file is
essentially just a text file on your local machine that maps hostnames to IP
addresses and we simply wish to force a domain name on a package here to point
to a particular IP address. So getting started we wish to go to ‘Managed hosting’. Once we’ve done that we then just wish
to ‘manage’ the package that we want to do this for. Once here we can see under
account information that the name of the package is 20isupport.com and the IP
address in question: 185.151.28.150. As I say you can do
this for any host name you like it doesn’t have to be…you can for example
your subdomain that might be on a package but for this video we’re just
using the primary domain that’s on a hosting package here.
So now that we have both the the hostname on the IP address we want
to open up the hosts file on our local machine. We can do this a few ways: we can
enter ‘command’ we can hit ‘command + space’ and type in terminal there if we wish to
do so. The easiest option will be to use Launchpad, so we’re just going to click
on Launchpad and we’re going to type in ‘Terminal’. Once we’ve done that we should
then get the following. In order to open-up the hosts file on our local machine we
need to type in the following command: ‘sudo vim’ – vim is the name of a text
editor – ‘/etc/hosts’. Once we’ve done that once we’ve written that
out we then wish to hit Enter. When you hit Enter you may be prompted for a
password. This will be just the password for your particular Mac user, so don’t be worried if that pops up. Enter-in the password and you should be fine and that
should then take you to the file. So just hit Enter and as we can see I’ve been
prompted for a password so I’m just gonna enter the password for this
particular Mac user and then that should then take me to the file. Once we’re in
the file as you can see there are a few things here we need to get this into
what we call ‘Insert mode’. To get this into Insert mode we just need to press
the letter ‘i’. Once we do so you should see at the bottom that it says ‘Insert’.
This basically means we’re free to edit the file now. So, in order to map a host
name to an IP address we need to…there should be lines similar
to this in any case you just want to make sure that after the last instance
or whatever is in this file just add a new line by pressing Enter. You can
browse using the arrows on your keyboard. So here we want to map 20isupport.com
or whatever our host name is or your host name is to the IP address previously
mentioned. So to do so, we’re going to copy this – no sorry – we’re going to
copy this and we’re going to paste it here. So that’s the IP that we wish to
force the hostname to go towards. Once we’ve done that we then want to grab the
hostname and then enter it there. Once saved this will then force 20isupport.com to go towards this IP address and view that particular…view that server
for this host name. However this will only do non-www: essentially just
20isupport.com. If we also wish ‘www’ to point to this and in
order to be viewable in testable we need to enter ‘www’ and then we need to
paste that in using ‘command + V’. Once we’re happy with that we then just need to
‘save’ the changes that we’ve made. In order to save the changes that we made
that we’ve made here we need to press ctrl + C on our keyboard. If we do so
that should then remove insert and just have a blank space there. If that’s the
case we then just want to hit ‘shift + :’. So you should be able to see a colon
there. If that’s the case you then just want to enter ‘W’ followed by an
exclamation mark which can usually be found by holding shift + 1. Once you’re
happy that’s the case then hit Enter. As you can see here it’s saying that
/private/etc/hosts has been written and that has been saved. In order
to get out of the file we just need to press ctrl + C again. We then just need to
insert a colon, so shift and colon and then we just need to do ‘Q’ – letter ‘q’ – and
an exclamation mark. This will then force- quit the file. So as we’ve shown there
that’s how you amend a host file on a macOS device. As we say it’s a great tool to use if you’re migrating from
another provider or you’re simply creating a new website on our platform
that you wish to test. If you have any issues please do get in touch with our
support team and we’ll be more than happy to help. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *