Everything about etc hosts file
I don't work in Windows or Mac often so I always forget where the hosts file is located. The windows etc hosts file and Unix etc host file both work the same, so usage is easy.
I don’t work in Windows or Mac often so I always forget where the hosts file is located. The windows etc hosts file and Unix etc host file both work the same, so usage is easy.
Here is a list to the locations of the hosts file on all popular operating systems.
What is etc hosts?
The etc host file lets you make Domain Name System(DNS) modifications locally. Your computer will always check and use this file before contacting your configured DNS.
How to use this file
The left column is for the IP address you want to contact. The second column is the host or domain name. Any further optional columns are for aliases of the host. You can comment any line of this file with the hash tag (#).
Why etc hosts is important to developers
I don’t use a development server for building websites. Using Git and Docker I do all my development locally. If you work on more than one project at a time, you likely use docker or Apache Virtual Hosts. To accommodate multiple projects you can change your /etc/hosts file and point it to your local machine. Here is an example of a hosts file:
127.0.0.1 dev.nickyeoman.com 127.0.0.1 narcissistdanger.fb dev.narcissistdanger.com 127.0.0.1 frostybot.ny frostybot.fb
I either add a sub-domain to my top level domain name or change the Top Level Domain (TLD) to avoid confusion with the live server. Ensure you have configured Apache to accept calls to your development domain. The etc/hosts file also allows you to test a new server to ensure it’s working as expected before you switch the DNS. This will help avoid any downtime when launching a new website.
I do all my development work on Ubuntu desktop so when it comes time to test in IE, I change the hosts file in windows to point to my Ubuntu computer. This way you can work on IE bugs on your local network without changing your development servers. Something like:
Matching Shared Hosting environment
Hosts is also useful to emulate your shared hosting environment. For example if you host uses a separate domain for their database server [internal.db.hostingco.com] you can emulate the host on your development machine, without affecting the live server.
Outside of development if you have resources such as Gitlab or Owncloud to access on an internal network (but is also available to the cloud) you can change your etc/host file to access the resource on your internal network without accessing the internet. This will save internet bandwidth. Example:
Modifying etc hosts is usually more convenient than changing DNS. If you need a more robust solution, pound (or any reverse proxy solution) would be what you should investigate next.
I tend to prefer blocking ads using browser solutions, but if you have a shared hosts file between computers it might be handy to do something like this:
Gotcha, Can’t use ports
The hosts file is for hostnames only, it can’t forward to ports, you need to use a reverse proxy for this.