o7planning

How to use the "hosts" file?

  1. hosts file
  2. Windows
  3. Linux (Ubuntu,...)
  4. Mac OS

1. hosts file

The hosts file is a file that is available on Windows, Linux, and Mac OS operating systems. It is used to map a domain name to an IP address. But it only works on that computer.
To be easy to understand, we give a situation. You have a website and are deploying it on a server that has an IP address of 1.2.3.4, and you can access it via IP:
You want to use abc.com domain name (or any) to access your website instead of IP. You need only to configure the hosts file and you will get what you want. Of course it only works on your computer.

2. Windows

On the Windows, open the file:
  • C:\Windows\System32\drivers\etc\hosts
Add a Configuration Snippet :
C:\Windows\System32\drivers\etc\hosts
1.2.3.4      abc.com
1.2.3.4      mydomain.com
In case, your website is being deployed on the computer you are working on, you can access it via localhost or configure the hosts file as follows:
C:\Windows\System32\drivers\etc\hosts
127.0.0.1      localhost
127.0.0.1      abc.com

3. Linux (Ubuntu,...)

For the Linux operating system, the hosts file has the full path:
  • /etc/hosts
Way of configuring the hosts file in this operating system is the same as that in Windows. However, you need to know commands to edit the contents of the file.
1- Open Terminal window.
2- Execute the following command:
sudo nano /etc/hosts
3 - Add the configuration snippet:
1.2.3.4      abc.com
1.2.3.4      mydomain.com
4 - Click Ctrl + X to save changes.

4. Mac OS

For the Mac OS operating system, the hosts file has the full path:
  • /private/etc/hosts
Way of configuring the hosts file in this operating system is the same as that in Windows. However, you need to know commands to edit the contents of the file.
1- Open Terminal window (Applications -> Utilities -> Terminal).
2- Execute the following command:
sudo nano /private/etc/hosts
3 - Add the following configuration snippet to the end of the file:
1.2.3.4      abc.com
1.2.3.4      mydomain.com
4 - Click Ctrl + X to save changes

Ubuntu Tutorials

Show More