hostctl 一款跨平台的 hosts 管理工具
介绍⌗
hostctl 是一款基于 Go 开发的用于管理 /etc/hosts 的跨平台 CLI 工具。
- 支持按 profile 管理域名映射
- 支持启用或禁用域名映射
- 使用 .etchosts 文件为项目定制域名映射
- 支持读取 Docker 或 Docker Compose 的容器,并映射到 /etc/hosts
安装⌗
brew install guumaster/tap/hostctl
使用⌗
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.
获取当前 /etc/hosts 列表:
hostctl list
+---------+--------+-----------------+---------------+
| PROFILE | STATUS | IP | DOMAIN |
+---------+--------+-----------------+---------------+
| default | on | 127.0.0.1 | localhost |
| default | on | 255.255.255.255 | broadcasthost |
| default | on | ::1 | localhost |
+---------+--------+-----------------+---------------+
增加新的域名解析记录:
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 |
+----------+--------+-----------+------------------+
还可以通过命令行设置启用或禁用某个 profile 下的解析记录:
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⌗
该工具不仅能简单的操作 /etc/hosts
还能获取 Docker 或 Docker Compose 中的容器,并同步到 /etc/hosts
文件中:
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 |
+----------+--------+-----------------+------------------+
- 同步时默认使用
.loc
作为 TLD,你可以通过--domain
来指定自己的 TLD; - 在同步 Docker Compose 的容器时,hostctl 只找当前目录下的
docker-compose.yml
文件,如果不是这个文件名则需要使用--compose-file
指定。
总结⌗
上手体验后我就开始反思,为什么在看到这个工具前,我没有想到,还能够通过这种方式管理 /etc/hosts,相较于用 vim 编辑 /etc/hosts 真的太省事了!
目前该工具还在持续开发中,未来还会支持 Kubernetes 的 Namespace 下的资源同步,持续关注吧!
I hope this is helpful, Happy hacking…