【每天一个 Linux 命令】网络相关命令(ifconfig、route、ping、traceroute

输出:

[root@localhost ~]# ifconfig eth0 192.168.120.56 
[root@localhost ~]# ifconfig eth0 192.168.120.56 netmask 255.255.255.0 
[root@localhost ~]# ifconfig eth0 192.168.120.56 netmask 255.255.255.0 broadcast 192.168.120.255

**说明:

ifconfig eth0 192.168.120.56

给eth0网卡配置IP地:192.168.120.56

ifconfig eth0 192.168.120.56 netmask 255.255.255.0

给eth0网卡配置IP地址:192.168.120.56 ,并加上子掩码:255.255.255.0

ifconfig eth0 192.168.120.56 netmask 255.255.255.0 broadcast 192.168.120.255

/给eth0网卡配置IP地址:192.168.120.56,加上子掩码:255.255.255.0,加上个广播地址: 192.168.120.255

实例6:*启用和关闭ARP协议

命令:

ifconfig eth0 arp

ifconfig eth0 -arp

输出:

[root@localhost ~]# ifconfig eth0 arp 
[root@localhost ~]# ifconfig eth0 -arp

说明:

ifconfig eth0 arp 开启网卡eth0 的arp协议;

ifconfig eth0 -arp 关闭网卡eth0 的arp协议;

实例7:设置最大传输单元

命令:

ifconfig eth0 mtu 1500

输出:

[root@localhost ~]# ifconfig eth0 mtu 1480
[root@localhost ~]# ifconfig
eth0   Link encap:Ethernet HWaddr 00:50:56:BF:26:1F 
     inet addr:192.168.120.203 Bcast:192.168.120.255 Mask:255.255.255.0
     UP BROADCAST RUNNING MULTICAST MTU:1480 Metric:1
     RX packets:8712395 errors:0 dropped:0 overruns:0 frame:0
     TX packets:36631 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000 
     RX bytes:597062089 (569.4 MiB) TX bytes:2643973 (2.5 MiB)

lo    Link encap:Local Loopback 
     inet addr:127.0.0.1 Mask:255.0.0.0
     UP LOOPBACK RUNNING MTU:16436 Metric:1
     RX packets:9973 errors:0 dropped:0 overruns:0 frame:0
     TX packets:9973 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0 
     RX bytes:518096 (505.9 KiB) TX bytes:518096 (505.9 KiB)

[root@localhost ~]# ifconfig eth0 mtu 1500
[root@localhost ~]# ifconfig
eth0   Link encap:Ethernet HWaddr 00:50:56:BF:26:1F 
     inet addr:192.168.120.203 Bcast:192.168.120.255 Mask:255.255.255.0
     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
     RX packets:8712548 errors:0 dropped:0 overruns:0 frame:0
     TX packets:36685 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000 
     RX bytes:597072333 (569.4 MiB) TX bytes:2650581 (2.5 MiB)

lo    Link encap:Local Loopback 
     inet addr:127.0.0.1 Mask:255.0.0.0
     UP LOOPBACK RUNNING MTU:16436 Metric:1
     RX packets:9973 errors:0 dropped:0 overruns:0 frame:0
     TX packets:9973 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0 
     RX bytes:518096 (505.9 KiB) TX bytes:518096 (505.9 KiB)

[root@localhost ~]# 

说明:

设置能通过的最大数据包大小为 1500 bytes

备注:用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在。要想将上述的配置信息永远的存的电脑里,那就要修改网卡的配置文件了。

route命令语法

Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table)。要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。在Linux系统中,设置路由通常是为了解决以下问题:该Linux系统在一个局域网中,局域网中有一个网关,能够让机器访问Internet,那么就需要将这台机器的IP地址设置为Linux机器的默认路由。要注意的是,直接在命令行下执行route命令来添加路由,不会永久保存,当网卡重启或者机器重启之后,该路由就失效了;可以在/etc/rc.local中添加route命令来保证该路由设置永久有效。

**1.**命令格式:

route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]

**2.**命令功能:

Route命令是用于操作基于内核ip路由表,它的主要作用是创建一个静态路由让指定一个主机或者一个网络通过一个网络接口,如eth0。当使用"add"或者"del"参数时,路由表被修改,如果没有参数,则显示路由表当前的内容。

**3.**命令参数:

-c 显示更多信息

-n 不解析名字

-v 显示详细的处理信息

-F 显示发送信息

-C 显示路由缓存

-f 清除所有网关入口的路由表。 

-p 与 add 命令一起使用时使路由具有永久性。


add:添加一条新路由。

del:删除一条路由。

-net:目标地址是一个网络。

-host:目标地址是一个主机。

netmask:当添加一个网络路由时,需要使用网络掩码。

gw:路由数据包通过网关。注意,你指定的网关必须能够达到。

metric:设置路由跳数。

Command 指定您想运行的命令 (Add/Change/Delete/Print)。 

Destination 指定该路由的网络目标。 

mask Netmask 指定与网络目标相关的网络掩码(也被称作子网掩码)。 

Gateway 指定网络目标定义的地址集和子网掩码可以到达的前进或下一跃点 IP 地址。 

metric Metric 为路由指定一个整数成本值标(从 1 至 9999),当在路由表(与转发的数据包目标地址最匹配)的多个路由中进行选择时可以使用。 

if Interface 为可以访问目标的接口指定接口索引。若要获得一个接口列表和它们相应的接口索引,使用 route print 命令的显示功能。可以使用十进制或十六进制值进行接口索引。

route命令使用示例

**实例1:**显示当前路由

命令:

route

route -n

输出:

[root@localhost ~]# route
Kernel IP routing table
Destination   Gateway     Genmask     Flags Metric Ref  Use Iface
192.168.120.0  *        255.255.255.0  U   0   0    0 eth0
e192.168.0.0   192.168.120.1  255.255.0.0   UG  0   0    0 eth0
10.0.0.0    192.168.120.1  255.0.0.0    UG  0   0    0 eth0
default     192.168.120.240 0.0.0.0     UG  0   0    0 eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination   Gateway     Genmask     Flags Metric Ref  Use Iface
192.168.120.0  0.0.0.0     255.255.255.0  U   0   0    0 eth0
192.168.0.0   192.168.120.1  255.255.0.0   UG  0   0    0 eth0
10.0.0.0    192.168.120.1  255.0.0.0    UG  0   0    0 eth0
0.0.0.0     192.168.120.240 0.0.0.0     UG  0   0    0 eth0

说明:

第一行表示主机所在网络的地址为192.168.120.0,若数据传送目标是在本局域网内通信,则可直接通过eth0转发数据包;

第四行表示数据传送目的是访问Internet,则由接口eth0,将数据包发送到网关192.168.120.240

其中Flags为路由标志,标记当前网络节点的状态。

Flags标志说明:

U Up表示此路由当前为启动状态

H Host,表示此网关为一主机

G Gateway,表示此网关为一路由器

R Reinstate Route,使用动态路由重新初始化的路由

D Dynamically,此路由是动态性地写入

M Modified,此路由是由路由守护程序或导向器动态修改

! 表示此路由当前为关闭状态

备注:

route -n (-n 表示不解析名字,列出速度会比route 快)

**实例2:**添加网关/设置网关

命令:

route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

输出:

[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
[root@localhost ~]# route
Kernel IP routing table
Destination   Gateway     Genmask     Flags Metric Ref  Use Iface
192.168.120.0  *        255.255.255.0  U   0   0    0 eth0
192.168.0.0   192.168.120.1  255.255.0.0   UG  0   0    0 eth0
10.0.0.0    192.168.120.1  255.0.0.0    UG  0   0    0 eth0
224.0.0.0    *        240.0.0.0    U   0   0    0 eth0
default     192.168.120.240 0.0.0.0     UG  0   0    0 eth0

[root@localhost ~]# 

说明:

增加一条 到达244.0.0.0的路由

实例3:屏蔽一条路由

命令:

route add -net 224.0.0.0 netmask 240.0.0.0 reject

输出:

[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 reject
[root@localhost ~]# route
Kernel IP routing table
Destination   Gateway     Genmask     Flags Metric Ref  Use Iface
192.168.120.0  *        255.255.255.0  U   0   0    0 eth0
192.168.0.0   192.168.120.1  255.255.0.0   UG  0   0    0 eth0
10.0.0.0    192.168.120.1  255.0.0.0    UG  0   0    0 eth0
224.0.0.0    -        240.0.0.0    !   0   -    0 -
224.0.0.0    *        240.0.0.0    U   0   0    0 eth0
default     192.168.120.240 0.0.0.0     UG  0   0    0 eth0

说明:

增加一条屏蔽的路由,目的地址为 224.x.x.x 将被拒绝

实例4:删除路由记录

命令:

route del -net 224.0.0.0 netmask 240.0.0.0

route del -net 224.0.0.0 netmask 240.0.0.0 reject

输出:

[root@localhost ~]# route
Kernel IP routing table
Destination   Gateway     Genmask     Flags Metric Ref  Use Iface
192.168.120.0  *        255.255.255.0  U   0   0    0 eth0
192.168.0.0   192.168.120.1  255.255.0.0   UG  0   0    0 eth0
10.0.0.0    192.168.120.1  255.0.0.0    UG  0   0    0 eth0
224.0.0.0    -        240.0.0.0    !   0   -    0 -
224.0.0.0    *        240.0.0.0    U   0   0    0 eth0
default     192.168.120.240 0.0.0.0     UG  0   0    0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0
[root@localhost ~]# route
Kernel IP routing table
Destination   Gateway     Genmask     Flags Metric Ref  Use Iface
192.168.120.0  *        255.255.255.0  U   0   0    0 eth0
192.168.0.0   192.168.120.1  255.255.0.0   UG  0   0    0 eth0
10.0.0.0    192.168.120.1  255.0.0.0    UG  0   0    0 eth0
224.0.0.0    -        240.0.0.0    !   0   -    0 -
default     192.168.120.240 0.0.0.0     UG  0   0    0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0 reject
[root@localhost ~]# route
Kernel IP routing table
Destination   Gateway     Genmask     Flags Metric Ref  Use Iface
192.168.120.0  *        255.255.255.0  U   0   0    0 eth0
192.168.0.0   192.168.120.1  255.255.0.0   UG  0   0    0 eth0
10.0.0.0    192.168.120.1  255.0.0.0    UG  0   0    0 eth0
default     192.168.120.240 0.0.0.0     UG  0   0    0 eth0
[root@localhost ~]# 

说明:

实例5:删除和添加设置默认网关

命令:

route del default gw 192.168.120.240

route add default gw 192.168.120.240

输出:

[root@localhost ~]# route del default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination   Gateway     Genmask     Flags Metric Ref  Use Iface
192.168.120.0  *        255.255.255.0  U   0   0    0 eth0
192.168.0.0   192.168.120.1  255.255.0.0   UG  0   0    0 eth0
10.0.0.0    192.168.120.1  255.0.0.0    UG  0   0    0 eth0
[root@localhost ~]# route add default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination   Gateway     Genmask     Flags Metric Ref  Use Iface
192.168.120.0  *        255.255.255.0  U   0   0    0 eth0
192.168.0.0   192.168.120.1  255.255.0.0   UG  0   0    0 eth0
10.0.0.0    192.168.120.1  255.0.0.0    UG  0   0    0 eth0
default     192.168.120.240 0.0.0.0     UG  0   0    0 eth0
[root@localhost ~]# 

ping命令语法

Linux系统的ping命令是常用的网络命令,它通常用来测试与目标主机的连通性,我们经常会说“ping一下某机器,看是不是开着”、不能打开网页时会说“你先ping网关地址192.168.1.1试试”。它通过发送ICMP ECHO_REQUEST数据包到网络主机(send ICMP ECHO_REQUEST to network hosts),并显示响应情况,这样我们就可以根据它输出的信息来确定目标主机是否可访问(但这不是绝对的)。有些服务器为了防止通过ping探测到,通过防火墙设置了禁止ping或者在内核参数中禁止ping,这样就不能通过ping确定该主机是否还处于开启状态。

linux下的ping和windows下的ping稍有区别,linux下ping不会自动终止,需要按ctrl+c终止或者用参数-c指定要求完成的回应次数。

**1.**命令格式:

ping [参数] [主机名或IP地址]

**2.**命令功能:

ping命令用于:确定网络和各外部主机的状态;跟踪和隔离硬件和软件问题;测试、评估和管理网络。如果主机正在运行并连在网上,它就对回送信号进行响应。每个回送信号请求包含一个网际协议(IP)和 ICMP 头,后面紧跟一个 tim 结构,以及来填写这个信息包的足够的字节。缺省情况是连续发送回送信号请求直到接收到中断信号(Ctrl-C)。

ping 命令每秒发送一个数据报并且为每个接收到的响应打印一行输出。ping 命令计算信号往返时间和(信息)包丢失情况的统计信息,并且在完成之后显示一个简要总结。ping 命令在程序超时或当接收到 SIGINT 信号时结束。Host 参数或者是一个有效的主机名或者是因特网地址。

**3.**命令参数:

-d 使用Socket的SO_DEBUG功能。

-f 极限检测。大量且快速地送网络封包给一台机器,看它的回应。

-n 只输出数值。

-q 不显示任何传送封包的信息,只显示最后的结果。

-r 忽略普通的Routing Table,直接将数据包送到远端主机上。通常是查看本机的网络接口是否有问题。

-R 记录路由过程。

-v 详细显示指令的执行过程。

<p>-c 数目:在发送指定数目的包后停止。

-i 秒数:设定间隔几秒送一个网络封包给一台机器,预设值是一秒送一次。

-I 网络界面:使用指定的网络界面送出数据包。

-l 前置载入:设置在送出要求信息之前,先行发出的数据包。

-p 范本样式:设置填满数据包的范本样式。

-s 字节数:指定发送的数据字节数,预设值是56,加上8字节的ICMP头,一共是64ICMP数据字节。

-t 存活数值:设置存活数值TTL的大小。

ping命令使用示例

实例1:ping的通的情况

命令:

ping 192.168.120.205

输出:

[root@localhost ~]# ping 192.168.120.205
PING 192.168.120.205 (192.168.120.205) 56(84) bytes of data.
64 bytes from 192.168.120.205: icmp_seq=1 ttl=64 time=0.720 ms
64 bytes from 192.168.120.205: icmp_seq=2 ttl=64 time=0.181 ms
64 bytes from 192.168.120.205: icmp_seq=3 ttl=64 time=0.191 ms
64 bytes from 192.168.120.205: icmp_seq=4 ttl=64 time=0.188 ms
64 bytes from 192.168.120.205: icmp_seq=5 ttl=64 time=0.189 ms

--- 192.168.120.205 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4000ms
rtt min/avg/max/mdev = 0.181/0.293/0.720/0.214 ms
[root@localhost ~]# 

说明:

实例2:ping不通的情况

命令:

ping 192.168.120.202

输出:

[root@localhost ~]# ping 192.168.120.202
PING 192.168.120.202 (192.168.120.202) 56(84) bytes of data.
From 192.168.120.204 icmp_seq=1 Destination Host Unreachable
From 192.168.120.204 icmp_seq=2 Destination Host Unreachable
From 192.168.120.204 icmp_seq=3 Destination Host Unreachable
From 192.168.120.204 icmp_seq=4 Destination Host Unreachable
From 192.168.120.204 icmp_seq=5 Destination Host Unreachable
From 192.168.120.204 icmp_seq=6 Destination Host Unreachable

--- 192.168.120.202 ping statistics ---
8 packets transmitted, 0 received, +6 errors, 100% packet loss, time 7005ms
, pipe 4
[root@localhost ~]#

说明:

**实例3:**ping网关

命令:

ping -b 192.168.120.1

输出:

[root@localhost ~]# route
Kernel IP routing table
Destination   Gateway     Genmask     Flags Metric Ref  Use Iface
192.168.120.0  *        255.255.255.0  U   0   0    0 eth0
192.168.0.0   192.168.120.1  255.255.0.0   UG  0   0    0 eth0
10.0.0.0    192.168.120.1  255.0.0.0    UG  0   0    0 eth0
default     192.168.120.240 0.0.0.0     UG  0   0    0 eth0
[root@localhost ~]# ping -b 192.168.120.1
PING 192.168.120.1 (192.168.120.1) 56(84) bytes of data.
64 bytes from 192.168.120.1: icmp_seq=1 ttl=255 time=2.02 ms
64 bytes from 192.168.120.1: icmp_seq=2 ttl=255 time=1.83 ms
64 bytes from 192.168.120.1: icmp_seq=3 ttl=255 time=1.68 ms
64 bytes from 192.168.120.1: icmp_seq=4 ttl=255 time=1.98 ms
64 bytes from 192.168.120.1: icmp_seq=5 ttl=255 time=1.88 ms

--- 192.168.120.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4000ms
rtt min/avg/max/mdev = 1.682/1.880/2.020/0.129 ms

说明:

**实例4:**ping指定次数

命令:

ping -c 10 192.168.120.206

输出:

[root@localhost ~]# ping -c 10 192.168.120.206
PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.
64 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=1.25 ms
64 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.260 ms
64 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.242 ms
64 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.271 ms
64 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.274 ms
64 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.295 ms
64 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.269 ms
64 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.270 ms
64 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.253 ms
64 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.289 ms

--- 192.168.120.206 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9000ms
rtt min/avg/max/mdev = 0.242/0.367/1.251/0.295 ms
[root@localhost ~]# 

说明:

实例5:时间间隔和次数限制的ping

命令:

ping -c 10 -i 0.5 192.168.120.206

输出:

[root@localhost ~]# ping -c 10 -i 0.5 192.168.120.206
PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.
64 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=1.24 ms
64 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.235 ms
64 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.244 ms
64 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.300 ms
64 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.255 ms
64 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.264 ms
64 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.263 ms
64 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.331 ms
64 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.247 ms
64 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.244 ms

--- 192.168.120.206 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 4499ms
rtt min/avg/max/mdev = 0.235/0.362/1.241/0.294 ms
[root@localhost ~]# ping -c 10 -i 0.01 192.168.120.206
PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.
64 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=0.244 ms
64 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.195 ms
64 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.219 ms
64 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.204 ms
64 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=3.56 ms
64 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=1.93 ms
64 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.193 ms
64 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.193 ms
64 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.202 ms
64 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.211 ms

--- 192.168.120.206 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 90ms
rtt min/avg/max/mdev = 0.193/0.716/3.564/1.080 ms
[root@localhost ~]# 

说明:

实例6:通过域名ping公网上的站点

命令:

ping -c 5 www.58.com

输出:

peida-VirtualBox ~ # ping -c 5 www.58.com
PING www.58.com (211.151.111.30) 56(84) bytes of data.
64 bytes from 211.151.111.30: icmp_req=1 ttl=49 time=14.7 ms
64 bytes from 211.151.111.30: icmp_req=2 ttl=49 time=16.4 ms
64 bytes from 211.151.111.30: icmp_req=3 ttl=49 time=15.2 ms
64 bytes from 211.151.111.30: icmp_req=4 ttl=49 time=14.6 ms
64 bytes from 211.151.111.30: icmp_req=5 ttl=49 time=19.9 ms

--- www.58.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 20101ms
rtt min/avg/max/mdev = 14.618/16.192/19.917/1.965 ms
peida-VirtualBox ~ # 

说明:

**实例7:**多参数使用

命令:

ping -i 3 -s 1024 -t 255 192.168.120.206

输出:

[root@localhost ~]# ping -i 3 -s 1024 -t 255 192.168.120.206
PING 192.168.120.206 (192.168.120.206) 1024(1052) bytes of data.
1032 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=1.99 ms
1032 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.694 ms
1032 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.300 ms
1032 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.481 ms
1032 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.415 ms
1032 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.600 ms
1032 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.411 ms
1032 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.281 ms
1032 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.318 ms
1032 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.362 ms
1032 bytes from 192.168.120.206: icmp_seq=11 ttl=64 time=0.408 ms
1032 bytes from 192.168.120.206: icmp_seq=12 ttl=64 time=0.445 ms
1032 bytes from 192.168.120.206: icmp_seq=13 ttl=64 time=0.397 ms
1032 bytes from 192.168.120.206: icmp_seq=14 ttl=64 time=0.406 ms
1032 bytes from 192.168.120.206: icmp_seq=15 ttl=64 time=0.458 ms

--- 192.168.120.206 ping statistics ---
15 packets transmitted, 15 received, 0% packet loss, time 41999ms
rtt min/avg/max/mdev = 0.281/0.531/1.993/0.404 ms
[root@localhost ~]# 

说明:

-i 3 发送周期为 3秒 -s 设置发送包的大小为1024 -t 设置TTL值为 255

traceroute命令语法

通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径。当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一样,但基本上来说大部分时候所走的路由是相同的。linux系统中,我们称之为traceroute,在MS Windows中为tracert。 traceroute通过发送小的数据包到目的设备直到其返回,来测量其需要多长时间。一条路径上的每个设备traceroute要测3次。输出结果中包括每次测试的时间(ms)和设备的名称(如有的话)及其IP地址。

在大多数情况下,我们会在linux主机系统下,直接执行命令行:

traceroute hostname

而在Windows系统下是执行tracert的命令:

tracert hostname

**1.**命令格式:

traceroute[参数][主机]

**2.**命令功能:

traceroute指令让你追踪网络数据包的路由途径,预设数据包大小是40Bytes,用户可另行设置。

具体参数格式:traceroute [-dFlnrvx][-f<存活数值>][-g<网关>…][-i<网络界面>][-m<存活数值>][-p<通信端口>][-s<来源地址>][-t<服务类型>][-w<超时秒数>][主机名称或IP地址][数据包大小]

**3.**命令参数:

-d 使用Socket层级的排错功能。

-f 设置第一个检测数据包的存活数值TTL的大小。

-F 设置勿离断位。

-g 设置来源路由网关,最多可设置8个。

-i 使用指定的网络界面送出数据包。

-I 使用ICMP回应取代UDP资料信息。

-m 设置检测数据包的最大存活数值TTL的大小。

-n 直接使用IP地址而非主机名称。

-p 设置UDP传输协议的通信端口。

-r 忽略普通的Routing Table,直接将数据包送到远端主机上。

-s 设置本地主机送出数据包的IP地址。

-t 设置检测数据包的TOS数值。

-v 详细显示指令的执行过程。

-w 设置等待远端主机回报的时间。

-x 开启或关闭数据包的正确性检验。

traceroute命令使用示例

**实例1:**traceroute 用法简单、最常用的用法

命令:

traceroute www.baidu.com 

输出:

[root@localhost ~]# traceroute www.baidu.com
traceroute to www.baidu.com (61.135.169.125), 30 hops max, 40 byte packets
 1 192.168.74.2 (192.168.74.2) 2.606 ms 2.771 ms 2.950 ms
 2 211.151.56.57 (211.151.56.57) 0.596 ms 0.598 ms 0.591 ms
 3 211.151.227.206 (211.151.227.206) 0.546 ms 0.544 ms 0.538 ms
 4 210.77.139.145 (210.77.139.145) 0.710 ms 0.748 ms 0.801 ms
 5 202.106.42.101 (202.106.42.101) 6.759 ms 6.945 ms 7.107 ms
 6 61.148.154.97 (61.148.154.97) 718.908 ms * bt-228-025.bta.net.cn (202.106.228.25) 5.177 ms
 7 124.65.58.213 (124.65.58.213) 4.343 ms 4.336 ms 4.367 ms
 8 202.106.35.190 (202.106.35.190) 1.795 ms 61.148.156.138 (61.148.156.138) 1.899 ms 1.951 ms
 9 * * *
30 * * *
[root@localhost ~]# 

说明:

记录按序列号从1开始,每个纪录就是一跳 ,每跳表示一个网关,我们看到每行有三个时间,单位是 ms,其实就是-q的默认参数。探测数据包向每个网关发送三个数据包后,网关响应后返回的时间;如果您用 traceroute -q 4 www.58.com ,表示向每个网关发送4个数据包。

有时我们traceroute 一台主机时,会看到有一些行是以星号表示的。出现这样的情况,可能是防火墙封掉了ICMP的返回信息,所以我们得不到什么相关的数据包返回数据。

有时我们在某一网关处延时比较长,有可能是某台网关比较阻塞,也可能是物理设备本身的原因。当然如果某台DNS出现问题时,不能解析主机名、域名时,也会 有延时长的现象;您可以加-n 参数来避免DNS解析,以IP格式输出数据。

如果在局域网中的不同网段之间,我们可以通过traceroute 来排查问题所在,是主机的问题还是网关的问题。如果我们通过远程来访问某台服务器遇到问题时,我们用到traceroute 追踪数据包所经过的网关,提交IDC服务商,也有助于解决问题;但目前看来在国内解决这样的问题是比较困难的,就是我们发现问题所在,IDC服务商也不可能帮助我们解决。

**实例2:**跳数设置

命令:

traceroute -m 10 www.baidu.com

输出:

[root@localhost ~]# traceroute -m 10 www.baidu.com
traceroute to www.baidu.com (61.135.169.105), 10 hops max, 40 byte packets
 1 192.168.74.2 (192.168.74.2) 1.534 ms 1.775 ms 1.961 ms
 2 211.151.56.1 (211.151.56.1) 0.508 ms 0.514 ms 0.507 ms
 3 211.151.227.206 (211.151.227.206) 0.571 ms 0.558 ms 0.550 ms
 4 210.77.139.145 (210.77.139.145) 0.708 ms 0.729 ms 0.785 ms
 5 202.106.42.101 (202.106.42.101) 7.978 ms 8.155 ms 8.311 ms
 6 bt-228-037.bta.net.cn (202.106.228.37) 772.460 ms bt-228-025.bta.net.cn (202.106.228.25) 2.152 ms 61.148.154.97 (61.148.154.97) 772.107 ms
 7 124.65.58.221 (124.65.58.221) 4.875 ms 61.148.146.29 (61.148.146.29) 2.124 ms 124.65.58.221 (124.65.58.221) 4.854 ms
 8 123.126.6.198 (123.126.6.198) 2.944 ms 61.148.156.6 (61.148.156.6) 3.505 ms 123.126.6.198 (123.126.6.198) 2.885 ms
 9 * * *
10 * * *
[root@localhost ~]#

说明:

**实例3:**显示IP地址,不查主机名

命令:

traceroute -n www.baidu.com

输出:

[root@localhost ~]# traceroute -n www.baidu.com
traceroute to www.baidu.com (61.135.169.125), 30 hops max, 40 byte packets
 1 211.151.74.2 5.430 ms 5.636 ms 5.802 ms
 2 211.151.56.57 0.627 ms 0.625 ms 0.617 ms
 3 211.151.227.206 0.575 ms 0.584 ms 0.576 ms
 4 210.77.139.145 0.703 ms 0.754 ms 0.806 ms
 5 202.106.42.101 23.683 ms 23.869 ms 23.998 ms
 6 202.106.228.37 247.101 ms * *
 7 61.148.146.29 5.256 ms 124.65.58.213 4.386 ms 4.373 ms
 8 202.106.35.190 1.610 ms 61.148.156.138 1.786 ms 61.148.3.34 2.089 ms
 9 * * *
30 * * *
[root@localhost ~]# traceroute www.baidu.com
traceroute to www.baidu.com (61.135.169.125), 30 hops max, 40 byte packets
 1 211.151.74.2 (211.151.74.2) 4.671 ms 4.865 ms 5.055 ms
 2 211.151.56.57 (211.151.56.57) 0.619 ms 0.618 ms 0.612 ms
 3 211.151.227.206 (211.151.227.206) 0.620 ms 0.642 ms 0.636 ms
 4 210.77.139.145 (210.77.139.145) 0.720 ms 0.772 ms 0.816 ms
 5 202.106.42.101 (202.106.42.101) 7.667 ms 7.910 ms 8.012 ms
 6 bt-228-025.bta.net.cn (202.106.228.25) 2.965 ms 2.440 ms 61.148.154.97 (61.148.154.97) 431.337 ms
 7 124.65.58.213 (124.65.58.213) 5.134 ms 5.124 ms 5.044 ms
 8 202.106.35.190 (202.106.35.190) 1.917 ms 2.052 ms 2.059 ms
 9 * * *
30 * * *
[root@localhost ~]# 

说明:

**实例4:**探测包使用的基本UDP端口设置6888

命令:

traceroute -p 6888 www.baidu.com

输出:

[root@localhost ~]# traceroute -p 6888 www.baidu.com
traceroute to www.baidu.com (220.181.111.147), 30 hops max, 40 byte packets
 1 211.151.74.2 (211.151.74.2) 4.927 ms 5.121 ms 5.298 ms
 2 211.151.56.1 (211.151.56.1) 0.500 ms 0.499 ms 0.509 ms
 3 211.151.224.90 (211.151.224.90) 0.637 ms 0.631 ms 0.641 ms
 4 * * *
 5 220.181.70.98 (220.181.70.98) 5.050 ms 5.313 ms 5.596 ms
 6 220.181.17.94 (220.181.17.94) 1.665 ms !X * *
[root@localhost ~]# 

说明:

**实例5:**把探测包的个数设置为值4

命令:

traceroute -q 4 www.baidu.com

输出:

[root@localhost ~]# traceroute -q 4 www.baidu.com
traceroute to www.baidu.com (61.135.169.125), 30 hops max, 40 byte packets
 1 211.151.74.2 (211.151.74.2) 40.633 ms 40.819 ms 41.004 ms 41.188 ms
 2 211.151.56.57 (211.151.56.57) 0.637 ms 0.633 ms 0.627 ms 0.619 ms
 3 211.151.227.206 (211.151.227.206) 0.505 ms 0.580 ms 0.571 ms 0.569 ms
 4 210.77.139.145 (210.77.139.145) 0.753 ms 0.800 ms 0.853 ms 0.904 ms
 5 202.106.42.101 (202.106.42.101) 7.449 ms 7.543 ms 7.738 ms 7.893 ms
 6 61.148.154.97 (61.148.154.97) 316.817 ms bt-228-025.bta.net.cn (202.106.228.25) 3.695 ms 3.672 ms *
 7 124.65.58.213 (124.65.58.213) 3.056 ms 2.993 ms 2.960 ms 61.148.146.29 (61.148.146.29) 2.837 ms
 8 61.148.3.34 (61.148.3.34) 2.179 ms 2.295 ms 2.442 ms 202.106.35.190 (202.106.35.190) 7.136 ms
 9 * * * *
30 * * * *
[root@localhost ~]# 

说明:

**实例6:**绕过正常的路由表,直接发送到网络相连的主机

命令:

 traceroute -r www.baidu.com

输出:

[root@localhost ~]# traceroute -r www.baidu.com
traceroute to www.baidu.com (61.135.169.125), 30 hops max, 40 byte packets
connect: 网络不可达
[root@localhost ~]# 

说明:

**实例7:**把对外发探测包的等待响应时间设置为3秒

命令:

traceroute -w 3 www.baidu.com

输出:

[root@localhost ~]# traceroute -w 3 www.baidu.com
traceroute to www.baidu.com (61.135.169.105), 30 hops max, 40 byte packets
 1 211.151.74.2 (211.151.74.2) 2.306 ms 2.469 ms 2.650 ms
 2 211.151.56.1 (211.151.56.1) 0.621 ms 0.613 ms 0.603 ms
 3 211.151.227.206 (211.151.227.206) 0.557 ms 0.560 ms 0.552 ms
 4 210.77.139.145 (210.77.139.145) 0.708 ms 0.761 ms 0.817 ms
 5 202.106.42.101 (202.106.42.101) 7.520 ms 7.774 ms 7.902 ms
 6 bt-228-025.bta.net.cn (202.106.228.25) 2.890 ms 2.369 ms 61.148.154.97 (61.148.154.97) 471.961 ms
 7 124.65.58.221 (124.65.58.221) 4.490 ms 4.483 ms 4.472 ms
 8 123.126.6.198 (123.126.6.198) 2.948 ms 61.148.156.6 (61.148.156.6) 7.688 ms 7.756 ms
 9 * * *
30 * * *
[root@localhost ~]# 

说明:

Traceroute的工作原理:

Traceroute最简单的基本用法是:traceroute hostname

Traceroute程序的设计是利用ICMP及IP header的TTL(Time To Live)栏位(field)。首先,traceroute送出一个TTL是1的IP datagram(其实,每次送出的为3个40字节的包,包括源地址,目的地址和包发出的时间标签)到目的地,当路径上的第一个路由器(router)收到这个datagram时,它将TTL减1。此时,TTL变为0了,所以该路由器会将此datagram丢掉,并送回一个「ICMP time exceeded」消息(包括发IP包的源地址,IP包的所有内容及路由器的IP地址),traceroute 收到这个消息后,便知道这个路由器存在于这个路径上,接着traceroute 再送出另一个TTL是2 的datagram,发现第2 个路由器… traceroute 每次将送出的datagram的TTL 加1来发现另一个路由器,这个重复的动作一直持续到某个datagram 抵达目的地。当datagram到达目的地后,该主机并不会送回ICMP time exceeded消息,因为它已是目的地了,那么traceroute如何得知目的地到达了呢?

Traceroute在送出UDP datagrams到目的地时,它所选择送达的port number 是一个一般应用程序都不会用的号码(30000 以上),所以当此UDP datagram 到达目的地后该主机会送回一个「ICMP port unreachable」的消息,而当traceroute 收到这个消息时,便知道目的地已经到达了。所以traceroute 在Server端也是没有所谓的Daemon 程式。

Traceroute提取发 ICMP TTL到期消息设备的IP地址并作域名解析。每次 ,Traceroute都打印出一系列数据,包括所经过的路由设备的域名及 IP地址,三个包每次来回所花时间。

netstat命令语法

netstat命令用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况。netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP监听,进程内存管理的相关报告。

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

**1.**命令格式:

netstat [-acCeFghilMnNoprstuvVwx][-A<网络类型>][–ip]

**2.*命令功能:

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

**3.**命令参数:

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

-A<网络类型>或–<网络类型> 列出该网络类型连线中的相关地址。

-c或–continuous 持续列出网络状态。

-C或–cache 显示路由器配置的快取信息。

-e或–extend 显示网络其他相关信息。

-F或–fib 显示FIB。

-g或–groups 显示多重广播功能群组组员名单。

-h或–help 在线帮助。

-i或–interfaces 显示网络界面信息表单。

-l或–listening 显示监控中的服务器的Socket。

-M或–masquerade 显示伪装的网络连线。

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

-N或–netlink或–symbolic 显示网络硬件外围设备的符号连接名称。

-o或–timers 显示计时器。

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

-r或–route 显示Routing Table。

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

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

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

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

-V或–version 显示版本信息。

-w或–raw 显示RAW传输协议的连线状况。

-x或–unix 此参数的效果和指定”-A unix”参数相同。

–ip或–inet 此参数的效果和指定”-A inet”参数相同。

netstat命令使用示例

实例1:无参数使用

命令:

netstat

输出:

[root@localhost ~]# netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State   
tcp    0  268 192.168.120.204:ssh     10.2.0.68:62420       ESTABLISHED 
udp    0   0 192.168.120.204:4371    10.58.119.119:domain    ESTABLISHED 
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags    Type    State     I-Node Path
unix 2   [ ]     DGRAM          1491  @/org/kernel/udev/udevd
unix 4   [ ]     DGRAM          7337  /dev/log
unix 2   [ ]     DGRAM          708823 
unix 2   [ ]     DGRAM          7539  
unix 3   [ ]     STREAM   CONNECTED   7287  
unix 3   [ ]     STREAM   CONNECTED   7286  
[root@localhost ~]#

说明:

从整体上看,netstat的输出结果可以分为两个部分:

一个是Active Internet connections,称为有源TCP连接,其中"Recv-Q"和"Send-Q"指的是接收队列和发送队列。这些数字一般都应该是0。如果不是则表示软件包正在队列中堆积。这种情况只能在非常少的情况见到。

另一个是Active UNIX domain sockets,称为有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。

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

套接口类型:

-t :TCP

-u :UDP

-raw :RAW类型

–unix :UNIX域类型

–ax25 :AX25类型

–ipx :ipx类型

–netrom :netrom类型

状态说明:

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:列出所有端口

命令:

netstat -a

输出:

[root@localhost ~]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address        Foreign Address       State   
tcp    0   0 localhost:smux       *:*             LISTEN   
tcp    0   0 *:svn            *:*             LISTEN   
tcp    0   0 *:ssh            *:*             LISTEN   
tcp    0  284 192.168.120.204:ssh     10.2.0.68:62420       ESTABLISHED 
udp    0   0 localhost:syslog      *:*                   
udp    0   0 *:snmp           *:*                   
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags    Type    State     I-Node Path
unix 2   [ ACC ]   STREAM   LISTENING   708833 /tmp/ssh-yKnDB15725/agent.15725
unix 2   [ ACC ]   STREAM   LISTENING   7296  /var/run/audispd_events
unix 2   [ ]     DGRAM          1491  @/org/kernel/udev/udevd
unix 4   [ ]     DGRAM          7337  /dev/log
unix 2   [ ]     DGRAM          708823 
unix 2   [ ]     DGRAM          7539  
unix 3   [ ]     STREAM   CONNECTED   7287  
unix 3   [ ]     STREAM   CONNECTED   7286  
[root@localhost ~]# 

说明:

显示一个所有的有效连接信息列表,包括已建立的连接(ESTABLISHED),也包括监听连接请(LISTENING)的那些连接。

实例3:显示当前UDP连接状况

命令:

netstat -nu

输出:

[root@andy ~]# netstat -nu
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State   
udp    0   0 ::ffff:192.168.12:53392   ::ffff:192.168.9.120:10000 ESTABLISHED 
udp    0   0 ::ffff:192.168.12:56723   ::ffff:192.168.9.120:10000 ESTABLISHED 
udp    0   0 ::ffff:192.168.12:56480   ::ffff:192.168.9.120:10000 ESTABLISHED 
udp    0   0 ::ffff:192.168.12:58154   ::ffff:192.168.9.120:10000 ESTABLISHED 
udp    0   0 ::ffff:192.168.12:44227   ::ffff:192.168.9.120:10000 ESTABLISHED 
udp    0   0 ::ffff:192.168.12:36954   ::ffff:192.168.9.120:10000 ESTABLISHED 
udp    0   0 ::ffff:192.168.12:53984   ::ffff:192.168.9.120:10000 ESTABLISHED 
udp    0   0 ::ffff:192.168.12:57703   ::ffff:192.168.9.120:10000 ESTABLISHED 
udp    0   0 ::ffff:192.168.12:53613   ::ffff:192.168.9.120:10000 ESTABLISHED 
[root@andy ~]# 

说明:

实例4:显示UDP端口号的使用情况

命令:

netstat -apu

输出:

[root@andy ~]# netstat -apu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address        Foreign Address       State    PID/Program name  
udp    0   0 *:57604           *:*                   28094/java     
udp    0   0 *:40583           *:*                   21220/java     
udp    0   0 *:45451           *:*                   14583/java     
udp    0   0 ::ffff:192.168.12:53392   ::ffff:192.168.9.120:ndmp  ESTABLISHED 19327/java     
udp    0   0 *:52370           *:*                   15841/java     
udp    0   0 ::ffff:192.168.12:56723   ::ffff:192.168.9.120:ndmp  ESTABLISHED 15841/java     
udp    0   0 *:44182           *:*                   31757/java     
udp    0   0 *:48155           *:*                   5476/java      
udp    0   0 *:59808           *:*                   17333/java     
udp    0   0 ::ffff:192.168.12:56480   ::ffff:192.168.9.120:ndmp  ESTABLISHED 28094/java     
udp    0   0 ::ffff:192.168.12:58154   ::ffff:192.168.9.120:ndmp  ESTABLISHED 15429/java     
udp    0   0 *:36780           *:*                   10091/java     
udp    0   0 *:36795           *:*                   24594/java     
udp    0   0 *:41922           *:*                   20506/java     
udp    0   0 ::ffff:192.168.12:44227   ::ffff:192.168.9.120:ndmp  ESTABLISHED 17333/java     
udp    0   0 *:34258           *:*                   8866/java      
udp    0   0 *:55508           *:*                   11667/java     
udp    0   0 *:36055           *:*                   12425/java     
udp    0   0 ::ffff:192.168.12:36954   ::ffff:192.168.9.120:ndmp  ESTABLISHED 16532/java     
udp    0   0 ::ffff:192.168.12:53984   ::ffff:192.168.9.120:ndmp  ESTABLISHED 20506/java     
udp    0   0 ::ffff:192.168.12:57703   ::ffff:192.168.9.120:ndmp  ESTABLISHED 31757/java     
udp    0   0 ::ffff:192.168.12:53613   ::ffff:192.168.9.120:ndmp  ESTABLISHED 3199/java      
udp    0   0 *:56309           *:*                   15429/java     
udp    0   0 *:54007           *:*                   16532/java     
udp    0   0 *:39544           *:*                   3199/java      
udp    0   0 *:43900           *:*                   19327/java     
[root@andy ~]# 

说明:

实例5:显示网卡列表

命令:

netstat -i

输出:

[root@andy ~]# netstat -i
Kernel Interface table
Iface    MTU Met  RX-OK RX-ERR RX-DRP RX-OVR  TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0    1500  0 151818887   0   0   0 198928403   0   0   0 BMRU
lo    16436  0  107235   0   0   0  107235   0   0   0 LRU
[root@andy ~]# 

说明:

实例6:显示组播组的关系

命令:

netstat -g

输出:

[root@andy ~]# netstat -g
IPv6/IPv4 Group Memberships
Interface    RefCnt Group
--------------- ------ ---------------------
lo       1   all-systems.mcast.net
eth0      1   all-systems.mcast.net
lo       1   ff02::1
eth0      1   ff02::1:ffff:9b0c
eth0      1   ff02::1
[root@andy ~]# 

说明:

实例7:显示网络统计信息

命令:

netstat -s

输出:

[root@localhost ~]# netstat -s
Ip:
  530999 total packets received
  0 forwarded
  0 incoming packets discarded
  530999 incoming packets delivered
  8258 requests sent out
  1 dropped because of missing route
Icmp:
  90 ICMP messages received
  0 input ICMP message failed.
  ICMP input histogram:
    destination unreachable: 17
    echo requests: 1
    echo replies: 72
  106 ICMP messages sent
  0 ICMP messages failed
  ICMP output histogram:
    destination unreachable: 8
    echo request: 97
    echo replies: 1
IcmpMsg:
    InType0: 72
    InType3: 17
    InType8: 1
    OutType0: 1
    OutType3: 8
    OutType8: 97
Tcp:
  8 active connections openings
  15 passive connection openings
  8 failed connection attempts
  3 connection resets received
  1 connections established
  3132 segments received
  2617 segments send out
  53 segments retransmited
  0 bad segments received.
  252 resets sent
Udp:
  0 packets received
  0 packets to unknown port received.
  0 packet receive errors
  5482 packets sent
TcpExt:
  1 invalid SYN cookies received
  1 TCP sockets finished time wait in fast timer
  57 delayed acks sent
  Quick ack mode was activated 50 times
  60 packets directly queued to recvmsg prequeue.
  68 packets directly received from backlog
  4399 packets directly received from prequeue
  520 packets header predicted
  51 packets header predicted and directly queued to user
  1194 acknowledgments not containing data received
  21 predicted acknowledgments
  0 TCP data loss events
  1 timeouts after reno fast retransmit
  9 retransmits in slow start
  42 other TCP timeouts
  3 connections aborted due to timeout
IpExt:
  InBcastPkts: 527777

说明:

按照各个协议分别显示其统计数据。如果我们的应用程序(如Web浏览器)运行速度比较慢,或者不能显示Web页之类的数据,那么我们就可以用本选项来查看一下所显示的信息。我们需要仔细查看统计数据的各行,找到出错的关键字,进而确定问题所在。

实例8:显示监听的套接口

命令:

netstat -l

输出:

[root@localhost ~]# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State   
tcp    0   0 localhost:smux       *:*             LISTEN   
tcp    0   0 *:svn            *:*             LISTEN   
tcp    0   0 *:ssh            *:*             LISTEN   
udp    0   0 localhost:syslog      *:*                   
udp    0   0 *:snmp           *:*                   
Active UNIX domain sockets (only servers)
Proto RefCnt Flags    Type    State     I-Node Path
unix 2   [ ACC ]   STREAM   LISTENING   708833 /tmp/ssh-yKnDB15725/agent.15725
unix 2   [ ACC ]   STREAM   LISTENING   7296  /var/run/audispd_events
[root@localhost ~]# 

说明:

实例9:显示所有已建立的有效连接

命令:

netstat -n

输出:

[root@localhost ~]# netstat -n
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State   
tcp    0  268 192.168.120.204:22     10.2.0.68:62420       ESTABLISHED 
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags    Type    State     I-Node Path
unix 2   [ ]     DGRAM          1491  @/org/kernel/udev/udevd
unix 4   [ ]     DGRAM          7337  /dev/log
unix 2   [ ]     DGRAM          708823 
unix 2   [ ]     DGRAM          7539  
unix 3   [ ]     STREAM   CONNECTED   7287  
unix 3   [ ]     STREAM   CONNECTED   7286  
[root@localhost ~]# 

说明:

实例10:显示关于以太网的统计数据

命令:

netstat -e

输出:

[root@localhost ~]# netstat -e
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State    User    Inode   
tcp    0  248 192.168.120.204:ssh     10.2.0.68:62420       ESTABLISHED root    708795   
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags    Type    State     I-Node Path
unix 2   [ ]     DGRAM          1491  @/org/kernel/udev/udevd
unix 4   [ ]     DGRAM          7337  /dev/log
unix 2   [ ]     DGRAM          708823 
unix 2   [ ]     DGRAM          7539  
unix 3   [ ]     STREAM   CONNECTED   7287  
unix 3   [ ]     STREAM   CONNECTED   7286  
[root@localhost ~]#

说明:

用于显示关于以太网的统计数据。它列出的项目包括传送的数据报的总字节数、错误数、删除数、数据报的数量和广播的数量。这些统计数据既有发送的数据报数量,也有接收的数据报数量。这个选项可以用来统计一些基本的网络流量)

实例11:显示关于路由表的信息

命令:

netstat -r

输出:

[root@localhost ~]# netstat -r
Kernel IP routing table
Destination   Gateway     Genmask     Flags  MSS Window irtt Iface
192.168.120.0  *        255.255.255.0  U     0 0     0 eth0
192.168.0.0   192.168.120.1  255.255.0.0   UG    0 0     0 eth0
10.0.0.0    192.168.120.1  255.0.0.0    UG    0 0     0 eth0
default     192.168.120.240 0.0.0.0     UG    0 0     0 eth0
[root@localhost ~]# 

说明:

实例12:列出所有 tcp 端口

命令:

netstat -at

输出:

[root@localhost ~]# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address        Foreign Address       State   
tcp    0   0 localhost:smux       *:*             LISTEN   
tcp    0   0 *:svn            *:*             LISTEN   
tcp    0   0 *:ssh            *:*             LISTEN   
tcp    0  284 192.168.120.204:ssh     10.2.0.68:62420       ESTABLISHED 
[root@localhost ~]#

说明:

实例13:统计机器中网络连接各个状态个数

命令:

netstat -a | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

输出:

[root@localhost ~]# netstat -a | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
ESTABLISHED 1
LISTEN 3
[root@localhost ~]# 

说明:

实例14:把状态全都取出来后使用uniq -c统计后再进行排序

命令:

netstat -nat |awk '{print $6}'|sort|uniq -c

输出:

[root@andy ~]# netstat -nat |awk '{print $6}'|sort|uniq -c
   14 CLOSE_WAIT
   1 established)
  578 ESTABLISHED
   1 Foreign
   43 LISTEN
   5 TIME_WAIT
[root@andy ~]# netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
  576 ESTABLISHED
   43 LISTEN
   14 CLOSE_WAIT
   5 TIME_WAIT
   1 Foreign
   1 established)
[root@andy ~]#

**说明:

实例15:*查看连接某服务端口最多的的IP地址

命令:

netstat -nat | grep "192.168.120.20:16067" |awk '{print $5}'|awk -F: '{print $4}'|sort|uniq -c|sort -nr|head -20

输出:

[root@andy ~]# netstat -nat | grep "192.168.120.20:16067" |awk '{print $5}'|awk -F: '{print $4}'|sort|uniq -c|sort -nr|head -20
   8 10.2.1.68
   7 192.168.119.13
   6 192.168.119.201
   6 192.168.119.20
   6 192.168.119.10
   4 10.2.1.199
   3 10.2.1.207
   2 192.168.120.20
   2 192.168.120.15
   2 192.168.119.197
   2 192.168.119.11
   2 10.2.1.206
   2 10.2.1.203
   2 10.2.1.189
   2 10.2.1.173
   1 192.168.120.18
   1 192.168.119.19
   1 10.2.2.227
   1 10.2.2.138
   1 10.2.1.208
[root@andy ~]# 

说明:

实例16:找出程序运行的端口

命令:

netstat -ap | grep ssh

输出:

[root@andy ~]# netstat -ap | grep ssh
tcp    0   0 *:ssh            *:*             LISTEN   2570/sshd      
tcp    0   0 ::ffff:192.168.120.206:ssh ::ffff:10.2.1.205:54508   ESTABLISHED 13883/14      
tcp    0   0 ::ffff:192.168.120.206:ssh ::ffff:10.2.0.68:62886   ESTABLISHED 20900/6       
tcp    0   0 ::ffff:192.168.120.206:ssh ::ffff:10.2.2.131:52730   ESTABLISHED 20285/sshd: root@no 
unix 2   [ ACC ]   STREAM   LISTENING   194494461 20900/6       /tmp/ssh-cXIJj20900/agent.20900
unix 3   [ ]     STREAM   CONNECTED   194307443 20285/sshd: root@no 
unix 3   [ ]     STREAM   CONNECTED   194307441 20285/sshd: root@no 
[root@andy ~]# 

说明:

**实例17:**在 netstat 输出中显示 PID 和进程名称

命令:

netstat -pt

输出:

[root@localhost ~]# netstat -pt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State    PID/Program name  
tcp    0  248 192.168.120.204:ssh     10.2.0.68:62420       ESTABLISHED 15725/0       
[root@localhost ~]# 

说明:

netstat -p 可以与其它开关一起使用,就可以添加 “PID/进程名称” 到 netstat 输出中,这样 debugging 的时候可以很方便的发现特定端口运行的程序。

实例18:找出运行在指定端口的进程

命令:

netstat -anpt | grep ':16064'

输出:

[root@andy ~]# netstat -anpt | grep ':16064'
tcp    0   0 :::16064          :::*            LISTEN   24594/java     
tcp    0   0 ::ffff:192.168.120.20:16064 ::ffff:192.168.119.201:6462 ESTABLISHED 24594/java     
tcp    0   0 ::ffff:192.168.120.20:16064 ::ffff:192.168.119.20:26341 ESTABLISHED 24594/java     
tcp    0   0 ::ffff:192.168.120.20:16064 ::ffff:192.168.119.20:32208 ESTABLISHED 24594/java     
tcp    0   0 ::ffff:192.168.120.20:16064 ::ffff:192.168.119.20:32207 ESTABLISHED 24594/java     
tcp    0   0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:51303   ESTABLISHED 24594/java     
tcp    0   0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:51302   ESTABLISHED 24594/java     
tcp    0   0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:50020   ESTABLISHED 24594/java     
tcp    0   0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:50019   ESTABLISHED 24594/java     
tcp    0   0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:56155   ESTABLISHED 24594/java     
tcp    0   0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:50681   ESTABLISHED 24594/java     
tcp    0   0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:50680   ESTABLISHED 24594/java     
tcp    0   0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:52136   ESTABLISHED 24594/java     
tcp    0   0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:56989   ESTABLISHED 24594/java     
tcp    0   0 ::ffff:192.168.120.20:16064 ::ffff:10.2.1.68:56988   ESTABLISHED 24594/java     
[root@andy ~]# 

说明:

运行在端口16064的进程id为24596,再通过ps命令就可以找到具体的应用程序了。

ss命令语法

ss是Socket Statistics的缩写。顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容。但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信息,而且比netstat更快速更高效。

当服务器的socket连接数量变得非常大时,无论是使用netstat命令还是直接cat /proc/net/tcp,执行速度都会很慢。可能你不会有切身的感受,但请相信我,当服务器维持的连接达到上万个的时候,使用netstat等于浪费 生命,而用ss才是节省时间。

天下武功唯快不破。ss快的秘诀在于,它利用到了TCP协议栈中tcp_diag。tcp_diag是一个用于分析统计的模块,可以获得Linux 内核中第一手的信息,这就确保了ss的快捷高效。当然,如果你的系统中没有tcp_diag,ss也可以正常运行,只是效率会变得稍慢。(但仍然比 netstat要快。)

**1.**命令格式:

ss [参数]

ss [参数] [过滤]

**2.**命令功能:

ss(Socket Statistics的缩写)命令可以用来获取 socket统计信息,此命令输出的结果类似于 netstat输出的内容,但它能显示更多更详细的 TCP连接状态的信息,且比 netstat 更快速高效。它使用了 TCP协议栈中 tcp_diag(是一个用于分析统计的模块),能直接从获得第一手内核信息,这就使得 ss命令快捷高效。在没有 tcp_diag,ss也可以正常运行。

**3.**命令参数:

-h, --help 帮助信息

-V, --version 程序版本信息

-n, --numeric 不解析服务名称

-r, --resolve    解析主机名

-a, --all 显示所有套接字(sockets)

-l, --listening 显示监听状态的套接字(sockets)

-o, --options    显示计时器信息

-e, --extended    显示详细的套接字(sockets)信息

-m, --memory     显示套接字(socket)的内存使用情况

-p, --processes 显示使用套接字(socket)的进程

-i, --info 显示 TCP内部信息

-s, --summary 显示套接字(socket)使用概况

-4, --ipv4      仅显示IPv4的套接字(sockets)

-6, --ipv6      仅显示IPv6的套接字(sockets)

-0, --packet     显示 PACKET 套接字(socket)

-t, --tcp 仅显示 TCP套接字(sockets)

-u, --udp 仅显示 UCP套接字(sockets)

-d, --dccp 仅显示 DCCP套接字(sockets)

-w, --raw 仅显示 RAW套接字(sockets)

-x, --unix 仅显示 Unix套接字(sockets)

-f, --family=FAMILY 显示 FAMILY类型的套接字(sockets),FAMILY可选,支持 unix, inet, inet6, link, netlink

-A, --query=QUERY, --socket=QUERY

   QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]

-D, --diag=FILE   将原始TCP套接字(sockets)信息转储到文件

 -F, --filter=FILE  从文件中都去过滤器信息

    FILTER := [ state TCP-STATE ] [ EXPRESSION ]

ss命令使用示例

**实例1:**显示TCP连接

命令:

ss -t -a

输出:

[root@localhost ~]# ss -t -a
State   Recv-Q Send-Q                Local Address:Port                  Peer Address:Port  
LISTEN   0   0                     127.0.0.1:smux                        *:*    
LISTEN   0   0                         *:3690                        *:*    
LISTEN   0   0                         *:ssh                        *:*    
ESTAB   0   0                  192.168.120.204:ssh                    10.2.0.68:49368  
[root@localhost ~]# 

说明:

**实例2:**显示 Sockets 摘要

命令:

ss -s

输出:

[root@localhost ~]# ss -s
Total: 34 (kernel 48)
TCP:  4 (estab 1, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 3

Transport Total   IP    IPv6
\*     48    -     -    
RAW    0     0     0    
UDP    5     5     0    
TCP    4     4     0    
INET   9     9     0    
FRAG   0     0     0    

[root@localhost ~]# 

说明:

列出当前的established, closed, orphaned and waiting TCP sockets

**实例3:**列出所有打开的网络连接端口

命令:

ss -l

输出:

[root@localhost ~]# ss -l
Recv-Q Send-Q                   Local Address:Port                     Peer Address:Port  
0   0                       127.0.0.1:smux                          *:*    
0   0                           *:3690                          *:*    
0   0                           *:ssh                           *:*    
[root@localhost ~]# 

说明:

**实例4:**查看进程使用的socket

命令:

ss -pl

输出:

[root@localhost ~]# ss -pl
Recv-Q Send-Q                   Local Address:Port                     Peer Address:Port  
0   0                       127.0.0.1:smux                          *:*    users:(("snmpd",2716,8))
0   0                           *:3690                          *:*    users:(("svnserve",3590,3))
0   0                           *:ssh                           *:*    users:(("sshd",2735,3))
[root@localhost ~]#

**实例5:**找出打开套接字/端口应用程序

命令:

ss -lp | grep 3306

输出:

[root@localhost ~]# ss -lp|grep 1935
0   0              *:1935             *:*    users:(("fmsedge",2913,18))
0   0          127.0.0.1:19350             *:*    users:(("fmsedge",2913,17))
[root@localhost ~]# ss -lp|grep 3306
0   0              *:3306             *:*    users:(("mysqld",2871,10))
[root@localhost ~]# 

说明:

**实例6:**显示所有UDP Sockets

命令:

ss -u -a

输出:

[root@localhost ~]# ss -u -a
State   Recv-Q Send-Q                Local Address:Port                  Peer Address:Port  
UNCONN   0   0                     127.0.0.1:syslog                       *:*    
UNCONN   0   0                         *:snmp                        *:*    
ESTAB   0   0                  192.168.120.203:39641                 10.58.119.119:domain 
[root@localhost ~]#

说明:

**实例7:**显示所有状态为established的SMTP连接

命令:

ss -o state established '( dport = :smtp or sport = :smtp )' 

输出:

[root@localhost ~]# ss -o state established '( dport = :smtp or sport = :smtp )' 
Recv-Q Send-Q                   Local Address:Port                     Peer Address:Port  
[root@localhost ~]#

说明:

**实例8:**显示所有状态为Established的HTTP连接

命令:

ss -o state established '( dport = :http or sport = :http )' 



### 最后的话

最近很多小伙伴找我要Linux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!

### 资料预览

给大家整理的视频资料:

![](https://img-blog.csdnimg.cn/img_convert/a08a48596fa614ecb10d20d935bd4462.png)

给大家整理的电子书资料:

  

![](https://img-blog.csdnimg.cn/img_convert/cce7fda3feb8b9d8f717f33af8039c29.png)



**如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!**

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
ss:Port                     Peer Address:Port  
0   0                       127.0.0.1:smux                          *:*    users:(("snmpd",2716,8))
0   0                           *:3690                          *:*    users:(("svnserve",3590,3))
0   0                           *:ssh                           *:*    users:(("sshd",2735,3))
[root@localhost ~]#

**实例5:**找出打开套接字/端口应用程序

命令:

ss -lp | grep 3306

输出:

[root@localhost ~]# ss -lp|grep 1935
0   0              *:1935             *:*    users:(("fmsedge",2913,18))
0   0          127.0.0.1:19350             *:*    users:(("fmsedge",2913,17))
[root@localhost ~]# ss -lp|grep 3306
0   0              *:3306             *:*    users:(("mysqld",2871,10))
[root@localhost ~]# 

说明:

**实例6:**显示所有UDP Sockets

命令:

ss -u -a

输出:

[root@localhost ~]# ss -u -a
State   Recv-Q Send-Q                Local Address:Port                  Peer Address:Port  
UNCONN   0   0                     127.0.0.1:syslog                       *:*    
UNCONN   0   0                         *:snmp                        *:*    
ESTAB   0   0                  192.168.120.203:39641                 10.58.119.119:domain 
[root@localhost ~]#

说明:

**实例7:**显示所有状态为established的SMTP连接

命令:

ss -o state established '( dport = :smtp or sport = :smtp )' 

输出:

[root@localhost ~]# ss -o state established '( dport = :smtp or sport = :smtp )' 
Recv-Q Send-Q                   Local Address:Port                     Peer Address:Port  
[root@localhost ~]#

说明:

**实例8:**显示所有状态为Established的HTTP连接

命令:

ss -o state established '( dport = :http or sport = :http )' 



### 最后的话

最近很多小伙伴找我要Linux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!

### 资料预览

给大家整理的视频资料:

[外链图片转存中...(img-wxhCi0NN-1714586718206)]

给大家整理的电子书资料:

  

[外链图片转存中...(img-4nWhca5p-1714586718207)]



**如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!**

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值