OUR BLOG

Keep up with the latest web trends and Hosting UK news.

Change my etcetera hosts?

This is a short article on the use of /etc/hosts – often referred to by our engineers as an edit, entry, or change to your “etcetera hosts” file. It is the simplest, most reliable, long lived solution to site previewing. The /etc/hosts file overrides DNS lookups. As such the world can look up your website and go to the live site, but you, with the change in place – go to another location. This means you can preview without redirection issues and work on a new site – and it only affects that machine. It’s not a new thing, and it’s not a hard thing – so here are some outline guides on how to get these in place.

This is in essence misdirection. DNS stays as it is. Your machine does not do DNS lookups. You get to where you want to be.

The format of the file is simplistic. The file contains IP addresses and aliases for that. The first column contains the IP address of the server you need the traffic to go to (this cannot be a name, cname, FQDN or URL).

You will see something like this at the top so:

127.0.0.1 localhost.localdomain localhost

You get the general idea <IP address> <alias> <alias> <alias>

At this point any lookups against localhost, or localhost.localdomain will resolve the 127.0.0.1 without the assistance of DNS as such – there are no lookups, locally, on your LAN, remotely… none. This is what we are trying to achieve. Each of these columns is separated by a space or many spaces.

Let’s take a practical example – our main website:

217.194.214.164   hostinguk.net    www.hostinguk.net

Where this matches the DNS at the time of writing – however – here we have the IP on the left, one of the things I need to resolve, and another. On the machine this change occurs – the domain name and the domain name with subdomain will resolve to the IP on the left. This will continue until that line is removed.

We are here because we want that to work – so let’s find out how to do that!

/etc/hosts Windows 8, 8.1 & 10

Current versions of windows make things a little harder for us (and the attacker) to make changes to system files. None the less the process is simple enough.

The goal here is to edit the file /etc/hosts which lives buried down within your system files.

Click on the SEARCH option at the bottom, and type in NOTEPAD.

Right click on this, and then select the top option – RUN AS ADMINISTRATOR.

Click on FILE and then OPEN – you need to open the following file:

c:\windows\system32\drivers\etc\hosts

Now add the string in the format you need there – IP space URL space URL and so on.

You are done. Force a refresh in your browser with CTRL-F5 or restart it to start testing.

/etc/hosts Windows 7

The goal here is to edit the file /etc/hosts which lives buried down within your system files.

The simplest way to do this is to Click on START and then RUN and enter the following:

notepad c:\windows\system32\drivers\etc\hosts

This will open up notepad with the file you need to edit. You will see any other entries you already have there. It is important to note that these files should be left there as they will be important. You are adding, and when the time comes – delete the entry you have added.

Going to the bottom of that file, add the entry you require, and then click on FILE, and SAVE.

You are done. Force a refresh in your browser with CTRL-F5 or restart it to start testing.

/etc/hosts on Ubuntu or CentOS

While you can use a GUI – the simplest way to get this is done is just to use a terminal.

$ sudo vim /etc/hosts

Sudo allows your user (assuming you have that level of access) to run as an administrator – however, to do so you will need to enter your password again before doing so. So at the prompt enter your password. If you have access, the terminal will change and look more like an editor opening a file with entries in.

Then go down to the last line of the file in the editor using your arrow keys and press o to insert a new line while switching to insert mode. Now add the string in the format you need there – IP space URL space URL and so on.

Once you are done – ESC, then :wq then return to write and quit. You are done. Force a refresh in your browser with CTRL-F5 or restart it to start testing.

/etc/hosts on macOS

This is very similar to the approach taken for a Linux machine. Open a terminal, and:

$ sudo vim /etc/hosts

Sudo allows your user (assuming you have that level of access) to run as an administrator – however, to do so you will need to enter your password again before doing so. So at the prompt enter your password. If you have access, the terminal will change and look more like an editor opening a file with entries in.

Then go down to the last line of the file in the editor using your arrow keys and press o to insert a new line while switching to insert mode. Now add the string in the format you need there – IP space URL space URL and so on.

Once you are done – ESC, then :wq then return to write and quit. You are done. Force a refresh in your browser with CTRL-F5 or restart it to start testing.

Leave a Reply