V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
enlightment
V2EX  ›  Linux

求解 Linux 对本地主机名的解析方式

  •  
  •   enlightment · 2020-11-02 15:17:11 +08:00 · 2320 次点击
    这是一个创建于 1474 天前的主题,其中的信息可能已经有所发展或是发生改变。
    centos7 本机的 hostname 为 xxxa
    吃时 ping xxxa 解析到了 eth1 对应的 ip,这个解析走的是哪里的 dns ?为什么会解析到 eth1 的 ip 而不是 eth2 eth3 的?
    12 条回复    2020-11-03 14:22:53 +08:00
    D0n9
        1
    D0n9  
       2020-11-02 15:21:18 +08:00
    先走 /etc/hosts
    enlightment
        2
    enlightment  
    OP
       2020-11-02 15:24:11 +08:00
    /etc/hosts 并里没有单独对 xxxa 做任何解析
    3dwelcome
        3
    3dwelcome  
       2020-11-02 15:33:07 +08:00 via Android   ❤️ 1
    主机名解析底层就是调用 gethostbyname,你说为什么不对应第二个 ip,理论上应该对应 0.0.0.0,绑定所有的本机 ip,但 linux 程序员就是写了获取回来的第一个 ip,你也没办法。
    enlightment
        4
    enlightment  
    OP
       2020-11-02 15:36:15 +08:00
    @3dwelcome 这个是内核里写的逻辑吗?
    3dwelcome
        5
    3dwelcome  
       2020-11-02 15:45:44 +08:00 via Android
    严格来说不算内核,就算是个提供给程序的调用库。
    julyclyde
        6
    julyclyde  
       2020-11-02 18:07:42 +08:00
    strace 一下 ping 看看?
    lenqu
        7
    lenqu  
       2020-11-02 18:12:19 +08:00
    traceroute 一下,看看那走的节点
    huangmingyou
        8
    huangmingyou  
       2020-11-02 18:18:38 +08:00
    man 5 nsswitch.conf
    huangmingyou
        9
    huangmingyou  
       2020-11-02 18:19:24 +08:00
    # /etc/nsswitch.conf
    #
    # Example configuration of GNU Name Service Switch functionality.
    # If you have the `glibc-doc-reference' and `info' packages installed, try:
    # `info libc "Name Service Switch"' for information about this file.

    passwd: compat
    group: compat
    shadow: compat
    gshadow: files

    hosts: files mdns4_minimal [NOTFOUND=return] dns
    networks: files

    protocols: db files
    services: db files
    ethers: db files
    rpc: db files

    netgroup: nis
    targetFree
        10
    targetFree  
       2020-11-02 18:25:12 +08:00
    @3dwelcome 好像 gethostbyname 已被 getaddrinfo 替代
    baobao1270
        11
    baobao1270  
       2020-11-03 12:10:15 +08:00 via Android
    systemd resolved?
    enlightment
        12
    enlightment  
    OP
       2020-11-03 14:22:53 +08:00
    @julyclyde
    [root@cent7 ~]# strace -f -e open ping cent7.550gtmp
    open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
    open("/lib64/libcap.so.2", O_RDONLY|O_CLOEXEC) = 3
    open("/lib64/libidn.so.11", O_RDONLY|O_CLOEXEC) = 3
    open("/lib64/libcrypto.so.10", O_RDONLY|O_CLOEXEC) = 3
    open("/lib64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = 3
    open("/lib64/libm.so.6", O_RDONLY|O_CLOEXEC) = 3
    open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
    open("/lib64/libattr.so.1", O_RDONLY|O_CLOEXEC) = 3
    open("/lib64/libdl.so.2", O_RDONLY|O_CLOEXEC) = 3
    open("/lib64/libz.so.1", O_RDONLY|O_CLOEXEC) = 3
    open("/etc/pki/tls/legacy-settings", O_RDONLY) = -1 ENOENT (No such file or directory)
    open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
    open("/etc/resolv.conf", O_RDONLY|O_CLOEXEC) = 4
    open("/etc/nsswitch.conf", O_RDONLY|O_CLOEXEC) = 4
    open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 4
    open("/lib64/libnss_files.so.2", O_RDONLY|O_CLOEXEC) = 4
    open("/etc/host.conf", O_RDONLY|O_CLOEXEC) = 4
    open("/etc/hosts", O_RDONLY|O_CLOEXEC) = 4
    open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 4
    open("/lib64/libnss_dns.so.2", O_RDONLY|O_CLOEXEC) = 4
    open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 4
    open("/lib64/libnss_myhostname.so.2", O_RDONLY|O_CLOEXEC) = 4
    open("/lib64/librt.so.1", O_RDONLY|O_CLOEXEC) = 4
    open("/lib64/libdw.so.1", O_RDONLY|O_CLOEXEC) = 4
    open("/lib64/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = 4
    open("/lib64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 4
    open("/usr/lib64/elfutils/tls/x86_64/libelf.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
    open("/usr/lib64/elfutils/tls/libelf.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
    open("/usr/lib64/elfutils/x86_64/libelf.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
    open("/usr/lib64/elfutils/libelf.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
    open("/lib64/libelf.so.1", O_RDONLY|O_CLOEXEC) = 4
    open("/usr/lib64/elfutils/liblzma.so.5", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
    open("/lib64/liblzma.so.5", O_RDONLY|O_CLOEXEC) = 4
    open("/usr/lib64/elfutils/libbz2.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
    open("/lib64/libbz2.so.1", O_RDONLY|O_CLOEXEC) = 4
    open("/etc/hosts", O_RDONLY|O_CLOEXEC) = 4
    open("/etc/gai.conf", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
    PING cent7.550gtmp (10.0.182.120) 56(84) bytes of data.
    open("/etc/hosts", O_RDONLY|O_CLOEXEC) = 4
    64 bytes from cent7.550gtmp (10.0.112.1): icmp_seq=1 ttl=64 time=0.050 ms
    64 bytes from cent7.550gtmp (10.0.112.1): icmp_seq=2 ttl=64 time=0.104 ms
    64 bytes from cent7.550gtmp (10.0.112.1): icmp_seq=3 ttl=64 time=0.049 ms
    64 bytes from cent7.550gtmp (10.0.112.1): icmp_seq=4 ttl=64 time=0.047 ms
    ^C
    strace: Process 56574 detached
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5328 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 07:56 · PVG 15:56 · LAX 23:56 · JFK 02:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.