如何验证服务是否同时侦听 ipv4 和 ipv6?

Gau*_* KS 7 linux networking ssl openssl https

我想检查 https 服务是否同时监听 IPv6 和 IPv4。

而且当我通过浏览器访问 url 时,我想知道请求是由 IPv4 还是 IPv6 提供的。

Joh*_*ith 6

您可以使用netstat

# netstat -tlnp | grep httpd
# netstat -tlnp | grep apache
# netstat -tlnp | grep nginx
# netstat -tlnp | grep ...
Run Code Online (Sandbox Code Playgroud)

然后检查每个 IP 协议版本(tcp 和 tcp6)是否都有一行。

#!/bin/bash

ns=$(netstat -tlnp | grep httpd)

if grep 'tcp ' <<< "$ns" >/dev/null; then
    echo "httpd has a listening IPv4 socket."
fi

if grep 'tcp6 ' <<< "$ns" >/dev/null; then
    echo "httpd has a listening IPv6 socket."
fi
Run Code Online (Sandbox Code Playgroud)

请注意,netstat如果您想查看属于 root 的进程(例如 Apache 主进程),则需要具有 root 权限。如果您想netstat作为标准用户使用,请删除该-p选项并改为使用grepIP 和端口。例如:

$ netstat -tln | grep '0.0.0.0:80'
$ netstat -tln | grep '127.0.0.1:80'
$ netstat -tln | grep '::1:80'
Run Code Online (Sandbox Code Playgroud)

请注意,在这种情况下,您不需要grep再次打开tcptcp6,因为您正在使用的 IPgrep已经提供了您要查找的信息。您应该查找 127.0.0.1 还是 0.0.0.0 取决于您的 Web 服务器配置。