Distributions
› Ubuntu
› Fedora
› CentOS
中文资源站
› 网易开源镜像站
jhwangpin
V2EX  ›  Linux

各位大神! CENTOS 的 hosts 文件能不能引用附加其他文本?

  •  
  •   jhwangpin · Aug 12, 2020 · 4263 views
    This topic created in 2280 days ago, the information mentioned may be changed or developed.

    问下各位有没有办法能够实现 linux 下 /etc/hosts 能够引用别的文件,比如我有个主机 IP 映射的文本,需要映射到 /etc/hosts 下,不然每次去维护 /etc/hosts 很麻烦。因为每台 host 不一样的。

    14 replies  •  2020-08-14 09:09:33 +08:00
    Nitroethane
    Nitroethane
    Aug 12, 2020
    1
    你这需求还不如直接搭个 DNS 服务器更方便
    nightwitch
    nightwitch
    Aug 12, 2020
    2
    不可以。可行的方法写个程序,把你的自定义 hosts 追加到现有的 hosts 中,不用了就删掉。你自己追加的这部分可以用一些特定的注释隔开,这样你删掉的话很容易找到哪部分是你自己填加的
    Oceanhime
    Oceanhime
    Aug 12, 2020
    3
    做不到的吧,不如写个同步脚本,定期同步到 /etc/hosts,或者监听文本更改后相应对 /etc/hosts 进行更改。
    可以像 #2 说的用两个注释隔开,一个 start 一个 end,同步脚本只更改注释中间内容,确保安全也为了你如果还要在 /etc/hosts 里加内容。
    Livid
    Livid
    MOD
    PRO
    Aug 12, 2020❤️1
    4
    试试用 ansible 来管理 /etc/hosts
    encro
    encro
    Aug 12, 2020
    5
    简单说是不可以,以下是变通方案:

    1,

    https://superuser.com/questions/128932/is-there-a-way-to-include-a-separate-file-in-etc-hosts

    Keep a hosts.base file and then write a simple script to build a complete, combined hosts file with that hosts.base file and the other files you want to include. You can cron it or execute on demand.


    2,
    dns

    3,
    pssh, ansible 等运维工具
    codehz
    codehz
    Aug 12, 2020
    6
    原生 hosts 大概无解了,不过既然都是 glibc 的“受害者”,自然可以用一个 nss 模块来解决,
    大概参考这个(为模板)写一个自定义的 hosts 提供器即可(这玩意根本没实现 readme 里的功能,基本上就是一个模板)
    https://github.com/apetresc/nss-hostslocal
    redial39
    redial39
    Aug 12, 2020
    7
    比较变通的方法是建立一个逻辑意义上的 host.d 文件夹,你可以在里面维护 host,并且可以分散在各个文件,然后用 inotify 去监控这个文件夹,所有增删改都触发 cat * 合并并重写 /etc/host
    goofool
    goofool
    Aug 12, 2020 via Android
    8
    同一个局域网内可以尝试用 mdns
    julyclyde
    julyclyde
    Aug 12, 2020
    9
    如果你有大量 hosts 需要改
    那你的需求肯定是错的
    Aliencn
    Aliencn
    Aug 12, 2020
    10
    我们用数据库维护列表,触发脚本通过 http 下载更新每台机器上的 hosts
    WeeH9T
    WeeH9T
    Aug 12, 2020
    11
    etcd + confd
    ungrown
    ungrown
    Aug 12, 2020
    12
    你可以用 dnsmasq
    支持额外的 hosts 文件
    如果你不了解 dnsmasq 的话,它麻雀虽小功能很多可玩性多面性都不错
    lucifer9
    lucifer9
    Aug 14, 2020
    14
    ldap 吧
    或者 mount --bind ?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2351 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 05:13 · PVG 13:13 · LAX 21:13 · JFK 00:13
    ♥ Do have faith in what you're doing.