更新

更新

我的服务器中主机名文件的内容是:

# cat /etc/hostname
sub.mysite.com

但是当我ping的 CentOS 7 服务器上显示:

# ping sub.mysite.com
64 bytes from sub ...

甚至:

# ping ns1.mysite.com
    64 bytes from sub ...

如何告诉我的服务器在 ping 时有以下输出?

64 bytes from sub.mysite.com ...

更新

以我的客户为例:

user@host:~$ ping ns1.mysite.com
PING ns1.mysite.com (x.x.x.x) 56(84) bytes of data.
64 bytes from sub (x.x.x.x): icmp_seq=1 ttl=56 time=7.88 ms
64 bytes from sub (x.x.x.x): icmp_seq=2 ttl=56 time=5.86 ms
64 bytes from sub (x.x.x.x): icmp_seq=3 ttl=56 time=4.99 ms
64 bytes from sub (x.x.x.x): icmp_seq=4 ttl=56 time=4.88 ms

我想要完整的主机名 (sub.mysite.com) 而不是sub.

答案1

ping不用于/etc/hostname解析 IP 到名称映射,它使用名称服务 ( netns) 来执行这些转换。顺便说一下,/etc/hostname它是 systemd 的一部分:

$ rpm -qf /etc/hostname
systemd-219-42.el7_4.10.x86_64

您看到的短名称是通过名称服务sub来自您的文件。/etc/hosts如果您使用,strace您可以看到如何ping找到sub

$ strace -s 2000 ping -c1 www.google.com |& grep /etc/host
open("/etc/host.conf", O_RDONLY|O_CLOEXEC) = 4
open("/etc/hosts", O_RDONLY|O_CLOEXEC)  = 4
open("/etc/hosts", O_RDONLY|O_CLOEXEC)  = 4

ping因此,解决问题的简单方法是将要显示的服务器名称放入/etc/hosts文件中。

例子

$ ping -c1 www.google.com
PING www.google.com (74.125.141.99) 56(84) bytes of data.
64 bytes from vl-in-f99.1e100.net (74.125.141.99): icmp_seq=1 ttl=63 time=109 ms

--- www.google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 109.903/109.903/109.903/0.000 ms
Now if we were to add that IP, 74.125.141.103, to your `/etc/hosts` file we could manipulate `ping` into showing whatever we want for it:

将其添加到/etc/hosts

74.125.141.99  blah.blah.com

现在重复我们的测试:

$ ping -c1 www.google.com
PING www.google.com (74.125.141.99) 56(84) bytes of data.
64 bytes from blah.blah.com (74.125.141.99): icmp_seq=1 ttl=63 time=109 ms

--- www.google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 109.886/109.886/109.886/0.000 ms

/etc/hosts 的顺序

请记住,添加主机的顺序/etc/hosts可能会导致名称按照您所看到的方式显示。

例如,如果我们的/etc/hosts

74.125.141.99  blah blah.blah.com

将会ping像您所看到的那样显示,并带有一个简短的名称:

$ ping -c1 www.google.com
PING www.google.com (74.125.141.99) 56(84) bytes of data.
64 bytes from blah (74.125.141.99): icmp_seq=1 ttl=63 time=108 ms

--- www.google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 108.065/108.065/108.065/0.000 ms

参考

相关内容