Introduction

hostctl is a cross-platform CLI tool developed in Go for managing /etc/hosts.

  • Supports managing domain mappings by profile
  • Supports enabling or disabling domain mappings
  • Uses .etchosts files for project-specific domain mappings
  • Supports reading Docker or Docker Compose containers and mapping them to /etc/hosts

Installation

brew install guumaster/tap/hostctl

Usage

hostctl

hostctl is a CLI tool to manage your hosts file with ease.
You can have multiple profiles, enable/disable exactly what
you need each time with a simple interface.

Usage:
  hostctl [command]

Available Commands:
  add         Add content to a profile in your hosts file.
  backup      Creates a backup copy of your hosts file
  disable     Disable a profile from your hosts file.
  enable      Enable a profile on your hosts file.
  help        Help about any command
  list        Shows a detailed list of profiles on your hosts file.
  remove      Remove a profile from your hosts file.
  replace     Replace content to a profile in your hosts file.
  restore     Restore hosts file content from a backup file.
  status      Shows a list of profile names and statuses on your hosts file.
  sync        Sync some system IPs with a profile.
  toggle      Change status of a profile on your hosts file.

Flags:
  -c, --column strings     Column names to show on lists. comma separated
  -h, --help               help for hostctl
      --host-file string   Hosts file path (default "/etc/hosts")
      --no-color           force colorless output
  -o, --out string         Output type (table|raw|markdown|json) (default "table")
  -q, --quiet              Run command without output
      --raw                Output without borders (same as -o raw)
  -v, --version            version for hostctl

Use "hostctl [command] --help" for more information about a command.

Get the current /etc/hosts list:

hostctl list
+---------+--------+-----------------+---------------+
| PROFILE | STATUS |       IP        |    DOMAIN     |
+---------+--------+-----------------+---------------+
| default | on     | 127.0.0.1       | localhost     |
| default | on     | 255.255.255.255 | broadcasthost |
| default | on     | ::1             | localhost     |
+---------+--------+-----------------+---------------+

Add new domain resolution records:

sudo hostctl add domains betterde www.betterde.com api.betterde.com
[✔] Domains 'www.betterde.com, api.betterde.com' added.

+----------+--------+-----------+------------------+
| PROFILE  | STATUS |    IP     |      DOMAIN      |
+----------+--------+-----------+------------------+
| betterde | on     | 127.0.0.1 | www.betterde.com |
| betterde | on     | 127.0.0.1 | api.betterde.com |
+----------+--------+-----------+------------------+

You can also enable or disable resolution records under a specific profile via command line:

sudo hostctl disable betterde
+----------+--------+-----------+------------------+
| PROFILE  | STATUS |    IP     |      DOMAIN      |
+----------+--------+-----------+------------------+
| betterde | off    | 127.0.0.1 | www.betterde.com |
| betterde | off    | 127.0.0.1 | api.betterde.com |
+----------+--------+-----------+------------------+

Docker

This tool not only can simply operate on /etc/hosts but also can get containers from Docker or Docker Compose and synchronize them to the /etc/hosts file:

docker ps
CONTAINER ID   IMAGE            COMMAND                  CREATED      STATUS          PORTS                                      NAMES
ad87448499f6   traefik:latest   "/entrypoint.sh --ap…"   4 days ago   Up 46 minutes   0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   traefik

sudo hostctl sync docker traefik
+---------+--------+-------------+-------------+
| PROFILE | STATUS |     IP      |   DOMAIN    |
+---------+--------+-------------+-------------+
| traefik | on     | 10.8.10.254 | traefik.loc |
+---------+--------+-------------+-------------+

sudo hostctl sync docker-compose zitadel --compose-file docker-compose.yaml
hostctl list
+----------+--------+-----------------+------------------+
| PROFILE  | STATUS |       IP        |      DOMAIN      |
+----------+--------+-----------------+------------------+
| default  | on     | 127.0.0.1       | localhost        |
| default  | on     | 255.255.255.255 | broadcasthost    |
| default  | on     | ::1             | localhost        |
+----------+--------+-----------------+------------------+
| betterde | off    | 127.0.0.1       | www.betterde.com |
| betterde | off    | 127.0.0.1       | api.betterde.com |
+----------+--------+-----------------+------------------+
| traefik  | on     | 10.8.10.254     | traefik.loc      |
+----------+--------+-----------------+------------------+
| zitadel  | on     | 10.8.10.3       | zitadel.loc      |
| zitadel  | on     | 10.8.10.2       | cockroachdb.loc  |
+----------+--------+-----------------+------------------+
  • By default, .loc is used as the TLD during synchronization; you can specify your own TLD using --domain;
  • When synchronizing Docker Compose containers, hostctl only looks for the docker-compose.yml file in the current directory. If the filename is different, you need to specify it using --compose-file.

Conclusion

After trying it out, I started to reflect on why I hadn’t thought of managing /etc/hosts in this way before discovering this tool. It’s so much more convenient than editing /etc/hosts with vim!

Currently, the tool is still under active development. In the future, it will also support synchronizing resources under Kubernetes Namespaces. Let’s keep an eye on it!

I hope this is helpful, Happy hacking…