Hosts File Management

At work I use a lot of host overrides. Working in a .NET environment on a MacBook Pro using virtual machines makes them essential. However once you have a lot of host overrides pointing at the same address they can be frustrating to update if the virtual machine address changes.

Virtual machines are great. They allow you to run software which might not be available for your operating system otherwise. However I don’t believe you should virtualise just because you can. If I can run something in the host OS I will. At work I use IIS to host .NET applications in the virtual machine which I access through Chrome on OS X. This approach means that I need a host override for each IIS hosted development website I want to access.

Management

Previously I have just added new host overrides when needed by running nano /etc/hosts and making the necessary addition. This only poses a problem if the address of my virtual machine changes. Unfortunately this happens whenever I’m not working in the office. When I connect to the office network my virtual machine is assigned a static IP. However when I’m working from home or a client site my virtual machine is assigned an IP by the network which renders my host overrides non-functional.

Normally I would change the few overrides which I’m working with on that particular day and then reverse the change when I’m back in the office. However this can be a little tiresome and I decided there should be an easier way to deal with this quickly on the command line.

Hostsman

I’ve created a very basic command-line tool using node. It has basic commands for adding and removing host overrides which all point to the same address. It also has a command that lets you update the address of the development machine and updates any existing host overrides with the new address.

You can find the source in anierzad/hostsman on GitHub. The package is available on npm.

As I mentioned previously the tool is very basic and I’ll probably find ways to improve it over time.

Installation

You can install using npm:

npm install -g hostsman

Usage

You can get usage information and help with:

hostsman --help