前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hostctl · 像PRO一样管理你的hosts文件

hostctl · 像PRO一样管理你的hosts文件

作者头像
公众号: 云原生生态圈
发布2020-12-29 11:23:37
8870
发布2020-12-29 11:23:37
举报
文章被收录于专栏:云原生生态圈云原生生态圈

使用背景

在日常的工作中,我们经常会通过编辑hosts文件来处理一些自定义域名的解析任务,每次手动的编辑linux或者mac上/etc/hosts和window上的hosts是一项繁琐的任务。但是现在,我们可以通过hostctl[1]来自动化管理本地域名解析文件,这样一来就可以使某些方面自动化,以使其变得更加干净快捷。

在不同的操作系统上hosts文件的路径是不一样:

  • Linux和Mac上的路径
代码语言:javascript
复制
/etc/hosts
  • window上的路径
代码语言:javascript
复制
C:/Windows/System32/Drivers/etc/hosts

下面我们就开启通过hostctl开启hosts文件内容的自动化管理之路

安装hostctl

代码语言:javascript
复制
https://github.com/guumaster/hostctl/releases/download/v1.0.14/hostctl_1.0.14_macOS_64-bit.tar.gz
tar xf hostctl_1.0.14_macOS_64-bit.tar.gz
mv hostctl /usr/local/bin/

在终端中执行hostctl的时候,Mac上会提示不受信任,因此需要在安全中心允许执行hostctl

Mac安全中心允许使用不受信任的开发工具

hostctl管理你的hosts

hostctl支持对hosts文件基础修改需求,如备份,从文件恢复,增加域名解析、删除域名解析记录、替换以及从Dockerdocker-compose运行的容器中同步解析记录到你的hosts文件,下面就通过几个简单的例子说明hostctl如何管理你的主机解析记录文件

代码语言:javascript
复制
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.

list查看hosts内容

代码语言:javascript
复制
☸️  dev? default  ? ? hostctl list
+---------+--------+-----------------+----------------------------+
| PROFILE | STATUS |       IP        |           DOMAIN           |
+---------+--------+-----------------+----------------------------+
| default | on     | 127.0.0.1       | localhost                  |
| default | on     | 255.255.255.255 | broadcasthost              |
| default | on     | ::1             | localhost                  |
| default | on     | 127.0.0.1       | kubernetes.docker.internal |
| default | on     | 127.0.0.1       | kubernetes.docker.internal |
| default | on     | 192.168.10.110  | apiserver.cluster.local    |
+---------+--------+-----------------+----------------------------+

# 同时支持多种格式输出,如markdown,json,table等

Add增加解析记录

代码语言:javascript
复制
☸️  dev? default  ? ? sudo hostctl add domains test admin.com admin.cn.com --ip 1.2.2.3
Password:
[✔] Domains 'admin.com, admin.cn.com' added.

+---------+--------+---------+--------------+
| PROFILE | STATUS |   IP    |    DOMAIN    |
+---------+--------+---------+--------------+
| test    | on     | 1.2.2.3 | admin.com    |
| test    | on     | 1.2.2.3 | admin.cn.com |
+---------+--------+---------+--------------+

☸️  dev? default  ? ? hostctl list
+---------+--------+-----------------+----------------------------+
| PROFILE | STATUS |       IP        |           DOMAIN           |
+---------+--------+-----------------+----------------------------+
| default | on     | 127.0.0.1       | localhost                  |
| default | on     | 255.255.255.255 | broadcasthost              |
| default | on     | ::1             | localhost                  |
| default | on     | 127.0.0.1       | kubernetes.docker.internal |
| default | on     | 127.0.0.1       | kubernetes.docker.internal |
| default | on     | 192.168.10.110  | apiserver.cluster.local    |
+---------+--------+-----------------+----------------------------+
| test    | on     | 1.2.2.3         | admin.com                  |
| test    | on     | 1.2.2.3         | admin.cn.com               |
+---------+--------+-----------------+----------------------------+
# 在/etc/hosts中已存在的profile就是default,上面增加的profile的名字为test

启用和关闭本地域名解析

代码语言:javascript
复制
☸️  dev? default  ? ? sudo hostctl enable test
+---------+--------+---------+--------------+
| PROFILE | STATUS |   IP    |    DOMAIN    |
+---------+--------+---------+--------------+
| test    | on     | 1.2.2.3 | admin.com    |
| test    | on     | 1.2.2.3 | admin.cn.com |
+---------+--------+---------+--------------+
☸️  dev? default  ? ? ping admin.com
PING admin.com (1.2.2.3): 56 data bytes
Request timeout for icmp_seq 0
^C
--- admin.com ping statistics ---
2 packets transmitted, 0 packets received, 100.0% packet loss
☸️  dev? default  ? ? sudo hostctl disable test
+---------+--------+---------+--------------+
| PROFILE | STATUS |   IP    |    DOMAIN    |
+---------+--------+---------+--------------+
| test    | off    | 1.2.2.3 | admin.com    |
| test    | off    | 1.2.2.3 | admin.cn.com |
+---------+--------+---------+--------------+

☸️  dev? default  ? ? ping admin.com
PING admin.com (13.225.150.119): 56 data bytes
64 bytes from 13.225.150.119: icmp_seq=0 ttl=240 time=237.629 ms
^C
--- admin.com ping statistics ---
2 packets transmitted, 1 packets received, 50.0% packet loss
round-trip min/avg/max/stddev = 237.629/237.629/237.629/0.000 ms

移除解析记录

代码语言:javascript
复制
# 移除指定域名
☸️  dev? default  ? ? sudo hostctl remove domains test admin.com 
[✔] Domains 'admin.com' removed.

+---------+--------+---------+--------------+
| PROFILE | STATUS |   IP    |    DOMAIN    |
+---------+--------+---------+--------------+
| test    | on     | 1.2.2.3 | admin.cn.com |
+---------+--------+---------+--------------+
☸️  dev? default  ? ? hostctl list
+---------+--------+-----------------+----------------------------+
| PROFILE | STATUS |       IP        |           DOMAIN           |
+---------+--------+-----------------+----------------------------+
| default | on     | 127.0.0.1       | localhost                  |
| default | on     | 255.255.255.255 | broadcasthost              |
| default | on     | ::1             | localhost                  |
| default | on     | 127.0.0.1       | kubernetes.docker.internal |
| default | on     | 127.0.0.1       | kubernetes.docker.internal |
| default | on     | 192.168.10.110  | apiserver.cluster.local    |
+---------+--------+-----------------+----------------------------+
| test    | on     | 1.2.2.3         | admin.cn.com               |
+---------+--------+-----------------+----------------------------+
# 移除指定profile的所有域名
☸️  dev? default  ? ? sudo hostctl remove test
[✔] Profile(s) 'test' removed.


☸️  dev? default  ? ? cat /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 kubernetes.docker.internal
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
192.168.10.110 apiserver.cluster.local

##################################################################
# Content under this line is handled by hostctl. DO NOT EDIT.
##################################################################

其他命令

代码语言:javascript
复制
# 备份
hostctl backup -path .
☸️  dev? default  ? ? ls -al
total 8
drwxr-xr-x    3 marionxue  staff    96 Dec 11 22:27 .
drwxr-xr-x+ 131 marionxue  staff  4192 Dec 11 23:27 ..
-rw-r--r--    1 marionxue  staff   609 Dec 11 22:27 hosts.20201211

# 通过文件替换已有的profile
☸️  dev? default  ? ? sudo hostctl replace test -f ./hosts
+---------+--------+---------+---------------+
| PROFILE | STATUS |   IP    |    DOMAIN     |
+---------+--------+---------+---------------+
| test    | on     | 1.1.1.1 | www.baidu.com |
+---------+--------+---------+---------------+

sync

hostsctl支持从docker以及docker-compose运行的容器中同步解析,但是kubernetes上暂时还未支持。

参考资料

[1]

hostctl: https://guumaster.github.io/

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-12-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 云原生生态圈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用背景
  • 安装hostctl
  • hostctl管理你的hosts
    • list查看hosts内容
      • Add增加解析记录
        • 启用和关闭本地域名解析
          • 移除解析记录
            • 其他命令
              • sync
                • 参考资料
                相关产品与服务
                容器服务
                腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档