運行netstat,查看激活Internet鏈接。具體參數列表請查看幫助文件。node
dell@dell:~$ man netstat dell@dell:~$ info netstat
netstat命令能夠顯示網絡鏈接,路由表,接口狀態,假裝鏈接,網絡鏈路信息和組播成員組。mysql
dell@dell:~$ netstat -ano | more 激活Internet鏈接 (服務器和已創建鏈接的) Proto Recv-Q Send-Q Local Address Foreign Address State Timer tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 關閉 (0.00/0/0) tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 關閉 (0.00/0/0) tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 關閉 (0.00/0/0) tcp 0 36 172.20.0.253:22 172.20.0.133:6576 ESTABLISHED 打開 (0.35/0/0) tcp6 0 0 :::8080 :::* LISTEN 關閉 (0.00/0/0) tcp6 0 0 :::22 :::* LISTEN 關閉 (0.00/0/0) udp 0 0 0.0.0.0:5353 0.0.0.0:* 關閉 (0.00/0/0) udp 0 0 0.0.0.0:36171 0.0.0.0:* 關閉 (0.00/0/0) udp 0 0 0.0.0.0:58934 0.0.0.0:* 關閉 (0.00/0/0) udp 0 0 127.0.1.1:53 0.0.0.0:* 關閉 (0.00/0/0) udp 0 0 0.0.0.0:68 0.0.0.0:* 關閉 (0.00/0/0) udp 0 0 0.0.0.0:631 0.0.0.0:* 關閉 (0.00/0/0) udp6 0 0 :::5353 :::* 關閉 (0.00/0/0) udp6 0 0 :::46962 :::* 關閉 (0.00/0/0) raw6 0 0 :::58 :::* 7 關閉 (0.00/0/0) 活躍的UNIX域套接字 (服務器和已創建鏈接的) Proto RefCnt Flags Type State I-Node 路徑 unix 2 [ ACC ] 流 LISTENING 37601 @/tmp/dbus-a4YY6cvUmD unix 2 [ ] 數據報 43222 /run/user/108/systemd/notify unix 2 [ ACC ] 流 LISTENING 37563 @/tmp/ibus/dbus-JHsXop1s unix 2 [ ] 數據報 20851 /run/user/1000/systemd/notify unix 2 [ ACC ] 流 LISTENING 43227 /run/user/108/systemd/private unix 2 [ ACC ] 流 LISTENING 20852 /run/user/1000/systemd/private unix 2 [ ACC ] SEQPACKET LISTENING 1809 /run/udev/control --更多--
也能夠運行netstat -nutlp,t和u分別表示TCP與UDP鏈接。sql
[root@localhost ~]# netstat -nutlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4452/mysqld tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 2517/dnsmasq tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1245/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1226/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2485/master tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 9053/zabbix_agentd tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::80 :::* LISTEN 1651/httpd tcp6 0 0 :::22 :::* LISTEN 1245/sshd tcp6 0 0 ::1:631 :::* LISTEN 1226/cupsd tcp6 0 0 ::1:25 :::* LISTEN 2485/master tcp6 0 0 :::443 :::* LISTEN 1651/httpd tcp6 0 0 :::10050 :::* LISTEN 9053/zabbix_agentd udp 0 0 127.0.0.1:323 0.0.0.0:* 842/chronyd udp 0 0 0.0.0.0:40060 0.0.0.0:* 807/avahi-daemon: r udp 0 0 0.0.0.0:5353 0.0.0.0:* 807/avahi-daemon: r udp 0 0 192.168.122.1:53 0.0.0.0:* 2517/dnsmasq udp 0 0 0.0.0.0:67 0.0.0.0:* 2517/dnsmasq udp6 0 0 ::1:323 :::* 842/chronyd [root@localhost ~]#
還有一種方法是運行 lsof -i。CentOS 7須要安裝。apache
[root@promote ~]# yum install lsof -y
安裝完成執行命令以下所示:服務器
[root@localhost ~]# lsof -i COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME systemd 1 root 45u IPv6 15960 0t0 TCP *:sunrpc (LISTEN) systemd 1 root 46u IPv4 15961 0t0 TCP *:sunrpc (LISTEN) avahi-dae 807 avahi 12u IPv4 18053 0t0 UDP *:mdns avahi-dae 807 avahi 13u IPv4 18863 0t0 UDP *:40060 chronyd 842 chrony 1u IPv4 17867 0t0 UDP localhost:323 chronyd 842 chrony 2u IPv6 17868 0t0 UDP localhost:323 chronyd 842 chrony 4u IPv4 1272721 0t0 UDP localhost.localdomain:53081->61-216-153-107.HINET-IP.hinet.net:ntp cupsd 1226 root 11u IPv6 23760 0t0 TCP localhost:ipp (LISTEN) cupsd 1226 root 12u IPv4 23761 0t0 TCP localhost:ipp (LISTEN) sshd 1245 root 3u IPv4 22054 0t0 TCP *:ssh (LISTEN) sshd 1245 root 4u IPv6 22056 0t0 TCP *:ssh (LISTEN) httpd 1651 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 1651 apache 6u IPv6 32680 0t0 TCP *:https (LISTEN) httpd 1653 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 1653 apache 6u IPv6 32680 0t0 TCP *:https (LISTEN) httpd 1654 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 1654 apache 6u IPv6 32680 0t0 TCP *:https (LISTEN) httpd 1655 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 1655 apache 6u IPv6 32680 0t0 TCP *:https (LISTEN) httpd 1656 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 1656 apache 6u IPv6 32680 0t0 TCP *:https (LISTEN) httpd 1657 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 1657 apache 6u IPv6 32680 0t0 TCP *:https (LISTEN) master 2485 root 13u IPv4 24672 0t0 TCP localhost:smtp (LISTEN) master 2485 root 14u IPv6 24673 0t0 TCP localhost:smtp (LISTEN) dnsmasq 2517 nobody 3u IPv4 23996 0t0 UDP *:bootps dnsmasq 2517 nobody 5u IPv4 24003 0t0 UDP localhost.localdomain:domain dnsmasq 2517 nobody 6u IPv4 24004 0t0 TCP localhost.localdomain:domain (LISTEN) sshd 2605 root 3u IPv4 1272258 0t0 TCP localhost.localdomain:ssh->172.20.0.133:6562 (ESTABLISHED) httpd 2726 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 2726 apache 6u IPv6 32680 0t0 TCP *:https (LISTEN) httpd 2728 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 2728 apache 6u IPv6 32680 0t0 TCP *:https (LISTEN) httpd 4200 root 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 4200 root 6u IPv6 32680 0t0 TCP *:https (LISTEN) mysqld 4452 mysql 14u IPv4 34998 0t0 TCP *:mysql (LISTEN) zabbix_ag 9053 zabbix 4u IPv4 56573 0t0 TCP *:zabbix-agent (LISTEN) zabbix_ag 9053 zabbix 5u IPv6 56574 0t0 TCP *:zabbix-agent (LISTEN) zabbix_ag 9054 zabbix 4u IPv4 56573 0t0 TCP *:zabbix-agent (LISTEN) zabbix_ag 9054 zabbix 5u IPv6 56574 0t0 TCP *:zabbix-agent (LISTEN) zabbix_ag 9055 zabbix 4u IPv4 56573 0t0 TCP *:zabbix-agent (LISTEN) zabbix_ag 9055 zabbix 5u IPv6 56574 0t0 TCP *:zabbix-agent (LISTEN) zabbix_ag 9056 zabbix 4u IPv4 56573 0t0 TCP *:zabbix-agent (LISTEN) zabbix_ag 9056 zabbix 5u IPv6 56574 0t0 TCP *:zabbix-agent (LISTEN) zabbix_ag 9057 zabbix 4u IPv4 56573 0t0 TCP *:zabbix-agent (LISTEN) zabbix_ag 9057 zabbix 5u IPv6 56574 0t0 TCP *:zabbix-agent (LISTEN) zabbix_ag 9058 zabbix 4u IPv4 56573 0t0 TCP *:zabbix-agent (LISTEN) zabbix_ag 9058 zabbix 5u IPv6 56574 0t0 TCP *:zabbix-agent (LISTEN) [root@localhost ~]# #查看80端口 [root@localhost ~]# lsof -i tcp:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME httpd 1651 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 1653 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 1654 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 1655 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 1656 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 1657 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 2726 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 2728 apache 4u IPv6 32672 0t0 TCP *:http (LISTEN) httpd 4200 root 4u IPv6 32672 0t0 TCP *:http (LISTEN) [root@localhost ~]#
lsof -i :port命令,port(端口號)是在0-65535範圍內整數。網絡
不過須要指出,外部主機訪問對應端口可能還須要防火牆開啓對應端口,本文再也不詳述。dom
下文補充一些常見其餘netstat用法。ssh
查看用戶進程PID和進程名稱(須要root權限)。tcp
dell@dell:~$ netstat -nlutp (並不是全部進程都能被檢測到,全部非本用戶的進程信息將不會顯示,若是想看到全部信息,則必須切換到 root 用戶) 激活Internet鏈接 (僅服務器) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN - tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp6 0 0 :::8080 :::* LISTEN - tcp6 0 0 :::22 :::* LISTEN - udp 0 0 0.0.0.0:5353 0.0.0.0:* - udp 0 0 0.0.0.0:36171 0.0.0.0:* - udp 0 0 0.0.0.0:58934 0.0.0.0:* - udp 0 0 127.0.1.1:53 0.0.0.0:* - udp 0 0 0.0.0.0:68 0.0.0.0:* - udp 0 0 0.0.0.0:631 0.0.0.0:* - udp6 0 0 :::5353 :::* - udp6 0 0 :::46962 :::* - #查看用戶進程PID、進程名稱和用戶名 #(並不是全部進程都能被檢測到,全部非本用戶的進程信息將不會顯示,若是想看到全部信息,則必須切換到 root 用戶) root@minmin-VirtualBox:~# netstat -nelutp | more 激活Internet鏈接 (僅服務器) Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 0 19407 890/dnsmasq tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 0 20296 856/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 0 26054 1994/cupsd tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 122 19426 878/postgres tcp6 0 0 :::22 :::* LISTEN 0 20301 856/sshd tcp6 0 0 ::1:631 :::* LISTEN 0 26053 1994/cupsd udp 0 0 0.0.0.0:50035 0.0.0.0:* 110 18396 711/avahi-daemon: r udp 0 0 0.0.0.0:37873 0.0.0.0:* 65534 19609 890/dnsmasq udp 0 0 127.0.1.1:53 0.0.0.0:* 0 19406 890/dnsmasq udp 0 0 0.0.0.0:68 0.0.0.0:* 0 19326 873/dhclient udp 0 0 0.0.0.0:631 0.0.0.0:* 0 26069 1995/cups-browsed udp 0 0 0.0.0.0:5353 0.0.0.0:* 110 18394 711/avahi-daemon: r udp6 0 0 :::37979 :::* 110 18397 711/avahi-daemon: r udp6 0 0 :::5353 :::* 110 18395 711/avahi-daemon: r root@minmin-VirtualBox:~#
查看本地路由表。oop
dell@dell:~$ netstat -r 內核 IP 路由表 Destination Gateway Genmask Flags MSS Window irtt Iface default 172.20.0.1 0.0.0.0 UG 0 0 0 enp2s0 172.20.0.0 * 255.255.255.0 U 0 0 0 enp2s0 dell@dell:~$
查看網卡信息。
#顯示本地網卡信息 [root@localhost ~]# netstat -i Kernel Interface table Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg lo 65536 84348 0 0 0 84348 0 0 0 LRU p1p1 1500 1358482 0 0 0 407522 0 0 0 BMRU virbr0 1500 0 0 0 0 0 0 0 0 BMU #顯示本地網卡詳細信息,對比ifconfig [root@localhost ~]# netstat -ie Kernel Interface table lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 84348 bytes 259268763 (247.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 84348 bytes 259268763 (247.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 p1p1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.20.0.155 netmask 255.255.255.0 broadcast 172.20.0.255 inet6 fe80::ffac:ac35:db67:e8a5 prefixlen 64 scopeid 0x20<link> ether c8:1f:66:1c:51:3c txqueuelen 1000 (Ethernet) RX packets 1358498 bytes 928997502 (885.9 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 407531 bytes 59529226 (56.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:2a:09:b3 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost ~]#
netstat -s能夠統計各個協議數據信息,-c能夠固定時間刷新統計信息。
CentOS 7 默認沒有安裝net-tools軟件包,能夠使用命令 ip addr查看網絡信息。