How to verify a service is listening on both ipv4 and ipv6?

18,731

Solution 1

You can use netstat:

# netstat -tlnp | grep httpd
# netstat -tlnp | grep apache
# netstat -tlnp | grep nginx
# netstat -tlnp | grep ...

Then check that you get a line for each IP protocol version (tcp and 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

Note that you'll need root privileges with netstat if you want to see processes which belong to root (such as the Apache master process). If you want to use netstat as a standard user, remove the -p option and grep by IP and port instead. For instance:

$ netstat -tln | grep '0.0.0.0:80'
$ netstat -tln | grep '127.0.0.1:80'
$ netstat -tln | grep '::1:80'

Note that in this case, you don't need to grep again on tcp or tcp6, since the IP you're grep-ing already provides the information you are looking for. Whether you should look for 127.0.0.1 or 0.0.0.0 depends on your web server configuration.

Solution 2

lsof is also useful.

You can see all listening addresses and established connections of nginx using IPv4 and IPv6 respectively as follows (run as root):

lsof -c nginx -a -i4
lsof -c nginx -a -i6

Show all connections of Google Chrome on the client host, both v4 and v6:

lsof -c chrome -a -i
Share:
18,731

Related videos on Youtube

Gaurav KS
Author by

Gaurav KS

Updated on September 18, 2022

Comments

  • Gaurav KS
    Gaurav KS over 1 year

    I want to check whether https service is listening on both IPv6 and IPv4.

    And also when I am accessing url via browser, I want to know request is served by IPv4 or IPv6.

  • Gaurav KS
    Gaurav KS over 8 years
    I only get below line: tcp 0 0 :::443 :::* LISTEN 13734/httpd
  • yorkshiredev
    yorkshiredev over 8 years
    @Archemar You are probably using IPv6 transport over IPv4, a compatibility workaround put in place for the IPv4 > IPv6 transition phase. Also note that Apache supports IPv4-mapped IPv6 addresses. (Gaurav KS: I edited my answer)
  • Gaurav KS
    Gaurav KS over 8 years
    I am a root user. and i cant see tcp6 in netstat output.
  • yaegashi
    yaegashi over 8 years
    Then you can conclude that your servers are listening only on IPv4 address.