How to change your hosts file | Quebec web hosting

You may already have been asked to modify your “hosts” file on your computer. The main reason behind this modification would be to help us see a website under development.

If you need to move your website, maybe this information will be useful to you. For those who want a no-hassle service, we offer a worry-free WordPress Hosting service in Canada. For others, take a look on our website to find a Canadian hosting service that will make your life easier.

In this article, I will attempt to explain what this file is used for and how to modify it.

For those who want a little shortcut, here are some links to help you navigate the article more quickly.

What is the “hosts” file used for?

To understand how the “hosts” file works (more info on wiki), we must start by looking at how a computer interacts with the internet.

When we want to see a website in our browser, we begin by writing the site address (URL) that we wish to view. To be able to display the site, the computer will first need to find the site’s IP address.

When a computer is connected to the internet, it has many resources available to it. Two of those resources serve in identifying the IP address assigned to a website. They are our “hosts” file and DNS servers.

Here is what the search path may look like when a computer searches where a website is located.

How the hosts file works | Web hosting

As illustrated above, the computer will begin by verifying in its “hosts” file to find where the website could be. If it doesn’t find the site, it will then verify its DNS servers. For your information, the DNS servers used by your computer are given by your Internet provider.

Where is my hosts file?

The hosts file hiding place will vary from an operating system to another. I’ll show you its exact location in the three most popular operating systems: Windows, Mac and Linux.

Finding the file in Windows

In the Windows operating system, the file is buried deep in hidden directories. In short, the directory is as follows:

%WINDIR%\System32\drivers\etc\

The majority of Windows installations, the access path will be in drive “C:”:

C:\Windows\System32\drivers\etc\

How to open the file in Windows

The simplest method to modify your hosts file is the Windows Notepad application.

  1. Open the notepad as an administrator. By making a right clic on the application, this option will be offered.
  2. Go in the “file” menu and “open”.
  3. In the bottom right corner of the window, change the file type from « *.txt » pour « *.* ». Without this adjustment, the hosts file will not be visible.
  4. Follow the access path according to your operating system, and clic on your hosts file.
  5. Carry out the desired modifications.
  6. To save these changes, go in the “file” menu and “save”. You can also use the keyboard shortcut “CTRL + S”.
Hosts file in notepad | Web Hosting Canada

Finding the file on Mac or in a Macbook

Mac files are classified in the same way as Linux operating systems.

The access path to modify the hosts file in Mac is the following:

/etc/

How to open the file in Mac

You must use the command line to modify a “hosts” file on Mac.

  1. Open the “Terminal” application.
  2. Insert the following command to open the file: “ sudo nano /etc/hosts ”.
  3. Carry out the desired modifications.
  4. To save these changes, type “CTRL + O”.
  5. To close the edition window, type “CTRL + X”.

Finding the file in Linux

Access to the file in Linux is the exact same in Mac. The file is in the following directory:

/etc/

How to open the file in Linux

You must be in the command line interface in your Linux system.

  1. Open the command line interface.
  2. Insert the following command to open the file: “ sudo nano /etc/hosts ”.
  3. Carry out the desired modifications.
  4. To save these changes, type “CTRL + O”.
  5. To close the edition window, type “CTRL + X”.
Hosts file in Linux with nano | WordPress hosting Canada

How to modify the hosts file

Now that the most complicated steps are behind us, we can finally modify our hosts file.

The hosts file is relatively simple to understand and modify. The first thing we must understand is that any line beginning with a “#” is a comment. Thus, it can be ignored.

Each line in the file is a new entry and must always have the following syntax:

IP Address [domain list]

Here is a simple example:

111.222.333.444 mydomain.com

If you have many domains and subdomains that need to be directed towards the same IP address, you can place them on the same line. For example:

111.222.333.444 mydomain.com  www.mydomain.com  mysite.ca

The important part is that each domain is separated with a space.

Summary on the hosts file

It can be found in the following directories:

  • Windows: %WINDIR%\System32\drivers\etc\ or C:\Windows\System32\drivers\etc\
  • Mac: /etc/
  • Linux: /etc/

It must be opened with the administrator access and can be opened with:

  • Windows: The « Notepad ».
  • Mac: The « Terminal ».
  • Linux: With « nano » or « vi » by command line.

Each line is a different entry and must be formatted as mentioned below:

IP Address [domain list]

111.222.333.444 mydomain.com

Hopefully this guide has helped you better understand how your computer works and its usage of the hosts file.