在Linux和FreeBSD查看打开的端口

时间:2019-11-20 08:53:34  来源:igfitidea点击:

如何查看Linux和FreeBSD服务器上打开了哪些端口?

netstat命令查看被打开的端口

语法为:

# netstat --listen

或者

# netstat -l

Debian 8.x输出示例

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 localhost:953           *:*                     LISTEN        
tcp        0      0 *:nfs                   *:*                     LISTEN     
tcp        0      0 *:afpovertcp            *:*                     LISTEN     
tcp        0      0 localhost:mysql         *:*                     LISTEN          
tcp        0      0 *:http                  *:*                     LISTEN     
tcp6       0      0 [::]:41864              [::]:*                  LISTEN     
tcp6       0      0 [::]:37833              [::]:*                  LISTEN     
tcp6       0      0 [::]:35658              [::]:*                  LISTEN     
tcp6       0      0 [::]:sunrpc             [::]:*                  LISTEN     
tcp6       0      0 [::]:http               [::]:*                  LISTEN     
tcp6       0      0 [::]:domain             [::]:*                  LISTEN     
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN     
udp        0      0 *:59997                 *:*                                                         
udp6       0      0 [::]:58606              [::]:*                             
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     16912    /var/run/mysqld/mysqld.sock
unix  2      [ ACC ]     STREAM     LISTENING     11796    /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     11799    /var/run/avahi-daemon/socket
unix  2      [ ACC ]     STREAM     LISTENING     18844    @/tmp/dbus-FStDtp4tGA

查看已打开的端口和已建立的TCP连接:

$ netstat -vatn

查看被打开的UDP端口:

$ netstat -vaun

FreeBSD/OS X Unix的用户尝试使用以下命令:

$ netstat -na | grep -i LISTEN
$ netstat -f inet -na | grep -i LISTEN

输出示例:

tcp6       0      0  ::1.52698              *.*                    LISTEN     
tcp4       0      0  127.0.0.1.49153        *.*                    LISTEN     
tcp4       0      0  127.0.0.1.49152        *.*                    LISTEN     
tcp4       0      0  *.22                   *.*                    LISTEN     
tcp6       0      0  *.22                   *.*                    LISTEN

lsof命令示例

查看打开的端口列表:

# lsof -i

查看所有被打开的文件:

# lsof

查看进程1234打开的端口号:

# lsof -i 4 -a -p 1234

查看正在TCP监听的进程和命令:

# lsof -iTCP -sTCP:LISTEN

输出示例:

COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rpcbind  680    rpc    8u  IPv4  17888      0t0  TCP *:sunrpc (LISTEN)
rpcbind  680    rpc   11u  IPv6  17891      0t0  TCP *:sunrpc (LISTEN)
cupsd   1119   root   11u  IPv6  26090      0t0  TCP localhost:ipp (LISTEN)
cupsd   1119   root   12u  IPv4  26091      0t0  TCP localhost:ipp (LISTEN)
sshd    1122   root    3u  IPv4  24844      0t0  TCP *:ssh (LISTEN)
sshd    1122   root    4u  IPv6  24846      0t0  TCP *:ssh (LISTEN)
master  1414   root   13u  IPv4  25506      0t0  TCP localhost:smtp (LISTEN)
master  1414   root   14u  IPv6  25507      0t0  TCP localhost:smtp (LISTEN)
dnsmasq 1728 nobody    6u  IPv4  26584      0t0  TCP theitroad:domain (LISTEN)

在FreeBSD查看被打开的端口

FreeBSD中,可以使用sockstat命令:

$ sockstat
$ sockstat -l
$ sockstat -4 -l
$ sockstat -6 -l