【每天一个 Linux 命令】网络相关命令(ifconfig、route、ping、traceroute、netstat、ss、telnet、rcp、scp)

ifconfig命令

许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改。Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config)。通常需要以root身份登录或使用sudo以便在Linux机器上使用ifconfig工具。依赖于ifconfig命令中使用一些选项属性,ifconfig工具不仅可以被用来简单地获取网络接口配置信息,还可以修改这些配置。

1.命令格式:

ifconfig [网络设备] [参数]

2.命令功能:

ifconfig 命令用来查看和配置网络设备。当网络环境发生改变时可通过此命令对网络进行相应的配置。

3.命令参数:

up 启动指定网络设备/网卡。

down 关闭指定网络设备/网卡。该参数可以有效地阻止通过指定接口的IP信息流,如果想永久地关闭一个接口,我们还需要从核心路由表中将该接口的路由信息全部删除。

arp 设置指定网卡是否支持ARP协议。

-promisc 设置是否支持网卡的promiscuous模式,如果选择此参数,网卡将接收网络中发给它所有的数据包

-allmulti 设置是否支持多播模式,如果选择此参数,网卡将接收网络中所有的多播数据包

-a 显示全部接口信息

-s 显示摘要信息(类似于 netstat -i)

add 给指定网卡配置IPv6地址

del 删除指定网卡的IPv6地址

<硬件地址> 配置网卡最大的传输单元

mtu<字节数> 设置网卡的最大传输单元 (bytes)

netmask<子网掩码> 设置网卡的子网掩码。掩码可以是有前缀0x的32位十六进制数,也可以是用点分开的4个十进制数。如果不打算将网络分成子网,可以不管这一选项;如果要使用子网,那么请记住,网络中每一个系统必须有相同子网掩码。

tunel 建立隧道

dstaddr 设定一个远端地址,建立点对点通信

-broadcast<地址> 为指定网卡设置广播协议

-pointtopoint<地址> 为网卡设置点对点通讯协议

multicast 为网卡设置组播标志

address 为网卡设置IPv4地址

txqueuelen<长度> 为网卡设置传输列队的长度

ifconfig命令使用示例

实例1:显示网络设备信息(激活状态的)

命令:

ifconfig

输出:

[root@localhost ~]# ifconfig
eth0   Link encap:Ethernet HWaddr 00:50:56:BF:26:20 
     inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
     RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0
     TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000 
     RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 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:68 errors:0 dropped:0 overruns:0 frame:0
     TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0 
     RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB)

说明:

eth0 表示第一块网卡, 其中 HWaddr 表示网卡的物理地址,可以看到目前这个网卡的物理地址(MAC地址)是 00:50:56:BF:26:20

inet addr 用来表示网卡的IP地址,此网卡的 IP地址是 192.168.120.204,广播地址, Bcast:192.168.120.255,掩码地址Mask:255.255.255.0

lo 是表示主机的回坏地址,这个一般是用来测试一个网络程序,但又不想让局域网或外网的用户能够查看,只能在此台主机上运行和查看所用的网络接口。比如把 HTTPD服务器的指定到回坏地址,在浏览器输入 127.0.0.1 就能看到你所架WEB网站了。但只是您能看得到,局域网的其它主机或用户无从知道。

第一行:连接类型:Ethernet(以太网)HWaddr(硬件mac地址)

第二行:网卡的IP地址、子网、掩码

第三行:UP(代表网卡开启状态)RUNNING(代表网卡的网线被接上)MULTICAST(支持组播)MTU:1500(最大传输单元):1500字节

第四、五行:接收、发送数据包情况统计

第七行:接收、发送数据字节数统计信息。

实例2:启动关闭指定网卡

命令:

ifconfig eth0 up

ifconfig eth0 down

输出:

说明:

ifconfig eth0 up 为启动网卡eth0 ;ifconfig eth0 down 为关闭网卡eth0。ssh登陆linux服务器操作要小心,关闭了就不能开启了,除非你有多网卡。

实例3:为网卡配置和删除IPv6地址

命令:

ifconfig eth0 add 33ffe:3240:800:1005::2/64

ifconfig eth0 del 33ffe:3240:800:1005::2/64

输出:

说明:

ifconfig eth0 add 33ffe:3240:800:1005::2/64 为网卡eth0配置IPv6地址;

ifconfig eth0 add 33ffe:3240:800:1005::2/64 为网卡eth0删除IPv6地址;

练习的时候,ssh登陆linux服务器操作要小心,关闭了就不能开启了,除非你有多网卡。

实例4:用ifconfig修改MAC地址

命令:

ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE

输出:

[root@localhost ~]# ifconfig eth0 down //关闭网卡
[root@localhost ~]# ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE //修改MAC地址
[root@localhost ~]# ifconfig eth0 up //启动网卡
[root@localhost ~]# ifconfig
eth0   Link encap:Ethernet HWaddr 00:AA:BB:CC:DD:EE 
     inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
     RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0
     TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000 
     RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 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:68 errors:0 dropped:0 overruns:0 frame:0
     TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0 
     RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB)
[root@localhost ~]# ifconfig eth0 hw ether 00:50:56:BF:26:20 //关闭网卡并修改MAC地址 
[root@localhost ~]# ifconfig eth0 up //启动网卡
[root@localhost ~]# ifconfig
eth0   Link encap:Ethernet HWaddr 00:50:56:BF:26:20 
     inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
     RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0
     TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000 
     RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 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:68 errors:0 dropped:0 overruns:0 frame:0
     TX packets:68 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0 
     RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB) 

说明:

实例5:配置IP地址

命令:

输出:

[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 )' 

输出:

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

说明:

**实例9:**列举出处于 FIN-WAIT-1状态的源端口为 80或者 443,目标网络为 193.233.7/24所有 tcp套接字

命令:

ss -o state fin-wait-1 '( sport = :http or sport = :https )' dst 193.233.7/24

输出:

说明:

**实例10:**用TCP 状态过滤Sockets:

命令:

ss -4 state FILTER-NAME-HERE 

ss -6 state FILTER-NAME-HERE

输出:

[root@localhost ~]#ss -4 state closing 
Recv-Q Send-Q                         Local Address:Port                           Peer Address:Port 
1   11094                         75.126.153.214:http                           192.168.10.42:4669 

说明:

FILTER-NAME-HERE 可以代表以下任何一个:

established

syn-sent

syn-recv

fin-wait-1

fin-wait-2

time-wait

closed

close-wait

last-ack

listen

closing

all : 所有以上状态

connected : 除了listen and closed的所有状态

synchronized :所有已连接的状态除了syn-sent

bucket : 显示状态为maintained as minisockets,如:time-wait和syn-recv.

big : 和bucket相反.

**实例11:**匹配远程地址和端口号

命令:

ss dst ADDRESS_PATTERN

ss dst 192.168.1.5

ss dst 192.168.119.113:http 

ss dst 192.168.119.113:smtp 

ss dst 192.168.119.113:443

输出:

[root@localhost ~]# ss dst 192.168.119.113
State   Recv-Q Send-Q                Local Address:Port                  Peer Address:Port  
ESTAB   0   0                  192.168.119.103:16014                192.168.119.113:20229  
ESTAB   0   0                  192.168.119.103:16014                192.168.119.113:61056  
ESTAB   0   0                  192.168.119.103:16014                192.168.119.113:61623  
ESTAB   0   0                  192.168.119.103:16014                192.168.119.113:60924  
ESTAB   0   0                  192.168.119.103:16050                192.168.119.113:43701  
ESTAB   0   0                  192.168.119.103:16073                192.168.119.113:32930  
ESTAB   0   0                  192.168.119.103:16073                192.168.119.113:49318  
ESTAB   0   0                  192.168.119.103:16014                192.168.119.113:3844  
[root@localhost ~]# ss dst 192.168.119.113:http
State   Recv-Q Send-Q                Local Address:Port                  Peer Address:Port  
[root@localhost ~]# ss dst 192.168.119.113:3844
State   Recv-Q Send-Q                Local Address:Port                  Peer Address:Port  
ESTAB   0   0                  192.168.119.103:16014                192.168.119.113:3844  
[root@localhost ~]# 

说明:

**实例12:**匹配本地地址和端口号

命令:

ss src ADDRESS_PATTERN

ss src 192.168.119.103

ss src 192.168.119.103:http

ss src 192.168.119.103:80

ss src 192.168.119.103:smtp

ss src 192.168.119.103:25

输出:

[root@localhost ~]# ss src 192.168.119.103:16021
State   Recv-Q Send-Q                Local Address:Port                  Peer Address:Port  
ESTAB   0   0                  192.168.119.103:16021                192.168.119.201:63054  
ESTAB   0   0                  192.168.119.103:16021                192.168.119.201:62894  
ESTAB   0   0                  192.168.119.103:16021                192.168.119.201:63055  
ESTAB   0   0                  192.168.119.103:16021                192.168.119.201:2274  
ESTAB   0   0                  192.168.119.103:16021                192.168.119.201:44784  
ESTAB   0   0                  192.168.119.103:16021                192.168.119.201:7233  
ESTAB   0   0                  192.168.119.103:16021                192.168.119.103:58660  
ESTAB   0   0                  192.168.119.103:16021                192.168.119.201:44822  
ESTAB   0   0                  192.168.119.103:16021                   10.2.1.206:56737  
ESTAB   0   0                  192.168.119.103:16021                   10.2.1.206:57487  
ESTAB   0   0                  192.168.119.103:16021                   10.2.1.206:56736  
ESTAB   0   0                  192.168.119.103:16021                   10.2.1.206:64652  
ESTAB   0   0                  192.168.119.103:16021                   10.2.1.206:56586  
ESTAB   0   0                  192.168.119.103:16021                   10.2.1.206:64653  
ESTAB   0   0                  192.168.119.103:16021                   10.2.1.206:56587  
[root@localhost ~]# 

说明:

**实例13:**将本地或者远程端口和一个数比较

命令:

ss dport OP PORT 

ss sport OP PORT

输出:

[root@localhost ~]# ss sport = :http 
[root@localhost ~]# ss dport = :http 
[root@localhost ~]# ss dport \> :1024 
[root@localhost ~]# ss sport \> :1024 
[root@localhost ~]# ss sport \< :32000 
[root@localhost ~]# ss sport eq :22 
[root@localhost ~]# ss dport != :22 
[root@localhost ~]# ss state connected sport = :http 
[root@localhost ~]# ss \( sport = :http or sport = :https \) 
[root@localhost ~]# ss -o state fin-wait-1 \( sport = :http or sport = :https \) dst 192.168.1/24

说明:

ss dport OP PORT 远程端口和一个数比较;ss sport OP PORT 本地端口和一个数比较。

OP 可以代表以下任意一个:

<= or le : 小于或等于端口号

>= or ge : 大于或等于端口号

== or eq : 等于端口号

!= or ne : 不等于端口号

< or gt : 小于端口号

> or lt : 大于端口号

**实例14:**ss 和 netstat 效率对比

命令:

time netstat -at

time ss

输出:

[root@localhost ~]# time ss  
real  0m0.739s
user  0m0.019s
sys   0m0.013s
[root@localhost ~]# 
[root@localhost ~]# time netstat -at
real  2m45.907s
user  0m0.063s
sys   0m0.067s
[root@localhost ~]#

说明:

用time 命令分别获取通过netstat和ss命令获取程序和概要占用资源所使用的时间。在服务器连接数比较多的时候,netstat的效率完全没法和ss比。

telnet命令语法

telnet命令通常用来远程登录。telnet程序是基于TELNET协议的远程登录客户端程序。Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式。它为用户提供了在本地计算机上完成远程主机工作的 能力。在终端使用者的电脑上使用telnet程序,用它连接到服务器。终端使用者可以在telnet程序中输入命令,这些命令会在服务器上运行,就像直接在服务器的控制台上输入一样。可以在本地就能控制服务器。要开始一个 telnet会话,必须输入用户名和密码来登录服务器。Telnet是常用的远程控制Web服务器的方法。

但是,telnet因为采用明文传送报文,安全性不好,很多Linux服务器都不开放telnet服务,而改用更安全的ssh方式了。但仍然有很多别的系统可能采用了telnet方式来提供远程登录,因此弄清楚telnet客户端的使用方式仍是很有必要的。

telnet命令还可做别的用途,比如确定远程服务的状态,比如确定远程服务器的某个端口是否能访问。

**1.**命令格式:

telnet[参数][主机]

**2.**命令功能:

执行telnet指令开启终端机阶段作业,并登入远端主机。

**3.**命令参数:

-8 允许使用8位字符资料,包括输入与输出。

-a 尝试自动登入远端系统。

-b<主机别名> 使用别名指定远端主机名称。

-c 不读取用户专属目录里的.telnetrc文件。

-d 启动排错模式。

-e<脱离字符> 设置脱离字符。

-E 滤除脱离字符。

-f 此参数的效果和指定"-F"参数相同。

-F 使用Kerberos V5认证时,加上此参数可把本地主机的认证数据上传到远端主机。

-k<域名> 使用Kerberos认证时,加上此参数让远端主机采用指定的领域名,而非该主机的域名。

-K 不自动登入远端主机。

-l<用户名称> 指定要登入远端主机的用户名称。

-L 允许输出8位字符资料。

-n<记录文件> 指定文件记录相关信息。

-r 使用类似rlogin指令的用户界面。

-S<服务类型> 设置telnet连线所需的IP TOS信息。

-x 假设主机有支持数据加密的功能,就使用它。

-X<认证形态> 关闭指定的认证形态。


telnet命令使用示例

实例1:远程服务器无法访问

命令:

telnet 192.168.120.206

输出:

[root@localhost ~]# telnet 192.168.120.209
Trying 192.168.120.209...
telnet: connect to address 192.168.120.209: No route to host
telnet: Unable to connect to remote host: No route to host
[root@localhost ~]# 

说明:

处理这种情况方法:

(1)确认ip地址是否正确?

(2)确认ip地址对应的主机是否已经开机?

(3)如果主机已经启动,确认路由设置是否设置正确?(使用route命令查看)

(4)如果主机已经启动,确认主机上是否开启了telnet服务?(使用netstat命令查看,TCP的23端口是否有LISTEN状态的行)

(5)如果主机已经启动telnet服务,确认防火墙是否放开了23端口的访问?(使用iptables-save查看)

实例2:域名无法解析

命令:

telnet www.baidu.com

输出:

[root@localhost ~]# telnet www.baidu.com
www.baidu.com/telnet: Temporary failure in name resolution
[root@localhost ~]# 

说明:

处理这种情况方法:

(1)确认域名是否正确

(2)确认本机的域名解析有关的设置是否正确(/etc/resolv.conf中nameserver的设置是否正确,如果没有,可以使用nameserver 8.8.8.8)

(3)确认防火墙是否放开了UDP53端口的访问(DNS使用UDP协议,端口53,使用iptables-save查看)

实例3:

命令:

输出:

[root@localhost ~]# telnet 192.168.120.206
Trying 192.168.120.206...
telnet: connect to address 192.168.120.206: Connection refused
telnet: Unable to connect to remote host: Connection refused
[root@localhost ~]#

说明:

处理这种情况:

(1)确认ip地址或者主机名是否正确?

(2)确认端口是否正确,是否默认的23端口

实例4:启动telnet服务

命令:

service xinetd restart

输出:

[root@localhost ~]# cd /etc/xinetd.d/
[root@localhost xinetd.d]# ll
总计 124
-rw-r--r-- 1 root root 1157 2011-05-31 chargen-dgram
-rw-r--r-- 1 root root 1159 2011-05-31 chargen-stream
-rw-r--r-- 1 root root 523 2009-09-04 cvs
-rw-r--r-- 1 root root 1157 2011-05-31 daytime-dgram
-rw-r--r-- 1 root root 1159 2011-05-31 daytime-stream
-rw-r--r-- 1 root root 1157 2011-05-31 discard-dgram
-rw-r--r-- 1 root root 1159 2011-05-31 discard-stream
-rw-r--r-- 1 root root 1148 2011-05-31 echo-dgram
-rw-r--r-- 1 root root 1150 2011-05-31 echo-stream
-rw-r--r-- 1 root root 323 2004-09-09 eklogin
-rw-r--r-- 1 root root 347 2005-09-06 ekrb5-telnet
-rw-r--r-- 1 root root 326 2004-09-09 gssftp
-rw-r--r-- 1 root root 310 2004-09-09 klogin
-rw-r--r-- 1 root root 323 2004-09-09 krb5-telnet
-rw-r--r-- 1 root root 308 2004-09-09 kshell
-rw-r--r-- 1 root root 317 2004-09-09 rsync
-rw-r--r-- 1 root root 1212 2011-05-31 tcpmux-server
-rw-r--r-- 1 root root 1149 2011-05-31 time-dgram
-rw-r--r-- 1 root root 1150 2011-05-31 time-stream
[root@localhost xinetd.d]# cat krb5-telnet 
\# default: off
\# description: The kerberized telnet server accepts normal telnet sessions, \
\#       but can also use Kerberos 5 authentication.
service telnet
{
    flags      = REUSE
    socket_type   = stream    
    wait      = no
    user      = root
    server     = /usr/kerberos/sbin/telnetd
    log_on_failure += USERID
    disable     = yes
}
[root@localhost xinetd.d]# 

说明:

配置参数**,通常的配置如下:

service telnet

{

disable = no #启用

flags = REUSE #socket可重用

socket_type = stream #连接方式为TCP

wait = no #为每个请求启动一个进程

user = root #启动服务的用户为root

server = /usr/sbin/in.telnetd #要激活的进程

log_on_failure += USERID #登录失败时记录登录用户名

}

如果要配置允许登录的客户端列表,加入

only_from = 192.168.0.2 #只允许192.168.0.2登录

如果要配置禁止登录的客户端列表,加入

no_access = 192.168.0.{2,3,4} #禁止192.168.0.2、192.168.0.3、192.168.0.4登录

如果要设置开放时段,加入

access_times = 9:00-12:00 13:00-17:00 # 每天只有这两个时段开放服务(我们的上班时间:P)

如果你有两个IP地址,一个是私网的IP地址如192.168.0.2,一个是公网的IP地址如218.75.74.83,如果你希望用户只能从私网来登录telnet服务,那么加入

bind = 192.168.0.2

各配置项具体的含义和语法可参考xined配置文件属性说明(man xinetd.conf)

配置端口,修改services文件:

# vi /etc/services

找到以下两句

telnet 23/tcp

telnet 23/udp

如果前面有#字符,就去掉它。telnet的默认端口是23,这个端口也是黑客端口扫描的主要对象,因此最好将这个端口修改掉,修改的方法很简单,就是将23这个数字修改掉,改成大一点的数字,比如61123。注意,1024以下的端口号是internet保留的端口号,因此最好不要用,还应该注意不要与其它服务的端口冲突。

启动服务:

service xinetd restart 

实例5:正常telnet

命令:

telnet 192.168.120.204

输出:

[root@andy ~]# telnet 192.168.120.204
Trying 192.168.120.204...
Connected to 192.168.120.204 (192.168.120.204).
Escape character is '^]'.

  localhost (Linux release 2.6.18-274.18.1.el5 #1 SMP Thu Feb 9 12:45:44 EST 2012) (1)

login: root
Password: 
Login incorrect

说明:

一般情况下不允许root从远程登录,可以先用普通账号登录,然后再用su -切到root用户。

rcp命令语法

rcp代表“remote file copy”(远程文件拷贝)。该命令用于在计算机之间拷贝文件。rcp命令有两种格式。第一种格式用于文件到文件的拷贝;第二种格式用于把文件或目录拷贝到另一个目录中。

**1.**命令格式:

rcp [参数] [源文件] [目标文件]

**2.**命令功能:

rcp命令用在远端复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到该目录中。

**3.**命令参数:

各选项含义:

-r 递归地把源目录中的所有内容拷贝到目的目录中。要使用这个选项,目的必须是一个目录。

-p 试图保留源文件的修改时间和模式,忽略umask。

-k 请求rcp获得在指定区域内的远程主机的Kerberos 许可,而不是获得由krb_relmofhost⑶确定的远程主机区域内的远程主机的Kerberos许可。

-x 为传送的所有数据打开DES加密。这会影响响应时间和CPU利用率,但是可以提高安全性。如果在文件名中指定的路径不是完整的路径名,那么这个路径被解释为相对远程机上同名用户的主目录。如果没有给出远程用户名,就使用当前用户名。如果远程机上的路径包含特殊shell字符,需要用反斜线(\)、双引号(”)或单引号(’)括起来,使所有的shell元字符都能被远程地解释。需要说明的是,rcp不提示输入口令,它通过rsh命令来执行拷贝。

directory 每个文件或目录参数既可以是远程文件名也可以是本地文件名。远程文件名具有如下形式:rname@rhost:path,其中rname是远程用户名,rhost是远程计算机名,path是这个文件的路径。

**4.**使用实例:

要使用 rcp,需要具备以下条件:

如果系统中有 /etc/hosts 文件,系统管理员应确保该文件包含要与之进行通信的远程主机的项。

/etc/hosts 文件中有一行文字,其中包含每个远程系统的以下信息:

internet_address official_name alias

例如:

9.186.10.*** webserver1.com.58.webserver

.rhosts 文件

.rhosts 文件位于远程系统的主目录下,其中包含本地系统的名称和本地登录名。

例如,远程系统的 .rhosts 文件中的项可能是:

webserver1 root

其中,webserver1 是本地系统的名称,root 是本地登录名。这样,webserver1 上的 root 即可在包含 .rhosts 文件的远程系统中来回复制文件。

配置过程:

只对root用户生效

\1. 在双方root用户根目录下建立.rhosts文件,并将双方的hostname加进去.在此之前应在双方的 /etc/hosts文件中加入对方的IP和hostname

\2. 把rsh服务启动起来,redhat默认是不启动的。

方法:用执行ntsysv命令,在rsh选项前用空格键选中,确定退出。然后执行:

service xinetd restart即可。

\3. 到/etc/pam.d/目录下,把rsh文件中的auth required /lib/security/pam_securetty.so

一行用“#”注释掉即可。(只有注释掉这一行,才能用root用户登录)

rcp命令使用示例

将文件复制到远程系统

要将本地系统中的文件复制到远程系统,请使用以下命令:

rcplocal_fileremote_hostname:remote_fileEnter

注意,如果当前目录下没有 local_file,则除本地文件名外,还需要提供相对路径(自当前目录开始)或绝对路径名(自 / 开始)。

仅当希望将 remote_hostname 上的 remote_file 放到其他目录(远程主目录除外)下时,才需要为其指定完整的(绝对)路径。

使用实例1:将当前目录下的 test1 复制到名为 webserver1的远程系统

命令

rcp test1 webserver1:/home/root/test3

说明:

在这种情况下,test1 被复制到远程子目录 test3下,名称仍为 test1 。如果仅提供了远程主机名,rcp 将把 test1 复制到远程主目录下,名称仍为 test1 。

还可以在目的目录中包含文件名。例如,将文件复制到名为 webserver1的系统中:

rcp test1 webserver1:/home/root/test3

在这种情况下,将 test1 复制到远程目录root 下并将其命名为 test3。

使用实例2:从远程系统复制文件:要将远程系统中的文件复制到本地目录下

命令:

rcp remote_hostname:remote_file local_fileEnter

使用实例:3:将远程系统 webserver1中的 test2复制到当前目录:

命令:

rcp webserver1:/home/root/test2 .Enter

说明:

点 (.) 是“当前目录”的简写形式。在这种情况下,远程目录中的 test2 被复制到当前目录下,名称仍为 test2 。

如果希望用新名称复制文件,请提供目标文件名。

如果希望将 test2 复制到本地系统中的其他目录下,请使用以下绝对或相对路径名:

rcp webserver1:/home/root/test2 otherdir/ Enter

或者,如果希望用其他文件名将文件复制到其他目录下:

rcp webserver1:/home/root/test2 otherdir/otherfile Enter

使用实例4:将目录复制到远程系统:要将本地目录及其文件和子目录复制到远程系统,请同时使用 rcp 和 -r(递归)选项。

命令

rcp –r local_dir remote_hostname:remote_dir Enter

说明:

如果当前目录下没有 local_dir,则除本地目录名外,还需要提供相对路径名(自当前目录开始)或绝对路径名(自 / 顶级目录开始)。另外,如果主目录下没有 remote_dir,则 remote_dir 将需要一个相对路径(自主目录开始)或绝对路径(自 / 开始)。

使用实例5:

要将名为 work 的子目录完整地复制到 webserver1远程计算机中的主目录下名为 products 的目录,请键入以下内容:

rcp –r work webserver1:/home/root/products Enter

此命令在 webserver1:/home/root/products 下创建名为 work 的目录及其全部内容(假定 /home/root/products 已存在于 webserver1中)。

本示例假定用户处于包含 work 的本地目录下。否则,必须提供该目录的相对或绝对路径,如 /home/root/work。

使用实例6:从远程系统复制目录:

要将远程目录及其所有文件和子目录复制到本地目录,请在以下语法中使用 rcp 和 -r(递归)选项。

命令:

rcp –r remote_hostname:remote_dir local_dir Enter

要将名为 work 的远程目录复制到当前目录,请键入以下内容:

rcp –r webserver1:/home/root/work .Enter

点 (.) 表示当前目录。将在此目录下创建 work 目录。

scp命令语法

scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的。可能会稍微影响一下速度。当你服务器硬盘变为只读 read only system时,用scp可以帮你把文件移出来。另外,scp还非常不占资源,不会提高多少系统负荷,在这一点上,rsync就远远不及它了。虽然 rsync比scp会快一点,但当小文件众多的情况下,rsync会导致硬盘I/O非常高,而scp基本不影响系统正常使用。

**1.**命令格式:

scp [参数] [原路径] [目标路径]

**2.**命令功能:

scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令。linux的scp命令可以在linux服务器之间复制文件和目录。

**3.**命令参数:

-1 强制scp命令使用协议ssh1 

-2 强制scp命令使用协议ssh2 

-4 强制scp命令只使用IPv4寻址 

-6 强制scp命令只使用IPv6寻址 

-B 使用批处理模式(传输过程中不询问传输口令或短语) 

-C 允许压缩。(将-C标志传递给ssh,从而打开压缩功能) 

-p 保留原文件的修改时间,访问时间和访问权限。 

-q 不显示传输进度条。 

-r 递归复制整个目录。 

-v 详细方式显示输出。scp和ssh(1)会显示出整个过程的调试信息。这些信息用于调试连接,验证和配置问题。  

-c cipher 以cipher将数据传输进行加密,这个选项将直接传递给ssh。  

-F ssh_config 指定一个替代的ssh配置文件,此参数直接传递给ssh。 

-i identity_file 从指定文件中读取传输时使用的密钥文件,此参数直接传递给ssh。  

-l limit 限定用户所能使用的带宽,以Kbit/s为单位。   

-o ssh_option 如果习惯于使用ssh_config(5)中的参数传递方式,  

-P port 注意是大写的P, port是指定数据传输用到的端口号  

-S program 指定加密传输时所使用的程序。此程序必须能够理解ssh(1)的选项。

scp命令使用示例

scp命令的实际应用概述:

从本地服务器复制到远程服务器

(1) 复制文件:

命令格式:

scp local_file remote_username@remote_ip:remote_folder

或者

scp local_file remote_username@remote_ip:remote_file

或者

scp local_file remote_ip:remote_folder

或者

scp local_file remote_ip:remote_file

第1,2个指定了用户名,命令执行后需要输入用户密码,第1个仅指定了远程的目录,文件名字不变,第2个指定了文件名

第3,4个没有指定用户名,命令执行后需要输入用户名和密码,第3个仅指定了远程的目录,文件名字不变,第4个指定了文件名

(2) 复制目录:

命令格式:

scp -r local_folder remote_username@remote_ip:remote_folder

或者

scp -r local_folder remote_ip:remote_folder

第1个指定了用户名,命令执行后需要输入用户密码;

第2个没有指定用户名,命令执行后需要输入用户名和密码;

从远程服务器复制到本地服务器

从远程复制到本地的scp命令与上面的命令雷同,只要将从本地复制到远程的命令后面2个参数互换顺序就行了。

**实例1:**从远处复制文件到本地目录

命令:

scp [email protected]:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/

输出:

[root@localhost ~]# cd /opt/soft/
[root@localhost soft]# ll
总计 80072
drwxr-xr-x 12 root root   4096 09-21 18:40 fms3.5
drwxr-xr-x 3 root root   4096 09-21 17:58 fms4.5
drwxr-xr-x 10 root root   4096 10-30 17:15 jdk1.6.0_16
drwxr-xr-x 10 root root   4096 09-17 19:27 jdk1.6.0_16.bak
-rwxr-xr-x 1 root root 81871260 2009-12-21 jdk-6u16-linux-x64.bin
drwxrwxrwx 2 root root   4096 09-21 01:16 mysql
drwxr-xr-x 3 root root   4096 09-21 18:40 setup_file
drwxr-xr-x 9 root root   4096 09-17 19:23 tomcat6.0.32
drwxr-xr-x 9 root root   4096 2012-08-14 tomcat_7.0
[root@localhost soft]# scp [email protected]:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/
[email protected]'s password: 
nginx-0.5.38.tar.gz                                        100% 479KB 478.7KB/s  00:00  
[root@localhost soft]# ll
总计 80556
drwxr-xr-x 12 root root   4096 09-21 18:40 fms3.5
drwxr-xr-x 3 root root   4096 09-21 17:58 fms4.5
drwxr-xr-x 10 root root   4096 10-30 17:15 jdk1.6.0_16
drwxr-xr-x 10 root root   4096 09-17 19:27 jdk1.6.0_16.bak
-rwxr-xr-x 1 root root 81871260 2009-12-21 jdk-6u16-linux-x64.bin
drwxrwxrwx 2 root root   4096 09-21 01:16 mysql
-rw-r--r-- 1 root root  490220 03-15 09:11 nginx-0.5.38.tar.gz
drwxr-xr-x 3 root root   4096 09-21 18:40 setup_file
drwxr-xr-x 9 root root   4096 09-17 19:23 tomcat6.0.32
drwxr-xr-x 9 root root   4096 2012-08-14 tomcat_7.0
[root@localhost soft]# 

说明:

从192.168.120.204机器上的/opt/soft/的目录中下载nginx-0.5.38.tar.gz 文件到本地/opt/soft/目录中

**实例2:**从远处复制到本地

命令:

scp -r [email protected]:/opt/soft/mongodb /opt/soft/

输出:

[root@localhost soft]# ll
总计 80556
drwxr-xr-x 12 root root   4096 09-21 18:40 fms3.5
drwxr-xr-x 3 root root   4096 09-21 17:58 fms4.5
drwxr-xr-x 10 root root   4096 10-30 17:15 jdk1.6.0_16
drwxr-xr-x 10 root root   4096 09-17 19:27 jdk1.6.0_16.bak
-rwxr-xr-x 1 root root 81871260 2009-12-21 jdk-6u16-linux-x64.bin
drwxrwxrwx 2 root root   4096 09-21 01:16 mysql
-rw-r--r-- 1 root root  490220 03-15 09:11 nginx-0.5.38.tar.gz
drwxr-xr-x 3 root root   4096 09-21 18:40 setup_file
drwxr-xr-x 9 root root   4096 09-17 19:23 tomcat6.0.32
drwxr-xr-x 9 root root   4096 2012-08-14 tomcat_7.0
[root@localhost soft]# scp -r [email protected]:/opt/soft/mongodb /opt/soft/
[email protected]'s password: 
mongodb-linux-i686-static-1.8.5.tgz                                100%  28MB 28.3MB/s  00:01  
README                                              100% 731   0.7KB/s  00:00  
THIRD-PARTY-NOTICES                                        100% 7866   7.7KB/s  00:00  
mongorestore                                           100% 7753KB  7.6MB/s  00:00  
mongod                                              100% 7760KB  7.6MB/s  00:01  
mongoexport                                            100% 7744KB  7.6MB/s  00:00  
bsondump                                             100% 7737KB  7.6MB/s  00:00  
mongofiles                                            100% 7748KB  7.6MB/s  00:01  
mongostat                                             100% 7808KB  7.6MB/s  00:00  
mongos                                              100% 5262KB  5.1MB/s  00:01  
mongo                                               100% 3707KB  3.6MB/s  00:00  
mongoimport                                            100% 7754KB  7.6MB/s  00:00  
mongodump                                             100% 7773KB  7.6MB/s  00:00  
GNU-AGPL-3.0                                           100%  34KB 33.7KB/s  00:00  
[root@localhost soft]# ll
总计 80560
drwxr-xr-x 12 root root   4096 09-21 18:40 fms3.5
drwxr-xr-x 3 root root   4096 09-21 17:58 fms4.5
drwxr-xr-x 10 root root   4096 10-30 17:15 jdk1.6.0_16
drwxr-xr-x 10 root root   4096 09-17 19:27 jdk1.6.0_16.bak
-rwxr-xr-x 1 root root 81871260 2009-12-21 jdk-6u16-linux-x64.bin
drwxr-xr-x 3 root root   4096 03-15 09:18 mongodb
drwxrwxrwx 2 root root   4096 09-21 01:16 mysql
-rw-r--r-- 1 root root  490220 03-15 09:11 nginx-0.5.38.tar.gz
drwxr-xr-x 3 root root   4096 09-21 18:40 setup_file
drwxr-xr-x 9 root root   4096 09-17 19:23 tomcat6.0.32
drwxr-xr-x 9 root root   4096 2012-08-14 tomcat_7.0
[root@localhost soft]# 

说明:

从192.168.120.204机器上的/opt/soft/中下载mongodb 目录到本地的/opt/soft/目录来。

**实例3:**上传本地文件到远程机器指定目录

命令:

scp /opt/soft/nginx-0.5.38.tar.gz [email protected]:/opt/soft/scptest

输出:

上传前目标机器的目标目录:
[root@localhost soft]# cd scptest/
[root@localhost scptest]# ll
总计 0
[root@localhost scptest]# ll

本地机器上传:
[root@localhost soft]# scp /opt/soft/nginx-0.5.38.tar.gz [email protected]:/opt/soft/scptest
[email protected]'s password: 
nginx-0.5.38.tar.gz                                        100% 479KB 478.7KB/s  00:00  
[root@localhost soft]# 

上传后目标机器的目标目录:
[root@localhost scptest]# ll
总计 484
-rw-r--r-- 1 root root 490220 03-15 09:25 nginx-0.5.38.tar.gz
[root@localhost scptest]#

说明:

复制本地opt/soft/目录下的文件nginx-0.5.38.tar.gz 到远程机器192.168.120.204的opt/soft/scptest目录

**实例4:**上传本地目录到远程机器指定目录

命令:

scp -r /opt/soft/mongodb [email protected]:/opt/soft/scptest

输出:

上传前目标机器的目标目录:
[root@localhost ~]# cd /opt/soft/scptest/
[root@localhost scptest]# ll
总计 484
-rw-r--r-- 1 root root 490220 03-15 09:25 nginx-0.5.38.tar.gz
[root@localhost scptest]# 
本地机器上传:
[root@localhost ~]# scp -r /opt/soft/mongodb [email protected]:/opt/soft/scptest
[email protected]'s password: 
mongodb-linux-i686-static-1.8.5.tgz                                100%  28MB 28.3MB/s  00:01  
README                                              100% 731   0.7KB/s  00:00  
THIRD-PARTY-NOTICES                                        100% 7866   7.7KB/s  00:00  
mongorestore                                           100% 7753KB  7.6MB/s  00:00  
mongod                                              100% 7760KB  7.6MB/s  00:01  
mongoexport                                            100% 7744KB  7.6MB/s  00:00  
bsondump                                             100% 7737KB  7.6MB/s  00:00  
mongofiles                                            100% 7748KB  7.6MB/s  00:00  
mongostat                                             100% 7808KB  7.6MB/s  00:01  
mongos                                              100% 5262KB  5.1MB/s  00:00  
mongo                                               100% 3707KB  3.6MB/s  00:00  
mongoimport                                            100% 7754KB  7.6MB/s  00:01  
mongodump                                             100% 7773KB  7.6MB/s  00:00  
GNU-AGPL-3.0                                           100%  34KB 33.7KB/s  00:00  
[root@localhost ~]# 

上传后目标机器的目标目录:
[root@localhost scptest]# ll
总计 488
drwxr-xr-x 3 root root  4096 03-15 09:33 mongodb
-rw-r--r-- 1 root root 490220 03-15 09:25 nginx-0.5.38.tar.gz
[root@localhost scptest]# 

说明:

​ 上传本地目录 /opt/soft/mongodb到远程机器192.168.120.204上/opt/soft/scptest的目录中去

猜你喜欢

转载自blog.csdn.net/qq_41854911/article/details/122502664