How to check if ssh/http daemon listens on TCP port in FreeBSD using sockstat & lsof & netstat util
How to check if ssh/http daemon listens on TCP port in FreeBSD using sockstat & lsof & netstat utilitiesAs compared to Linux, FreeBSD "netstat" doesn't show the process ID, but instead, the "sockstat" utility does.
Checking SSH and Apache TCP ports in Linux and FreeBSD using lsof utility:Code:
# lsof -Pni :22 -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 17543 www 3u IPv6 0xc35d3cb0 0t0 TCP *:80 (LISTEN)
sshd 61609 root 3u IPv6 0xc35d3570 0t0 TCP *:22 (LISTEN)
sshd 61609 root 4u IPv4 0xc35d3ae0 0t0 TCP *:22 (LISTEN)
httpd 75304 root 3u IPv6 0xc35d3cb0 0t0 TCP *:80 (LISTEN)
httpd 78080 www 3u IPv6 0xc35d3cb0 0t0 TCP *:80 (LISTEN)
Checking SSH and Apache TCP ports in FreeBSD using netstat:Code:
# netstat -anf inet | grep -E "22|80"
Active Internet connections (including servers)
Proto Recv-Q Send-Q Local Address Foreign Address (state)
tcp46 0 0 *.80 *.* LISTEN
tcp4 0 0 *.22 *.* LISTEN
Checking SSH and Apache TCP ports in FreeBSD using sockstat:Code:
# sockstat -4l | grep -E ":22|:80"
www httpd 56459 3 tcp4 6 *:80 *:*
www httpd 78080 3 tcp4 6 *:80 *:*
www httpd 18278 3 tcp4 6 *:80 *:*
www httpd 18046 3 tcp4 6 *:80 *:*
www httpd 18040 3 tcp4 6 *:80 *:*
www httpd 17815 3 tcp4 6 *:80 *:*
www httpd 17543 3 tcp4 6 *:80 *:*
root httpd 75304 3 tcp4 6 *:80 *:*
root inetd 72331 7 tcp4 *:222 *:*
root sshd 61609 4 tcp4 *:22 *:*
Also, "sockstat" can show connected sockets if used with "-4c" instead.
Man sockstat:
Quote:
NAME
sockstat -- list open sockets
Synopsis
sockstat [-46clu] [-p ports] [-P protocols]
Description
The sockstat command lists open Internet or UNIX domain sockets.
The following options are available:
-4 Show AF_INET (IPv4) sockets.
-c Show connected sockets.
-l Show listening sockets.
"lsof" utility is not a base package in FreeBSD and it needs to be installed from ports (make install clean -C /usr/ports/sysutils/lsof) or from the online repository (pkg_add -rv lsof).