每天一个Linux命令(50)netstat命令

时间:2023-03-08 22:10:53

    netstat命令用来打印Linux中网络系统的状态信息,可让你得知整个Linux系统的网络情况。

    (1)用法:

    用法:  netstat [选项参数]

    (2)功能:

    功能:  netstat用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况。

如果你的计算机有时候接收到的数据报导致出错数据或故障,你不必感到奇怪,TCP/IP可以容许这些类型的错误,并能够自动重发数据报。但如果累计的出错情况数目占到所接收的IP数据报相当大的百分比,或者它的数目正迅速增加,那么你就应该使用netstat查一查为什么会出现这些情况了。

    (3)选项参数:

1) -a或–all      显示所有连线中的Socket。

2) -n或–numeric     直接使用IP地址,而不通过域名服务器。

3) -t或–tcp     显示TCP传输协议的连线状况。

4) -u或–udp       显示UDP传输协议的连线状况。

5) -v或–verbose     显示指令执行过程。

6) -p或–programs  显示正在使用Socket的程序识别码和程序名称。

7) -s或–statistice   显示网络工作信息统计表。

    (4)实例:

1)[root@localhost ~]# netstat        无参数的使用

[root@localhost ~]# netstat
Active Internet connections (w/o servers) //有源TCP连接
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 192.168.120.204:ssh 10.2.0.68: ESTABLISHED
udp 192.168.120.204: 10.58.119.119:domain ESTABLISHED
Active UNIX domain sockets (w/o servers) //有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)
Proto RefCnt Flags Type State I-Node Path
unix [ ] DGRAM @/org/kernel/udev/udevd
unix [ ] DGRAM /dev/log
unix [ ] DGRAM
unix [ ] DGRAM
unix [ ] STREAM CONNECTED
unix [ ] STREAM CONNECTED
[root@localhost ~]#

说明:

1."Recv-Q"和"Send-Q"指的是接收队列和发送队列。

2.Proto显示连接使用的协议;RefCnt表示连接到本套接口上的进程号;Types显示套接口的类型;State显示套接口当前的状态;Path表示连接到套接口的其它进程使用的路径名。

3.套接口类型:

    -t :TCP

    -u :UDP

    -raw :RAW类型

    --unix :UNIX域类型

    --ax25 :AX25类型

    --ipx :ipx类型

    --netrom :netrom类型

4.状态说明:

    LISTEN:     侦听来自远方的TCP端口的连接请求

    SYN-SENT:    再发送连接请求后等待匹配的连接请求(如果有大量这样的状态包,检查是否中招了)

    SYN-RECEIVED    再收到和发送一个连接请求后等待对方对连接请求的确认(如有大量此状态,估计被flood攻击了)

    ESTABLISHED:   代表一个打开的连接

    FIN-WAIT-1:      等待远程TCP连接中断请求,或先前的连接中断请求的确认

    FIN-WAIT-2:      从远程TCP等待连接中断请求

    CLOSE-WAIT:    等待从本地用户发来的连接中断请求

    CLOSING:          等待远程TCP对连接中断的确认

    LAST-ACK:     等待原来的发向远程TCP的连接中断请求的确认(不是什么好东西,此项出现,检查是否被攻击)

    TIME-WAIT:       等待足够的时间以确保远程TCP接收到连接中断请求的确认

    CLOSED:            没有任何连接状态

2)[sunjimeng@localhost ~]$ netstat -a      显示所有已监听或者没有监听的端口

[sunjimeng@localhost ~]$ netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0.0.0.0:ssh 0.0.0.0:* LISTEN
tcp localhost:ipp 0.0.0.0:* LISTEN
tcp localhost:smtp 0.0.0.0:* LISTEN
tcp 192.168.142.128: 115.28.122.210:http TIME_WAIT
tcp 192.168.142.128: 112.124.140.210:http TIME_WAIT
tcp 192.168.142.128: 115.28.122.210:http TIME_WAIT
tcp 192.168.142.128: 115.28.122.210:http TIME_WAIT
tcp 192.168.142.128: 112.124.140.210:http TIME_WAIT
tcp 192.168.142.128: 115.28.122.210:http TIME_WAIT
tcp 192.168.142.128: 112.124.140.210:http TIME_WAIT
tcp 192.168.142.128: 202.204.80.77:http TIME_WAIT
tcp 192.168.142.128: 115.28.122.210:http TIME_WAIT
tcp 192.168.142.128: 112.124.140.210:http TIME_WAIT
tcp 192.168.142.128: ec2----.:https ESTABLISHED
tcp 192.168.142.128: 112.124.140.210:http TIME_WAIT
tcp 192.168.142.128: 202.204.80.77:http TIME_WAIT
tcp 192.168.142.128: 115.28.122.210:http TIME_WAIT
tcp 192.168.142.128: 202.204.80.77:http TIME_WAIT
tcp6 [::]:ssh [::]:* LISTEN
tcp6 localhost:ipp [::]:* LISTEN
tcp6 localhost:smtp [::]:* LISTEN
udp 0.0.0.0:bootpc 0.0.0.0:*
udp 0.0.0.0:ntp 0.0.0.0:*
udp localhost: 0.0.0.0:*
udp 192.168.142.128: 192.168.142.2:domain ESTABLISHED
udp 0.0.0.0: 0.0.0.0:*
udp 0.0.0.0:mdns 0.0.0.0:*
udp 0.0.0.0: 0.0.0.0:*
udp 0.0.0.0: 0.0.0.0:*
udp6 [::]:ntp [::]:*
udp6 localhost: [::]:*
udp6 [::]: [::]:*
raw6 [::]:ipv6-icmp [::]:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix [ ACC ] STREAM LISTENING @/tmp/.ICE-unix/
unix [ ACC ] STREAM LISTENING @/tmp/dbus-XARCfYJ4
unix [ ACC ] STREAM LISTENING /var/run/abrt/abrt.socket
......
unix [ ] STREAM CONNECTED

3)[sunjimeng@localhost ~]$ netstat -nu      显示已建立的UDP连接

[sunjimeng@localhost ~]$ netstat -nu
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 192.168.142.128: 192.168.142.2: ESTABLISHED
[sunjimeng@localhost ~]$ netstat -anu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0.0.0.0: 0.0.0.0:*
udp 0.0.0.0: 0.0.0.0:*
udp 127.0.0.1: 0.0.0.0:*
udp 192.168.142.128: 192.168.142.2: ESTABLISHED
udp 0.0.0.0: 0.0.0.0:*
udp 0.0.0.0: 0.0.0.0:*
udp 0.0.0.0: 0.0.0.0:*
udp6 ::: :::*
udp6 ::: :::*
udp6 ::: :::*

4)[sunjimeng@localhost ~]$ netstat -nt       显示所有已建立的TCP连接

[sunjimeng@localhost ~]$ netstat -nat
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0.0.0.0: 0.0.0.0:* LISTEN
tcp 127.0.0.1: 0.0.0.0:* LISTEN
tcp 127.0.0.1: 0.0.0.0:* LISTEN
tcp 192.168.142.128: 52.27.123.81: ESTABLISHED
tcp 192.168.142.128: 115.28.122.210: ESTABLISHED
tcp6 ::: :::* LISTEN
tcp6 ::: :::* LISTEN
tcp6 ::: :::* LISTEN
[sunjimeng@localhost ~]$ netstat -nt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 192.168.142.128: 52.27.123.81: ESTABLISHED
tcp 192.168.142.128: 115.28.122.210: ESTABLISHED

5)[sunjimeng@localhost ~]$ netstat -nupa     显示UDP端口号的使用情况

[sunjimeng@localhost ~]$ netstat -nupa                    //没有root权限
(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp 0.0.0.0: 0.0.0.0:* -
udp 0.0.0.0: 0.0.0.0:* -
udp 127.0.0.1: 0.0.0.0:* -
udp 192.168.142.128: 192.168.142.2: ESTABLISHED /netstat
udp 0.0.0.0: 0.0.0.0:* -
udp 0.0.0.0: 0.0.0.0:* -
udp 0.0.0.0: 0.0.0.0:* -
udp6 ::: :::* -
udp6 ::: :::* -
udp6 ::: :::* -
[sunjimeng@localhost ~]$ su root
密码:
[root@localhost sunjimeng]# netstat -aunp          //有root权限
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp 0.0.0.0: 0.0.0.0:* /dhclient
udp 0.0.0.0: 0.0.0.0:* /chronyd
udp 127.0.0.1: 0.0.0.0:* /chronyd
udp 192.168.142.128: 192.168.142.2: ESTABLISHED /netstat
udp 0.0.0.0: 0.0.0.0:* /dhclient
udp 0.0.0.0: 0.0.0.0:* /avahi-daemon: r
udp 0.0.0.0: 0.0.0.0:* /avahi-daemon: r
udp 192.168.142.128: 192.168.142.2: ESTABLISHED /python
udp6 ::: :::* /chronyd
udp6 ::: :::* /chronyd
udp6 ::: :::* /dhclient

6)[root@localhost sunjimeng]# netstat -i      显示网卡列表

[root@localhost sunjimeng]# netstat -i
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eno16777 BMRU
lo LRU

7)[root@localhost sunjimeng]# netstat -s      显示网络统计信息

[root@localhost sunjimeng]# netstat -s
Ip:
total packets received
forwarded
incoming packets discarded
incoming packets delivered
requests sent out
outgoing packets dropped
dropped because of missing route
Icmp:
ICMP messages received
input ICMP message failed.
ICMP input histogram:
destination unreachable:
timeout in transit:
echo requests:
echo replies:
ICMP messages sent
ICMP messages failed
ICMP output histogram:
destination unreachable:
echo request:
echo replies:
IcmpMsg:
InType0:
InType3:
InType8:
InType11:
OutType0:
OutType3:
OutType8:
Tcp:
active connections openings
passive connection openings
failed connection attempts
connection resets received
connections established
segments received
segments send out
segments retransmited
bad segments received.
resets sent
Udp:
packets received
packets to unknown port received.
packet receive errors
packets sent
receive buffer errors
send buffer errors
UdpLite:
TcpExt:
TCP sockets finished time wait in fast timer
delayed acks sent
delayed acks further delayed because of locked socket
Quick ack mode was activated times
packet headers predicted
acknowledgments not containing data payload received
predicted acknowledgments
congestion windows recovered without slow start after partial ack
other TCP timeouts
connections reset due to unexpected data
connections reset due to early user close
IPReversePathFilter:
TCPRcvCoalesce:
TCPChallengeACK:
TCPSYNChallenge:
IpExt:
InNoRoutes:
InMcastPkts:
OutMcastPkts:
InBcastPkts:
OutBcastPkts:
InOctets:
OutOctets:
InMcastOctets:
OutMcastOctets:
InBcastOctets:
OutBcastOctets:

8)[root@localhost sunjimeng]# netstat -r        显示路由表的信息

[root@localhost sunjimeng]# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default 192.168.142.2 0.0.0.0 UG eno16777736
192.168.142.0 0.0.0.0 255.255.255.0 U eno16777736
192.168.142.0 0.0.0.0 255.255.255.0 U eno16777736
[root@localhost sunjimeng]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.142.2 0.0.0.0 UG eno16777736
192.168.142.0 0.0.0.0 255.255.255.0 U eno16777736
192.168.142.0 0.0.0.0 255.255.255.0 U eno16777736

    (5)其他:

Linux网络套接字:

套接字连接的过程如同(客户)打一个电话到一个大公司,接线员(服务器进程)接听电话并把它转接到你要找的部门,然后再从那里转到你要找的人(服务器套接字),然后接线员(服务器进程)再继续转接其它(客户)的电话。

      套接字有本地套接字和网络套接字两种。本地套接字的名字是Linux文件系统中的文件名,一般放在/tmp或/usr/tmp目录中;网络套接字的名字是与客户连接的特定网络有关的服务标识符(端口号或访问点)。这个标识符允许Linux将进入的针对特定端口号的连接转到正确的服务器进程。
      套接字的连接建立过程:http://blog.chinaunix.net/uid-25829053-id-3015832.html;
      Java通信中的套接字连接过程:http://www.cnblogs.com/MenAngel/p/5317082.html;

相关文章