How To Setup Your Own Server In Local Host With Hosts File

What is a hosts file?
A computer hosts files are plain text files used in an operating system to map hostnames to IP addresses. These are network configurations that decide how the operating system handles network requests. Traditionally they are named as “hosts”.
Purpose
Hostname Resolution
As you all know, every resource on the internet has an IP address associated with it. The primary purpose of the hosts file is to resolve hostnames to IP addresses. On every computer, when you search for a website URL, the operating system first checks the hosts file for the IP address before querying the DNS server on the internet.
Local DNS
You have already seen that the hosts files come before the DNS server on the internet. Since they can act as a local DNS service for your local machine, this comes with several perks. One of the important use case is, it overrides the mappings from the DNS.
In this article you will see, how we can override or create a DNS mapping using our local hosts file.
What’s in it ?
- Each line in a host file represents a single mapping of IP address and its host name.
- IP address is added as the first text field followed by one or more host names.
- Here the IP address can be of IPv4 or IPv6.
- Each section is separated by a tab or white space.
- You can comment a mapping by using the ‘#’ symbol.
- Here the IP address `127.0.0.1` is mapped to `localhost`.
- Here all the users accessing example.com are redirected to the `192.168.0.1`.
Use Cases
Blocking or Redirecting website
Have you ever come across an error page while accessing instagram or facebook from your school or office wifi? Similarly, you can achieve this from your own local machine. In the above example, we are redirecting users accessing example.com to the local IP addresses. We can block or redirect users from accessing certain websites.
Local Development
Developers often use host files to create local domains for testing purposes. By mapping a domain to 127.0.0.1, developers can work on a local version of a website without affecting the live version on the internet.
Creating Shortcuts
You are no longer required to remember that IP address of your printer or the router. You simply add them in your hosts file.
Creating a personalized website experience
You don’t need to pay those hefty dollars for your favorite website domain name anymore, at least not in your local machines. In the hosts file you don’t have to add the exact IP address and domain name mapping. This can even be a domain name that has not yet been created, why to wait? Create and use your favorite domain names for your local applications.

Where to find it?
Depending on the OS you are using, the hosts file location can be varied.
- Linux
- /etc/hosts
- macOS
- /etc/hosts
- Windows
- c:\Windows\System32\Drivers\etc\hosts
How to edit a host file?
Now we have seen what we can do with a host file and where to find one, let’s apply what you’ve learned.
- Based on the operating system you are using, locate the hosts file location.
- Make sure you are having administrator privilege or credentials to make the changes.
- Open the hosts file as an administrator or with an admin privilege.
- Add the entries or update the entries in the hosts file and save it.
Tada!!!! Now you have set up your own server in your local machine.
Best Practices
Updating or adding entries in your hosts file never involves any risk or breaks your operating system. Since the hosts file is responsible for handling all the network requests, always be mindful about the changes you are adding to the file.The following are the simple and best practices while dealing with a hosts file.
- Remove that entry which is no longer required.
- If your server has multiple environments(local and remote servers) which are assessed through the same production URL, make sure you have commented or enabled the right environment.
- Always use hosts switching applications while dealing with multiple environments for development and testing.
Read More
Credits:
Photo by Domenico Loia on Unsplash