在Linux系統中,會發現一個有趣的現象,通過 netstat 查看監聽的服務端口時,發現有些服務 Proto(protocol協議) 項只顯示了 tcp6(代表監聽服務端口在IPv6協議), 沒有 tcp (代表監聽服務端口在IPv4協議),這時千萬不要認爲此服務就不能正常提供 IPv4 地址的服務
我們先舉個栗子來區分下現象: SSH服務:(sshd_config 配置文件中配置下列三行內容,同時監聽 IPv4 和 IPv6 協議地址)
root@BJ-CentOS7
~
# vim sshd_config
AddressFamily any
ListenAddress
::
ListenAddress
0.0.0.0
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
Listen
:::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 地址服務使用的)