黑白格子

  • 首页
  • 所有文章
  • 文章分类
  • 关于页面
  • 友链

  • 搜索
随笔 脚本 Shell MAC IE SSL iptables 网络 k8s docker ubuntu web 镜像 Nginx Linux Windows cloud-init

netstat查看服务端口监听在ipv6但是通过ipv4地址可正常访问

发表于 2020-07-03 | 分类于 Linux | 0 | 阅读次数 1026

在Linux系统中,会发现一个有趣的现象,通过 netstat 查看监听的服务端口时,发现有些服务 Proto(protocol协议) 项只显示了 tcp6(代表监听服务端口在IPv6协议), 没有 tcp (代表监听服务端口在IPv4协议),这时千万不要认为此服务就不能正常提供 IPv4 地址的服务

我们先举个栗子来区分下现象:

SSH服务:(sshd_config 配置文件中配置下列三行内容,同时监听 IPv4 和 IPv6 协议地址)

root@BJ-CentOS7 ~ # vim sshd_config
AddressFamily any
ListenAddress ::		#监听IPv6
ListenAddress 0.0.0.0		#监听IPv4

root@BJ-CentOS7 ~ # netstat -anpt | grep ssh       #重启服务发现22端口同时监听在Ipv4和Ipv6协议
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      25105/sshd          
tcp6       0      0 :::22                   :::*                    LISTEN      25105/sshd     

Apache服务:(httpd 配置文件中配置以下两行内容,同时监听 IPv4 和 IPv6 协议地址)

root@BJ-CentOS7 ~ # vim httpd.conf
Listen 80		#监听IPv4 80端口
Listen :::80		#监听IPv6 80端口

root@BJ-CentOS7 ~ # netstat -anpt | grep httpd     #重启服务发现80端口仅监听在Ipv6协议
tcp6       0      0 :::80                   :::*                    LISTEN      24829/httpd

分别来测试 IPv4 地址加服务端口,都是可以正常连通的:

root@BJ-CentOS7 ~ # tcping 192.168.1.10 22
192.168.1.10 port 22 open.
root@BJ-CentOS7 ~ # tcping 192.168.1.10 80
192.168.1.10 port 80 open.

为什么httpd服务看起来只是监听了IPv6地址却可以提供 Ipv4 地址服务呢?
从Apache官网文档及Apache源码中可以得到答案:
Apache配置监听地址说明:http://httpd.apache.org/docs/2.4/bind.html

/* If we have the unspecified IPv4 address (0.0.0.0) and
 * the unspecified IPv6 address (::) is next, we need to
 * swap the order of these in the list. We always try to
 * bind to IPv6 first, then IPv4, since an IPv6 socket
 * might be able to receive IPv4 packets if V6ONLY is not
 * enabled, but never the other way around.
 * Note: In some configurations, the unspecified IPv6 address
 * could be even later in the list.  This logic only corrects
 * the situation where it is next in the list, such as when
 * apr_sockaddr_info_get() returns an IPv4 and an IPv6 address,
 * in that order.
 */

上述描述大概,服务监听了 tcp6 后,tcp 也可以用的。

虽然协议显示了 IPv6 的端口监听,但并不代表只接受 IPv6 的连接,实际上,apache会以 mapped address (::FFFF:a.b.c.d) 映射地址方式来接受 IPv4 的连接。除了少部分平台上,例如FreeBSD,NetBSD,OpenBSD之外, Apache 在编译时,默认启用了 --enable-v4-mapped 选项。所以,Apache会同时接受 IPv6 和 IPv4 的连接请求。
可以通过 httpd -V 命令来查看默认编译参数:

root@BJ-CentOS7 ~ # httpd -V
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
Server version: Apache/2.4.6 (CentOS)
Server built:   Apr 20 2018 18:10:38
Server's Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)          #重点看这里,启用IPv4地址映射
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/sbin/suexec"
 -D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"

如果我们在编译 httpd 的时候使用 --disable-v4-mapped 参数禁止 ipv4 mapped,那么默认情况下, httpd 会分别监听在 ipv4 和 ipv6,而非只监听 ipv6,如下所示:

netstat -anpt | grep httpd
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      27173/httpd
tcp6       0      0 :::80                   :::*                    LISTEN      27173/httpd

总结:

Linux 下 Apache 服务默认开启了 Ipv4 地址映射,会以 (::FFFF:a.b.c.d) 映射地址方式来接受 IPv4 的连接并处理,所以通过 netstat 命令看不到端口监听 Ipv4 地址不代表不能正常提供 Ipv4 地址服务。(发现MySQL也是同样,在配置文件 my.cnf 中 [mysqld] 配置中加入 bind-address=0.0.0.0 即可看到正常监听在了 Ipv4 协议地址上,但是前者也是不影响 Ipv4 地址服务使用的)

  • 本文作者: 黑白格子
  • 本文链接: https://www.clhuang.cn/?p=65
  • 版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0 许可协议。转载请注明出处!
# 随笔 # 脚本 # Shell # MAC # IE # SSL # iptables # 网络 # k8s # docker # ubuntu # web # 镜像 # Nginx # Linux # Windows # cloud-init
通过SSH 连接 linux服务器提示 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
linux下tomcat开通443端口
黑白格子

黑白格子

54 日志
11 分类
17 标签
Creative Commons
0%
© 2021 黑白格子
主题 - NexT.Mist
网站已稳定运行:
陕ICP备16020191号