The Purpose of a Hosts File
In order for a domain to point to a server it must be mapped to a DNS resolver. For instance if you type onpointplugins.com
in your browser, you are sent to IP address “108.62.123.43” and the website loads. Almost always, the DNS resolution happens through an exchange of DNS servers both within your computer, router and servers located in the cloud.
A Hosts files allows you to override all other DNS resolvers in favor of file’s contents for specified domains. For example, if you enter “192.100.1.10 onpointplugins.com” in your computer’s hosts file, all requests for “onpointplugins.com” will go to “192.100.1.0” and the site will not load. Any domain may be added the hosts file.
Hosts file format
IP Address | Domain |
Examples
192.100.1.10 | onpointplugins.com |
127.0.0.1 | local-test-site.dev |
Use Cases for Editing Your Hosts File
Some of the more common use cases for editing your hosts file include:
- Block known malicious sites.
- Prevent access to particular sites for parental control.
- Point to a specific server in a cluster.
- Server domain configuration verfication.
- Development.
The most common use case is server domain configuration verification, while adding a domain to a server or moving a site to a new location. Pointing your hosts file to the new location allows you to verify everything is working before updating the public DNS records.
Editing Your Hosts File
Regardless of your operating system, the hosts file is considered a protected system file and must be edited using either administrator or sudo permissions.
Windows
To edit the hosts file on Windows operating systems we use the app called “Notepad.” The process to run Notepad as an administrator is as follows:
- Go to the Start menu and start typing the Notepad.
- Right-click Notepad and choose “Run as administrator.”
Once Notepad is open, you may use the “File -> Open” menu to select the hosts file. You paste the full path in the “File name” box and click “Open.”C:\Windows\System32\Drivers\etc\hosts
You may also browse to the file location manually if you prefer but you’ll have to select “All Files (.*)” above the Open button. If you are prompted to select a program to open the file, select Notepad.
Linux or Mac OS
In a terminal window you may edit the file using your preferred text editor such as nano
or vim
you’ll just have to open the editor using sudo
.
Examples
You will most likely be prompted for your sudo password.
Adding a Domain Record
Your hosts will likely have some records already for things such as “localhost” or “loopback” and may look something like this.
You’ll want to leave any existing records intact and add any new records to the bottom of the file.
For example, lets say we want to point the “example.com” domain to IP address “43.234.12.1.” We add a new line at the bottom of the file and save.