LINUX核心命令实战总结十一——网络管理命令

1.1命令ifconfig:配置或显示网络接口信息

【功能说明】

命令ifconfig用于配置网卡IP地址等,网络参数或显示当前网络的接口状态,其类似于Windowsipconfig命令

如果没有ifconfig命令,用 yum -y install net-tools

【语法格式】

ifconfig interface  options
ifconfig 网络接口       选项

【选项说明】

参数选项

解释说明(带*的为重点)

-a

显示所有的网络接口信息,包括活动的非活动的

up

激活指定的网络接口(*

down

关闭指定网络接口(*

hw

设置网络接口的物理地址(MAC地址)

【使用范例】

显示所有接口信息

eth0      Link encap:Ethernet  HWaddr 00:50:56:AD:90:11  
          inet addr:10.28.11.235  Bcast:10.28.11.255  Mask:255.255.255.224
          inet6 addr: fe80::250:56ff:fead:9011/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:24123 errors:0 dropped:0 overruns:0 frame:0
          TX packets:31427 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:12210843 (11.6 MiB)  TX bytes:2444213 (2.3 MiB)

eth1      Link encap:Ethernet  HWaddr 00:50:56:AD:43:68  
          inet addr:10.16.50.243  Bcast:10.16.50.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fead:4368/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2756613 errors:0 dropped:0 overruns:0 frame:0
          TX packets:58731 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:185789473 (177.1 MiB)  TX bytes:8478380 (8.0 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:14 errors:0 dropped:0 overruns:0 frame:0
          TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:938 (938.0 b)  TX bytes:938 (938.0 b)
View Code

显示指定网卡的信息

[root@A ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:50:56:AD:90:11  
          inet addr:10.28.11.235  Bcast:10.28.11.255  Mask:255.255.255.224
          inet6 addr: fe80::250:56ff:fead:9011/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:24127 errors:0 dropped:0 overruns:0 frame:0
          TX packets:31434 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:12211199 (11.6 MiB)  TX bytes:2444761 (2.3 MiB)
启动/关闭网卡
[root@A ~]# ifconfig eth0 down
[root@A ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:50:56:AD:90:11  
          inet addr:10.28.11.235  Bcast:10.28.11.255  Mask:255.255.255.224
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:24135 errors:0 dropped:0 overruns:0 frame:0
          TX packets:31452 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:12212199 (11.6 MiB)  TX bytes:2446347 (2.3 MiB)

[root@A ~]# ifconfig eth0 up
[root@A ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:50:56:AD:90:11  
          inet addr:10.28.11.235  Bcast:10.28.11.255  Mask:255.255.255.224
          inet6 addr: fe80::250:56ff:fead:9011/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:60 (60.0 b)  TX bytes:258 (258.0 b)

为网卡配置IP地址(配置文件不改变,重启失效)

[root@A ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:50:56:AD:90:11  
          inet addr:10.28.11.235  Bcast:10.28.11.255  Mask:255.255.255.224
          inet6 addr: fe80::250:56ff:fead:9011/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:120 (120.0 b)  TX bytes:508 (508.0 b)

[root@A ~]# ifconfig eth0 192.168.1.4
[root@A ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:50:56:AD:90:11  
          inet addr:192.168.1.4  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fead:9011/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:120 (120.0 b)  TX bytes:508 (508.0 b)
[root@A ~]# cat eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=10.28.11.235
NETMASK=255.255.255.224
6INIT=no
DNS1=202.1GATEWAY=10.28.11.225
IPV01.224.69
DNS2=219.141.136.10
USERCTL=no
View Code

配置别名IP实际上就是为一个网卡配置多个IP

[root@A ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:50:56:AD:90:11  
          inet addr:192.168.1.4  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fead:9011/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:109 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:20276 (19.8 KiB)  TX bytes:508 (508.0 b)

[root@A ~]# ifconfig eth0:0 192.168.1.5 netmask 255.255.255.0 up
[root@A ~]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:50:56:AD:90:11  
          inet addr:192.168.1.4  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fead:9011/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:109 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:20276 (19.8 KiB)  TX bytes:508 (508.0 b)

eth0:0    Link encap:Ethernet  HWaddr 00:50:56:AD:90:11  
          inet addr:192.168.1.5  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

修改网卡MAC地址的例子

[root@A ~]# ifconfig eth0 hw ether 00:AA:bb:cc:dd:ee
[root@A ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:AA:BB:CC:DD:EE  
          inet addr:192.168.1.4  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fead:9011/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:113 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:21324 (20.8 KiB)  TX bytes:508 (508.0 b)

1.2 命令ifup:激活网络接口

【功能说明】

命令ifup用于激活指定网络接口,ifup其实是一个shell脚本,可以用which命令来找到这个脚本并读一读。

【语法格式】

ifup IFACE
ifup 网络接口

【使用范例】

[root@A ~]# ifup eth0  
Determining if ip address 10.28.11.235 is already in use for device eth0...

1.3 命令ifdown:禁用网络接口

【功能说明】

命令ifdown用于禁用指定的网络接口。和ifup命令相同。

【语法格式】

ifdown IFACE
ifdown 网络接口

【使用范例】

[root@A ~]# ifdown eth0
[root@A ~]# ifconfig
eth1      Link encap:Ethernet  HWaddr 00:50:56:AD:43:68  
          inet addr:10.16.50.243  Bcast:10.16.50.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fead:4368/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:124620 errors:0 dropped:0 overruns:0 frame:0
          TX packets:737 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:8404832 (8.0 MiB)  TX bytes:84777 (82.7 KiB)

1.4 命令route:显示或管理路由表

【功能说明】

命令route可以显示或管理linux系统的路由表,route命令设置的路由主要是静态路由。

【语法格式】

route [option]
route [选项]

【选项说明】

命令参数route选项及说明

参数选项

解释说明(带*的为重点)

-n

直接使用IP地址,不进行DNS解析主机名(*

-ee

显示更详细的路由信息

add

添加路由信息(*

del

删除路由信息

target

指定目标网络或主机。可以用IP地址或主机/网络名

-net

到一个网络的路由,后面接的是一个网络号地址(*

-host

到一个主机路由,后面接的是一个主机地址(*

Netmask NM

为添加的路由指定网络掩码(*

gw GW

为发往目标网络/主机的任何分组指定网关(*

Dev if

指定由那个网络设备出去,后面接网络设备名,如eth0等(*

【使用范例】

查看当前系统路由

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.16.50.0      *               255.255.255.0   U     0      0        0 eth1

删除和添加默认网关

[root@bachup ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.28.11.224    0.0.0.0         255.255.255.224 U     0      0        0 eth0
10.16.50.0      0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
0.0.0.0         10.28.11.225    0.0.0.0         UG    0      0        0 eth0
[root@bachup ~]# route del default gw 10.28.11.225
[root@bachup ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.28.11.224    *               255.255.255.224 U     0      0        0 eth0
10.16.50.0      *               255.255.255.0   U     0      0        0 eth1
link-local      *               255.255.0.0     U     1002   0        0 eth0
link-local      *               255.255.0.0     U     1003   0        0 eth1
[root@bachup ~]# route add default gw 10.28.11.225
[root@bachup ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.28.11.224    0.0.0.0         255.255.255.224 U     0      0        0 eth0
10.16.50.0      0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
0.0.0.0         10.28.11.225    0.0.0.0         UG    0      0        0 eth0

1.5 命令arp:管理系统的arp缓存

【功能说明】

命令arp命令用于操作本机的arp缓冲区,它可以显示arp缓存区中的所有条目,删除指定的条目或者添加静态的IP地址与MAC地址的对应关系。

 什么是arp?即地址解析协议,其主要功能是根据IP地址获取物理地址(MAC地址)

【语法格式】

arp [option]
arp       [选项]

【选项说明】

参数选项

解释说明(带*的为重点)

-n

显示数字IP地址(*

-s<主机><MAC地址>

设置指定主机的IP地址MAC地址的静态映射(*

-d<主机>

arp缓存区中删除指定主机的arp条目(*

-i<接口>

指定网络接口

     -v

显示详细的arp缓存区条目,包括缓冲区条目的统计信息

【使用范例】

显示arp缓存区的所有条目

[root@web02 ~]# arp
Address                  HWtype  HWaddress           Flags Mask            Iface
10.28.11.225             ether   3c:8c:40:02:1a:84   C                     eth0
10.16.50.4               ether   00:50:56:ad:59:ef   C                     eth1
[root@web02 ~]# arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
10.28.11.225             ether   3c:8c:40:02:1a:84   C                     eth0
10.16.50.4               ether   00:50:56:ad:59:ef   C                     eth1

查询指定主机的arp条目

[root@web02 ~]# arp 10.16.50.4
Address                  HWtype  HWaddress           Flags Mask            Iface
10.16.50.4               ether   00:50:56:ad:59:ef   C                     eth1

静态绑定Ip地址与MAC地址

[root@web02 ~]# arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
10.28.11.239             ether   00:50:56:ad:03:da   CM                    eth0
10.28.11.235             ether   00:50:56:ad:03:dc   CM                    eth0
10.16.50.4               ether   00:50:56:ad:59:ef   C                     eth1
[root@web02 ~]# arp -d 10.28.11.239
[root@web02 ~]# arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
10.28.11.239                     (incomplete)                              eth0
10.28.11.225             ether   3c:8c:40:02:1a:84   C                     eth0
10.28.11.235             ether   00:50:56:ad:03:dc   CM                    eth0
10.16.50.4               ether   00:50:56:ad:59:ef   C                     eth1

1.6 命令IP:网络配置工具

【功能说明】

命令ipiproute软件包中的一个强大的网络配置工具,用于显示或管理linux系统的路由,网络设备、策略路由和隧道。

【语法格式】

ip [ OPTIONS ] OBJECT COMMAND
ip [ 选项 ] 网络对象 操作命令

【选项说明】

参数选项

解释说明(带*的为重点)

-s

输出更详细的信息,为了显示更详细的信息,可重复使用此选项

-r

显示主机时,不使用IP地址,而是使用主机的域名

网络对象

指定要管理的网络对象

支持的网络对象如下:

link              网络设备

address          设备的协议地址(IP地址)

addrlabel        协议地址标签管理

neighbour        arpndisc缓存表      

route           路由表

rule            策略路由表

maddress      多播地址

mroute        多路由缓存表

tunnel         IP隧道

xfrem          IPsec协议框架

这里有一个有趣的用法,比如ip address可以简写为ip addr 或者最简化ip a

help

Ip help      查看 ip命令帮助

Ip  [object]  help 查看指定网络对象的帮助

    操作命令

对指定的网络对象完成的具体操作,通常,每一个具体操作的命令后面又有一组相关的命令选项。

不同的操作对象所支持的操作命令也不同。下面按照操作的网络对象给出所支持的常见操作命令。

link对象支持的操作命令:set(修改设备属性)、show(显示设备属性);

address对象支持的操作命令:add(添加协议地址)、del(删除协议地址)、flush(清除协议地址)、show(查看协议地址);

addrlabel对象支持的操作命令:adddellistflush

neighbour对象支持的操作命令:addchangereplacedeleteshowflush;

route对象支持的操作命令:addchangereplacedeleteshowflushget;

rule对象支持的操作命令:adddeleteflushshow;

maddress对象支持的操作命令:showadddelete

mroute对象支持操作命令:show

tunnel对象支持的操作命令:statepolicymonitor;

【使用范例】

显示网络设备属性

[root@web01 ~]# ip -s link show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    0          0        0       0       0       0      
    TX: bytes  packets  errors  dropped carrier collsns 
    466        7        0       0       0       0     

关闭和激活网络设备

[root@web01 ~]# ip link set eth0 down
[root@web01 ~]# ip link show dev eth0
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff
[root@web01 ~]# ip link set eth0 up
[root@web01 ~]# ip link show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff

修改网卡MAC地址

[root@web01 ~]# ip link show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff
[root@web01 ~]# ip link show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:aa:bb:cc:dd:ea brd ff:ff:ff:ff:ff:ff

查看网卡信息

[root@web01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:aa:bb:cc:dd:ea brd ff:ff:ff:ff:ff:ff
    inet 10.28.11.235/27 brd 10.28.11.255 scope global eth0
    inet6 fe80::2aa:bbff:fecc:ddee/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:50:56:ad:43:68 brd ff:ff:ff:ff:ff:ff
    inet 10.16.50.243/24 brd 10.16.50.255 scope global eth1
    inet6 fe80::250:56ff:fead:4368/64 scope link 
       valid_lft forever preferred_lft forever

添加或删除Ip地址

[root@web01 ~]# ip a show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:aa:bb:cc:dd:ea brd ff:ff:ff:ff:ff:ff
    inet 10.28.11.235/27 brd 10.28.11.255 scope global eth0
    inet6 fe80::2aa:bbff:fecc:ddee/64 scope link 
       valid_lft forever preferred_lft forever
[root@web01 ~]# ip link set eth0 up
[root@web01 ~]# ip a show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:aa:bb:cc:dd:ea brd ff:ff:ff:ff:ff:ff
    inet 10.28.11.235/27 brd 10.28.11.255 scope global eth0
    inet6 fe80::2aa:bbff:fecc:ddee/64 scope link 
       valid_lft forever preferred_lft forever
[root@web01 ~]# ip a show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:aa:bb:cc:dd:ea brd ff:ff:ff:ff:ff:ff
    inet 10.28.11.235/27 brd 10.28.11.255 scope global eth0
    inet6 fe80::2aa:bbff:fecc:ddee/64 scope link 
       valid_lft forever preferred_lft forever
[root@web01 ~]# ip link set eth0 up
[root@web01 ~]# ip a show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:aa:bb:cc:dd:ea brd ff:ff:ff:ff:ff:ff
    inet 10.28.11.235/27 brd 10.28.11.255 scope global eth0
    inet6 fe80::2aa:bbff:fecc:ddee/64 scope link 
       valid_lft forever preferred_lft forever
[root@web01 ~]# ip a add 192.168.1.13/24 dev eth0
[root@web01 ~]# ip a show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:aa:bb:cc:dd:ea brd ff:ff:ff:ff:ff:ff
    inet 10.28.11.235/27 brd 10.28.11.255 scope global eth0
    inet 192.168.1.13/24 scope global eth0
    inet6 fe80::2aa:bbff:fecc:ddee/64 scope link 
       valid_lft forever preferred_lft forever
[root@web01 ~]# ip a del 192.168.1.13/24 dev eth0
[root@web01 ~]# ip a show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:aa:bb:cc:dd:ea brd ff:ff:ff:ff:ff:ff
    inet 10.28.11.235/27 brd 10.28.11.255 scope global eth0
    inet6 fe80::2aa:bbff:fecc:ddee/64 scope link 
       valid_lft forever preferred_lft forever

如何用IP命令创建别名IP

[root@web01 ~]# ip a show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:aa:bb:cc:dd:ea brd ff:ff:ff:ff:ff:ff
    inet 10.28.11.235/27 brd 10.28.11.255 scope global eth0
    inet6 fe80::2aa:bbff:fecc:ddee/64 scope link 
       valid_lft forever preferred_lft forever
[root@web01 ~]# ip a add 10.0.0.20/31 dev eth0 label eth0:0
[root@web01 ~]# ip a show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:aa:bb:cc:dd:ea brd ff:ff:ff:ff:ff:ff
    inet 10.28.11.235/27 brd 10.28.11.255 scope global eth0
    inet 10.0.0.20/31 scope global eth0:0
    inet6 fe80::2aa:bbff:fecc:ddee/64 scope link 
       valid_lft forever preferred_lft forever

查看路由表

[root@web01 ~]# ip route
10.0.0.20/31 dev eth0  proto kernel  scope link  src 10.0.0.20 
10.28.11.224/27 dev eth0  proto kernel  scope link  src 10.28.11.235 
10.16.50.0/24 dev eth1  proto kernel  scope link  src 10.16.50.243 
[root@web01 ~]# ip route|column -t
10.0.0.20/31     dev  eth0  proto  kernel  scope  link  src  10.0.0.20
10.28.11.224/27  dev  eth0  proto  kernel  scope  link  src  10.28.11.235
10.16.50.0/24    dev  eth1  proto  kernel  scope  link  src  10.16.50.243
[root@web01 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.20       0.0.0.0         255.255.255.254 U     0      0        0 eth0
10.28.11.224    0.0.0.0         255.255.255.224 U     0      0        0 eth0
10.16.50.0      0.0.0.0         255.255.255.0   U     0      0        0 eth1

查看ARP缓存

[root@web01 ~]# ip neighbour
10.16.50.4 dev eth1 lladdr 00:50:56:ad:59:ef REACHABLE

添加或删除静态ARP

[root@web01 ~]# ip route add 10.1.0.0/24 via 10.0.0.253 dev eth0
RTNETLINK answers: No such process
[root@web01 ~]# ip neighbour
10.16.50.4 dev eth1 lladdr 00:50:56:ad:59:ef REACHABLE
[root@web01 ~]# ip neighbour add 192.168.1.100 lladdr 00:aa:bb:cc:dd:aa dev eth0
[root@web01 ~]# ip neighbour
192.168.1.100 dev eth0 lladdr 00:aa:bb:cc:dd:aa PERMANENT
10.16.50.4 dev eth1 lladdr 00:50:56:ad:59:ef REACHABLE
[root@web01 ~]# ip neighbour del 192.168.1.100 dev eth0
[root@web01 ~]# ip neighbour
10.16.50.4 dev eth1 lladdr 00:50:56:ad:59:ef REACHABLE

1.7 命令netstat:查看网络状态

【功能说明】

命令netstat用于本机网络的连接状态、运行端口和路由等信息

【语法格式】

ip [ OPTIONS ]
ip [ 选项 ] 

【选项说明】

命令netstat的参数选项及说明

参数选项

解释说明(带*的为重点)

-r

显示路由器信息,该功能类似于前面学过的routeip route

-g

显示多播功能群组成员,该功能类似于前面学过的ip maddr

-i

显示网络接口信息,该功能类似于前面学过的ip -s link

-s

显示各类协议的统计信息

-n

显示数字形式的地址而不是去解析主机、端口或用户名。默认情况下,netstat命令会尝试解析并显示主机的主机名,这个过程通常比较长也是非必需的(*

-a

显示处于监听状态和非监听状态的socket信息(*

-A

显示指定网络类型的网络连接状态

-c<秒数>

后面跟的秒数表示每隔几秒就刷新显示一次(*

-l

仅显示连接状态为”LISTEN”的服务的网络状态

-t

显示所有的TCP连接情况(*

-u

显示所有的UDP连接情况(*

-p

显示socket所属进程的PID和名称(*

【使用范例】

常用选项组合

[root@web01 ~]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 0.0.0.0:51921               0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:2049                0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:35206               0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:40615               0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:56650               0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:34283               0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:875                 0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
tcp        0      0 10.16.50.243:22             10.16.50.4:57310            ESTABLISHED 
tcp        0     52 10.16.50.243:22             10.16.50.4:57497            ESTABLISHED 

常用选项组合二

[root@web01 ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:51921               0.0.0.0:*                   LISTEN      1677/rpc.mountd     
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1744/sshd           
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      1511/cupsd          
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1848/master         
tcp        0      0 0.0.0.0:2049                0.0.0.0:*                   LISTEN      -                   
tcp        0      0 0.0.0.0:35206               0.0.0.0:*                   LISTEN      1677/rpc.mountd     
tcp        0      0 0.0.0.0:40615               0.0.0.0:*                   LISTEN      -                   
tcp        0      0 0.0.0.0:56650               0.0.0.0:*                   LISTEN      1376/rpc.statd      
tcp        0      0 0.0.0.0:34283               0.0.0.0:*                   LISTEN      1677/rpc.mountd     
tcp        0      0 0.0.0.0:875                 0.0.0.0:*                   LISTEN      1672/rpc.rquotad    
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1356/rpcbind        
tcp        0      0 :::37937                    :::*                        LISTEN      1376/rpc.statd      
tcp        0      0 :::50482                    :::*                        LISTEN      1677/rpc.mountd     
tcp        0      0 :::22                       :::*                        LISTEN      1744/sshd           
tcp        0      0 :::40118    
View Code

显示当前系统的路由表

[root@web01 ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
10.0.0.20       0.0.0.0         255.255.255.254 U         0 0          0 eth0
10.28.11.224    0.0.0.0         255.255.255.224 U         0 0          0 eth0
10.16.50.0      0.0.0.0         255.255.255.0   U         0 0          0 eth1

选项-i显示网络的接口状态

root@web01 ~]# 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        0      0      0      0        3      0      0      0 BMRU
eth0:0     1500   0      - no statistics available -                            BMRU
eth1       1500   0   318398      0      0      0     2471      0      0      0 BMRU
lo        65536   0      666      0      0      0      666      0      0      0 LRU

1.8命令ss:查看网络状态

命令ss是类似并将取代nestatde 工具,它能用来查看网络状态信息,包括TCPUDP连接、端口等。它的优点是能够显示更多更详细的有关网络连接状态的信息,而且比netstat更快速更高效。

如果系统没有ss命令,那就需要安装一下,ss命令属于iproute包,因此安装命令是 yum -y install iproute

【语法格式】

ss [options] [ FILTER ]

ss [options] [ FILTER ]

【选项说明】

参数选项

解释说明(带*的为重点)

-n

显示路由器信息,该功能类似于前面学过的routeip route

-r

显示多播功能群组成员,该功能类似于前面学过的ip maddr

-a

显示网络接口信息,该功能类似于前面学过的ip -s link

-l

显示各类协议的统计信息

-o

显示数字形式的地址而不是去解析主机、端口或用户名。默认情况下,netstat命令会尝试解析并显示主机的主机名,这个过程通常比较长也是非必需的(*

-e

显示处于监听状态和非监听状态的socket信息(*

-m

显示指定网络类型的网络连接状态

-p

后面跟的秒数表示每隔几秒就刷新显示一次(*

-i

仅显示连接状态为”LISTEN”的服务的网络状态

-s

显示所有的TCP连接情况(*

-4

显示所有的UDP连接情况(*

-6

显示socket所属进程的PID和名称(*

-0

仅显示PACKETsocket

-t

仅显示TCPsocket*

-u

仅显示UCPsocket

-d

仅显示DCCPsocket

-w

仅显示RAWsocket

-x

仅显示UNIXsocket

【使用案例】

常用选项组合

[root@web01 ~]# ss -an|column -t
State   Recv-Q  Send-Q  Local            Address:Port      Peer  Address:Port
LISTEN  0       128     *:51921          *:*
LISTEN  0       128     :::37937         :::*
LISTEN  0       128     :::50482         :::*
LISTEN  0       128     :::22            :::*
LISTEN  0       128     *:22             *:*
LISTEN  0       128     :::40118         :::*
LISTEN  0       128     :::42199         :::*
LISTEN  0       128     127.0.0.1:631    *:*

常用选项组合

[root@web01 ~]# ss -lntup|column -t
Netid  State   Recv-Q  Send-Q  Local          Address:Port  Peer                            Address:Port
udp    UNCONN  0       0       *:51766        *:*           users:(("rpc.statd",1376,7))
udp    UNCONN  0       0       *:34618        *:*
udp    UNCONN  0       0       *:34747        *:*           users:(("rpc.mountd",1677,7))
udp    UNCONN  0       0       127.0.0.1:708  *:*           users:(("rpc.statd",1376,10))
udp    UNCONN  0       0       *:875          *:*           users:(("rpc.rquotad",1672,3))
udp    UNCONN  0       0       *:111          *:*           users:(("rpcbind",1356,6))
udp    UNCONN  0       0       *:631          *:*           users:(("cupsd",1511,9))

显示socket统计

[root@web01 ~]# ss -s
Total: 252 (kernel 269)
TCP:   23 (estab 2, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 16

Transport Total     IP        IPv6
*         269       -         -        
RAW       0         0         0        
UDP       19        11        8        
TCP       23        13        10       
INET      42        24        18       
FRAG      0         0         0  

1.9 命令ping:测试主机之间网络的连通性

【功能说明】

命令ping可用于测试主机之间的连通性。执行ping命令会使用ICMP传输协议,发出要求回应的信息,若远端主机的网络功能没有问题,

【语法格式】

ping    [ option]  destination
ping    [ 选项]   目标主机

【选项说明】

命令参数ping选项及说明

参数选项

解释说明(带*的为重点)

-c<时间>

指定发送ICMP报文的次数,否则,ping命令将一直发送报文(*

-i<时间间隔>

相邻两次发送报文的时间间隔,默认时间间隔为1s

-n

不查询主机名,直接显示其IP地址

-q

只显示命令开始时的信息和运行结束的统计信息,忽略命运运行过程中的输出信息。

-s<数据包大小>

设置发送数据包的大小,默认大小为56字节,再加上8字节的ICMP头,一共是64字节的ICMP包。

-t<生存期>

设置发送的数据包,其生存期(TTL)的值

-w<截止时间>

超过截止时间,立即退出ping程序

-W<超时时间>

等待响应的超时时间

【使用范例】

[root@server ~]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39: icmp_seq=1 ttl=54 time=26.5 ms
64 bytes from 14.215.177.39: icmp_seq=2 ttl=54 time=26.8 ms
64 bytes from 14.215.177.39: icmp_seq=3 ttl=54 time=27.4 ms
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2399ms
rtt min/avg/max/mdev = 26.501/26.924/27.447/0.392 ms

网络故障时ping结果

[root@server ~]# ping 10.16.50.2
PING 10.16.50.2 (10.16.50.2) 56(84) bytes of data.
64 bytes from 10.16.50.2: icmp_seq=1 ttl=64 time=1.70 ms
64 bytes from 10.16.50.2: icmp_seq=2 ttl=64 time=0.446 ms
64 bytes from 10.16.50.2: icmp_seq=3 ttl=64 time=0.379 ms
^C
--- 10.16.50.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2409ms
rtt min/avg/max/mdev = 0.379/0.844/1.707/0.610 ms

使用ping组合不同参数

[root@server ~]# ping -c 3 -i 3 -s 1024 -t 255 baidu.com
PING baidu.com (123.125.114.144) 1024(1052) bytes of data.
1032 bytes from 123.125.114.144: icmp_seq=1 ttl=50 time=41.5 ms
1032 bytes from 123.125.114.144: icmp_seq=2 ttl=50 time=53.0 ms
1032 bytes from 123.125.114.144: icmp_seq=3 ttl=50 time=41.4 ms

--- baidu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 6048ms
rtt min/avg/max/mdev = 41.428/45.319/53.012/5.442 ms

1.10 命令traceroute:追踪数据传输路由状况

【功能说明】

Traceroute命令用于显示网络数据包传输到指定主机的路径信息,追踪数据传输路径。默认数据包大小是60字节或80字节,用户可另外设置。它与Windows下的trance 命令类似。

【语法格式】

 traceroute [option] [host] [packet_len]
 traceroute [选项] [主机名或IP] [数据包大小]

【选项说明】

命令traceroute参数选项及说明

参数选项

解释说明(带*的为重点)

-q<nqueries>

设置每一跳的探测包数量。默认是3

-I

使用ICMP ECHO探测,即ping

-n

直接使用IP地址而不使主机名

-m

设置最大的跳数,默认为0

【使用范例】

查看主机的路由状况

[root@server ~]# traceroute baidu.com
traceroute to baidu.com (123.125.114.144), 30 hops max, 60 byte packets
 1  10.28.11.225 (10.28.11.225)  1.664 ms  3.075 ms  2.376 ms
 2  10.16.1.13 (10.16.1.13)  0.972 ms 10.16.1.9 (10.16.1.9)  1.951 ms  0.974 ms
 3  10.16.1.1 (10.16.1.1)  0.374 ms 10.16.1.5 (10.16.1.5)  0.397 ms  0.398 ms
 4  117.21.237.89 (117.21.237.89)  1.595 ms  1.456 ms  1.319 ms
 5  117.41.250.33 (117.41.250.33)  0.590 ms  0.567 ms  0.512 ms
 6  202.97.75.201 (202.97.75.201)  30.644 ms  30.423 ms  30.473 ms
 7  * * 202.97.88.254 (202.97.88.254)  27.853 ms
 8  219.158.43.13 (219.158.43.13)  41.876 ms 219.158.40.181 (219.158.40.181)  47.605 ms *
 9  219.158.4.157 (219.158.4.157)  45.582 ms  45.498 ms  44.056 ms
10  61.49.214.2 (61.49.214.2)  45.302 ms  45.293 ms  46.038 ms
11  61.148.143.110 (61.148.143.110)  53.075 ms  43.295 ms  43.222 ms
12  123.125.248.126 (123.125.248.126)  69.875 ms 123.125.248.46 (123.125.248.46)  65.415 ms  55.757 ms
13  * * *
14  * * *
15  * * *
16  * * *
17  * * *
18  * * *
19  * * *
20  * * *
21  * * *
22  * * *
23  * * *
24  * * *
25  * * *
26  * * *
27  * * *
28  * * *
29  * * *
30  * * *
[root@server ~]# 
View Code

加快查询时间

[root@server ~]# traceroute  -I baidu.com
traceroute to baidu.com (123.125.114.144), 30 hops max, 60 byte packets
 1  10.28.11.225 (10.28.11.225)  1.223 ms  1.868 ms  2.611 ms
 2  10.16.1.13 (10.16.1.13)  1.268 ms  1.761 ms  2.350 ms
 3  10.16.1.5 (10.16.1.5)  0.345 ms  0.357 ms  0.391 ms
 4  117.21.237.89 (117.21.237.89)  1.243 ms  1.393 ms  1.477 ms
 5  117.41.250.33 (117.41.250.33)  0.825 ms  0.839 ms  0.846 ms
 6  202.97.75.201 (202.97.75.201)  29.487 ms  29.152 ms  29.110 ms
 7  * * *
 8  219.158.39.57 (219.158.39.57)  43.040 ms  41.844 ms  41.881 ms
 9  219.158.4.157 (219.158.4.157)  41.649 ms  41.655 ms  41.653 ms
10  61.49.214.2 (61.49.214.2)  42.166 ms  42.164 ms  42.071 ms
11  61.148.143.110 (61.148.143.110)  43.133 ms  91.175 ms  91.192 ms
12  123.125.248.46 (123.125.248.46)  165.501 ms  152.301 ms  152.258 ms
13  * * *
14  123.125.114.144 (123.125.114.144)  41.674 ms  41.276 ms  41.251 ms

1.11 命令arping:发送arp请求

命令arping是用于发送arp请求到一个相邻主机的工具,arping使用arp数据包查局域网内所有地址的硬件地址。

【语法格式】

arping    [ option]
arping    [ 选项]  

【选项说明】

命令arping的参数及说明

参数选项

解释说明(带*的为重点)

-c<次数>

发送指定次数的arp报文后退出命令(*

-f

当收到第一个应答报文时,立即退出命令,此选项用于判断目标主机是否存在或者正常运行(*

-I网络接口

指定网络接口发送arp报文

-w<截止时间>

设置命令的执行截止时间

-s source

设定arping发送的arp数据包中的源地址

【使用范例】

测试目标主机是否存在

[root@server ~]# arping -f 10.16.50.242
ARPING 10.16.50.242 from 10.28.11.233 eth0
Unicast reply from 10.16.50.242 [00:50:56:AD:86:81]  0.825ms
Sent 1 probes (1 broadcast(s))
Received 1 response(s)
[root@server ~]# arping -f 10.16.50.244
ARPING 10.16.50.244 from 10.28.11.233 eth0
Unicast reply from 10.16.50.244 [00:50:56:AD:03:DB]  0.881ms
Sent 1 probes (1 broadcast(s))
Received 1 response(s)
[root@server ~]# arping -f 10.16.50.245
ARPING 10.16.50.245 from 10.28.11.233 eth0
Unicast reply from 10.16.50.245 [00:50:56:AD:EF:5B]  0.899ms
Sent 1 probes (1 broadcast(s))
Received 1 response(s)

Linux负载均衡器停机切换时ARP缓存导致故障案例

Linux负载均衡器发生停机故障时,我们使用备用的设备接管时,因为所有用户以及客户端的arp缓存里对应的仍然是停机时的负载均衡器的IP,因此,切换完的新负载均衡器之后,短时间内用户访问可能依然不正常,此时就需要执行arping命令,让所有的客户端缓存失效,这也是高可用软件的做法。

[root@server ~]# arping  -c 1 -I eth0 -s 10.16.50.242 10.16.50.6

1.12 命令telnet:远程登录主机

【功能说明】

命令telnet以前是用来远程登录主机,对远程主机进行管理的。但是因为telnet是采用明文传送报文的,其安全不好,因此现在很多Linux登录都是不开放telnet服务,而是开放更安全的SSH服务了。当然,交换机等网络还是会采用telnet登录的方式。

现在使用telnet命令的场景主要是判断远端服务器端口是否开放。

【语法格式】

telnet [option]  [host] [port]
telnet [选项]  [主机名或IP] [端口]

【使用范例】

[root@server ~]# telnet 10.16.50.243
Trying 10.16.50.243...
telnet: connect to address 10.16.50.243: Connection refused
[root@server ~]# telnet 10.16.50.243 22
Trying 10.16.50.243...
Connected to 10.16.50.243.
Escape character is '^]'.
SSH-2.0-OpenSSH_5.3
^C^]
telnet> quit
Connection closed.

端口没有开访问

[root@server ~]# telnet 10.16.50.243 23
Trying 10.16.50.243...
telnet: connect to address 10.16.50.243: Connection refused

1.13 命令nc:多功能网络工具

【功能说明】

工具nc是一个简单、可靠、强大的网络工具,它可以建立TCP连接,发送UDP数据包,监听任意的TCPUDP端口,进行端口扫描,处理IPV4IPV6数据包。

如果系统没有nc命令,那么可以手动安装,安装命令为yum  -y install nc

【语法格式】

 nc [option]
 nc [选项]

【选项说明】

参数选项

解释说明(带*的为重点)

-l

指定监听端口,然后一直等待网络连接(*

-z

表示zero,表示扫描时不发送任何数据(*

-v

显示详细输出(*

-w

设置超时时间,对-l选项失效(*

-p

指定nc命令使用的端口,就不能和-l选项一起使用,可能引起错误

-u

使用UDP连接,默认是TCP连接

-s

指定发送数据的源IP地址,适用于多网卡主机

【使用案例】

利用nc扫描端口

[root@server ~]# nc -z 10.16.50.244 20-40
Connection to 10.16.50.244 22 port [tcp/ssh] succeeded!
[root@server ~]# nc -z 10.16.50.244 20-4000
Connection to 10.16.50.244 22 port [tcp/ssh] succeeded!
Connection to 10.16.50.244 111 port [tcp/sunrpc] succeeded!

1.14 命令ssh:安全地远程登录主机

【功能说明】

命令sshopenssh套件中的客户连接工具,可以使用ssh加密协议实现安全的远程登录服务器,实现对服务器的远程管理,windows中的替代工具为XshellputtysecureCRT.

【语法格式】

 ssh [option] [user@hostname] [command]
 ssh [选项] [用户@主机名或IP地址] [远程执行命令]

【选项说明】

参数选项

解释说明(带*的为重点)

-p

指定ssh登录端口,如果忽略则默认端口为22SSH服务器的默认端口)

-t

强制分配伪终端,可以在远程机器上执行任何全屏(screen-based)程序,所以非常有用,例如菜单服务。即使没有本地终端,多个-t选项也会强制分配终端。这个选项在进行写远程批量管理shell脚本时非常有用。

-v

调式模式(*

【使用范例】

[root@server ~]# ssh 10.16.50.243
The authenticity of host '10.16.50.243 (10.16.50.243)' can't be established.
RSA key fingerprint is 11:11:97:0e:f1:96:60:d5:96:6a:73:50:16:65:dc:5f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.16.50.243' (RSA) to the list of known hosts.
root@10.16.50.243's password: 
Last login: Fri Feb  2 11:23:44 2018 from 10.16.50.4
[root@web01 ~]# hostname -I
10.28.11.235 10.0.0.20 10.16.50.243

不使用root用户登录,明确指定登录用户,也可以同时指定端口

[root@server ~]# ssh -p 22 yhh@10.16.50.243
yhh@10.16.50.243's password: 
[yhh@web01 ~]$ 

SSH远程连接服务比较慢的问题排查方案

[root@server ~]# ssh -v root@10.16.50.243
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 10.16.50.243 [10.16.50.243] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/identity type -1
debug1: identity file /root/.ssh/identity-cert type -1
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host '10.16.50.243' is known and matches the RSA host key.
debug1: Found key in /root/.ssh/known_hosts:2
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/identity
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Next authentication method: password
root@10.16.50.243's password: 
debug1: Authentication succeeded (password).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = zh_CN.UTF-8
Last login: Sun Feb  4 17:37:11 2018 from 10.16.50.241
View Code

1.15 命令wget:命令行下载工具

命令wget用于从网络上下载一些资料,该命令对于能够连接互联网的Linux系统的作用非常大,可以直接从网上下载自己所需要的文件。

【语法格式】

wget [option] [url]
wget [选项] [下载地址] 

【选项说明】

命令wget的参数选项及说明

参数选项

解释说明(带*的为重点)

-o

将命令的执行结果写入文件中

-O

指定保存的文件名后下载文件(*

--limit-rate

限速下载(*

-b

转入后台执行命令

-c

断点续传(*

--user-agent

指定客户端标志

-q

关闭下载时的输出

--tries=number

设置重试的次数

--spider

模拟爬虫访问

-T seconds

--timeout=seconds

设置访问的超时时间

【使用案例】

使用wget下载单个文件(url失效请自行更换链接)

wget http://www.linuxde.net/testfile.zip

使用-O选项指定下载文件的保存文件名

wget -O wordpress.zip http://www.linuxde.net/download.aspx?id=1080

通过--limit-rate限速下载

wget --limit-rate=300k http://www.linuxde.net/testfile.zip

使用-c参数断点续传

wget -c http://www.linuxde.net/testfile.zip

使用wget -b后台下载文件

wget -b http://www.linuxde.net/testfile.zip

监控网站url是否正常的案例

[root@client ~]# wget -q -T 3 --tries=1 --spider www.baidu.com
[root@client ~]# echo $?
0
[root@client ~]# wget -q -T 3 --tries=1 --spider www.baiu.com 
[root@client ~]# echo $?
8

1.16 命令mails:显示邮件传输队列

【功能说明】

命令是maliqmail queue的缩写,它会显示待发送的邮件队列,显示的条目包括邮件队列ID、邮件大小、加入队列时间、邮件发送者和接受者。如果邮件进行最后一次尝试后还没有将邮件投递出去,则显示发送失败的原因。

【语法说明】

mailq [option]
mailq [选项]

【选项说明】

命令mailq的参数选项及说明

参数选项

解释说明(带*的为重点)

-v

调试模式,显示详细信息

【使用范例】

查看邮件队列

root@web01 ~]# mailq
Mail queue is empty
[root@web01 ~]# /etc/init.d/postfix start
[root@web01 ~]# mailq
Mail queue is empty
[root@web01 ~]# mailq -v
postqueue: dict_eval: const  mail
postqueue: dict_eval: const  all
postqueue: dict_eval: const  
postqueue: dict_eval: const  
postqueue: dict_eval: const  
postqueue: name_mask: all
postqueue: dict_eval: const  web01.localdomain
postqueue: dict_eval: const  localdomain
postqueue: dict_eval: const  Postfix
postqueue: dict_eval: expand ${multi_instance_name:postfix}${multi_instance_name?$multi_instance_name} -> postfix
postqueue: dict_eval: const  postfix
postqueue: dict_eval: const  postdrop
postqueue: dict_eval: expand $myhostname, localhost.$mydomain, localhost -> web01.localdomain, localhost.localdomain, localhos 

1.17 命令mail:发送和接收邮件

【功能说明】

命令mail是命令行的电子邮件发送和接收的工具。Mail命令是个软链接,真实的程序文件是mail

【语法格式】

mail [option]
mail [选项]

【选项说明】

命令mail的参数选项及说明

参数选项

解释说明(带*的为重点)

-s

指定邮件主题(*

-a

发送邮件附件,多个附件使用多次-a选项即可(*

-b

指定密件抄送的收信人地址

-c

指定抄送的收信人地址

【语法格式】

mail    -s 邮件主题 收件人1 收件人2 .....

【使用案例】

[root@server ~]# mail -s "hello world" 981654601@qq.com
ni hao 
i love you 
EFO
EOT
[root@server ~]# 

 使用管道传入内容并发送电子邮件

root@server ~]# echo -e"hello this is the content of mail "|mail -s "Hello from nanchan" 981654601@qq.com   
[root@server ~]#

 使用文件发送电子邮件

[root@server ~]# mail -s "Hello world1" 981654601@qq.com <aa

nslookup         [选项]         [域名/IP ]         [DNS服务器]

【选项说明】

直接输入nslookup命令,若不加任何参数,则会直接进入交换模式,此时nslookup会连接到默认的域名服务器

交互模式也支持选定不同的域名服务器,只需要将第一个参数设置为-”,然后第二个参数是设置连接的域名服务器主机名或IP地址。

交互模式下nslookup命令参数选项及说明

参数选项

解释说明(带*的为重点)

exit

退出nslookup命令

server<域名服务器>

指定解析域名的服务器地址(*

set关键字=

设置查询关键字(域名属性)的值。常见的关键字如下:(*

all(全部)查询域名有关的所有信息

domain=name 指定查询的域名

port=端口号   指定域名服务器使用的端口号

type= 类型名 指定域名查询的类型(例如:AHINFO,PTR,NS,MX等)

retry= <次数>指定查询时重试的次数

timeout= 秒数 指定查询的超时时间

非交互式下nslookup命令的参数选项及说明

参数选项

解释说明(带*的为重点)

-timeout

指定查询的超时时间

-query

指定域名查询类型

【使用范例】

交互模式

[root@web02 ~]# nslookup
> www.baidu.com
Server:         202.101.224.69
Address:        202.101.224.69#53

Non-authoritative answer:
www.baidu.com   canonical name = www.a.shifen.com.
Name:   www.a.shifen.com
Address: 14.215.177.39
Name:   www.a.shifen.com
Address: 14.215.177.38
> 

非交互式

[root@web02 ~]# nslookup https://mail.qq.com
Server:         202.101.224.69
Address:        202.101.224.69#53

Non-authoritative answer:
Name:   https://mail.qq.com
Address: 61.131.208.210

1.18命令dig:域名查询工具

【功能说明】

命令dig是常用的域名功能查询工具,可以用于测试域名系统的工作是否正常。

【语法格式】

 dig [option]
 dig           [选项]

【选项说明】

命令dig参数选项及说明

参数选项

解释说明(带*的为重点)

@<DNS服务器地址>

指定进行域名解析的域名服务器。当不希望使用本机磨人的DNS服务器设置时,使用此选项可指定进行域名解析的其他的域名服务器(*

-x

反向域名解析

-t

指定要查的DNS数据类型,如AMXPTR等。默认的查询类型为A*

-b

指定使用本机的那个IP地址向域名服务器发送域名查询请求

-p

指定域名服务器所使用的端口号。默认情况下,域名服务器使用UDP协议的53端口

+trace

从根域开始跟踪查询结果(*

+nocmd

不输出dig的版本信息

+short

仅输出最精简CNAME信息和A记录,其他的都不会输出

+nocomment

不输出dig的详情注释信息

+nostat

不输出最后的统计信息

【使用范例】

查询指定域名的IP地址

[root@web02 ~]# dig www.baidu.com

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.30.rc1.el6 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53627
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.baidu.com.                 IN      A

;; ANSWER SECTION:
www.baidu.com.          955     IN      CNAME   www.a.shifen.com.
www.a.shifen.com.       72      IN      A       14.215.177.39
www.a.shifen.com.       72      IN      A       14.215.177.38

;; Query time: 15 msec
;; SERVER: 202.101.224.69#53(202.101.224.69)
;; WHEN: Mon Feb  5 13:53:36 2018
;; MSG SIZE  rcvd: 90

反向解析例子

[root@web02 ~]# dig -x 61.131.208.210

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.30.rc1.el6 <<>> -x 61.131.208.210
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 33994
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0

;; QUESTION SECTION:
;210.208.131.61.in-addr.arpa.   IN      PTR

;; AUTHORITY SECTION:
131.61.in-addr.arpa.    25468   IN      SOA     ns.jxncptt.net.cn. root.jxncptt.net.cn. 2 10800 3600 604800 86400

;; Query time: 10 msec
;; SERVER: 202.101.224.69#53(202.101.224.69)
;; WHEN: Mon Feb  5 13:54:59 2018
;; MSG SIZE  rcvd: 103

查询MX类型的域名信息

[root@web02 ~]# dig -t MX baidu.com

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.30.rc1.el6 <<>> -t MX baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 63715
;; flags: qr rd ra cd; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;baidu.com.                     IN      MX

;; ANSWER SECTION:
baidu.com.              6420    IN      MX      10 mx.maillb.baidu.com.
baidu.com.              6420    IN      MX      15 mx.n.shifen.com.
baidu.com.              6420    IN      MX      20 mx1.baidu.com.
baidu.com.              6420    IN      MX      20 jpmx.baidu.com.
baidu.com.              6420    IN      MX      20 mx50.baidu.com.

;; Query time: 1 msec
;; SERVER: 202.101.224.69#53(202.101.224.69)
;; WHEN: Mon Feb  5 13:57:11 2018
;; MSG SIZE  rcvd: 143

显示完整的DNS解析过程

[root@web02 ~]# dig @61.131.208.210 https://mail.qq.com +trace

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.30.rc1.el6 <<>> @61.131.208.210 https://mail.qq.com +trace
; (1 server found)
;; global options: +cmd
;; connection timed out; no servers could be reached

精简输出例子

[root@web02 ~]# dig +nocmd +nocomment +nostat https://mail.qq.com
;https://mail.qq.com.           IN      A
https://mail.qq.com.    31      IN      A       61.131.208.210

1.19 命令host:域名查询工具

【功能说明】

命令host是用于查询DNS的工具,它可以将指定主机名称转换为IP地址。

【语法格式】

host     [option]
host               [选项]

【选项说明】

命令host的参数选项及说明

参数选项

解释说明(带*的为重点)

-a

显示详细的DNS信息

-t

指定查询的域名信息类型,可以是“A”、“All”、“MX”和“NS”等

【使用范例】

DNS查询

[root@web02 ~]# host www.baidu.com
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 14.215.177.38
www.a.shifen.com has address 14.215.177.39

查询详细信息

[root@web02 ~]# host -a www.baidu.com
Trying "www.baidu.com"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 28926
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.baidu.com.                 IN      ANY

;; ANSWER SECTION:
www.baidu.com.          250     IN      CNAME   www.a.shifen.com.

Received 58 bytes from 202.101.224.69#53 in 2 ms

指定DNS服务器查询

[root@web02 ~]# host -a ww.baidu.com 202.101.224.68
Trying "ww.baidu.com"
Using domain server:
Name: 202.101.224.68
Address: 202.101.224.68#53
Aliases: 

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23864
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;ww.baidu.com.                  IN      ANY

;; ANSWER SECTION:
ww.baidu.com.           4788    IN      CNAME   ps_other.a.shifen.com.

Received 62 bytes from 202.101.224.68#53 in 0 ms

按类进行查询

[root@web02 ~]# host -t MX baidu.com
baidu.com mail is handled by 20 mx1.baidu.com.
baidu.com mail is handled by 20 jpmx.baidu.com.
baidu.com mail is handled by 20 mx50.baidu.com.
baidu.com mail is handled by 10 mx.maillb.baidu.com.
baidu.com mail is handled by 15 mx.n.shifen.com.

1.20命令nmap:网络探测工具和安全/端口扫描器

【功能说明】

工具nmap是一款开放源代码的网络探测和安全审核工具,是Network Mapper的缩写。期设置目标是快速地扫描大型网络。Nmap可以发现网络上有哪些主机,主机提供了什么服务,并探测操作系统的类型及版本信息。

如果系统没有nmap命令,则可以使用下面的命令来安装。 yum -y install nmap

【语法格式】

nmap     [Scan TYpe] [option ] {target specification}
nmap [扫描类型...] [选项] {扫描目标说明}

【选项说明】

命令nmap的参数选项及说明

参数选项

解释说明(带*的为重点)

-sS

TCP同步扫描(TCP SYN)(*

-sT

TCP连接扫描

-sn

不进行端口扫描,只检查主机正在运行,该选项与老版本的-sP相同(*

-sU

扫描UDP端口

-sV

探测服务版本信息

-Pn

只进行扫描,不ping主机

-O

激活对TCP/IP指纹特征(fingerprinting)的扫描,默认80端口,也可以指定端口、格式为-PS22-PS22-25,80,113,1050,35000,记住PS和端口号之间不要有空格

-v

显示扫描过程中的详细信息(*

-S<IP>

设置扫描的IP地址

-g port

设置扫描的源端口

-oN

把扫描的结果重定向到文件中

-iL filename

从文中读取扫描的目标

-p<端口>

指定要扫描的端口,可以是一个单独的端口,也可以用逗号分隔开多个端口,或者使用“-

表示端口范围(*

-n

不进行DNS解析,加快扫描速度(*

--exclude

排除指定主机

--excludefile

排除指定文件中的主机

【使用范围】

查看主机当前开发的端口

[root@server ~]# nmap 10.16.50.2

Starting Nmap 5.51 ( http://nmap.org ) at 2018-02-05 15:24 CST
Nmap scan report for 10.16.50.2
Host is up (0.00021s latency).
Not shown: 985 closed ports
PORT      STATE SERVICE
21/tcp    open  ftp
80/tcp    open  http
135/tcp   open  msrpc
139/tcp   open  netbios-ssn
443/tcp   open  https
902/tcp   open  iss-realsecure
912/tcp   open  apex-mesh
3306/tcp  open  mysql
3389/tcp  open  ms-term-serv
5432/tcp  open  postgresql
49152/tcp open  unknown
49153/tcp open  unknown
49154/tcp open  unknown
49155/tcp open  unknown
49165/tcp open  unknown
MAC Address: 00:50:56:AD:A7:4D (VMware)

扫描主机的指定端口

[root@web02 ~]# nmap 10.27.2.88 -p 22

Starting Nmap 5.51 ( http://nmap.org ) at 2018-02-05 15:49 CST
Nmap scan report for 10.27.2.88
Host is up (0.00029s latency).
PORT   STATE SERVICE
22/tcp open  ssh

Nmap done: 1 IP address (1 host up) scanned in 0.17 seconds

扫描局域网内所有IP

[root@web02 ~]# nmap -p 22 10.27.2.0/24

Starting Nmap 5.51 ( http://nmap.org ) at 2018-02-05 15:43 CST
Nmap scan report for bogon (10.27.2.1)
Host is up (0.0020s latency).
PORT   STATE  SERVICE
22/tcp closed ssh

Nmap scan report for 10.27.2.12
Host is up (0.00027s latency).
PORT   STATE    SERVICE
22/tcp filtered ssh

Nmap scan report for bogon (10.27.2.13)
Host is up (0.0085s latency).
PORT   STATE    SERVICE
22/tcp filtered ssh

Nmap scan report for 10.27.2.16
Host is up (0.0010s latency).
PORT   STATE    SERVICE
22/tcp filtered ssh

探测目标主机的服务和操作系统的版本

[root@server ~]# nmap -O -sV 10.27.2.88

Starting Nmap 5.51 ( http://nmap.org ) at 2018-02-05 16:46 CST
Nmap scan report for 10.27.2.88
Host is up (0.00026s latency).
Not shown: 996 closed ports
PORT     STATE SERVICE    VERSION
22/tcp   open  ssh        OpenSSH 5.3 (protocol 2.0)
80/tcp   open  http       nginx
111/tcp  open  rpcbind
1521/tcp open  oracle-tns Oracle TNS Listener
1 service unrecognized despite returning data. If you know the service/version, please submit the following fingerprint at http://www.insecure.org/cgi-bin/servicefp-submit.cgi :
SF-Port1521-TCP:V=5.51%I=7%D=2/5%Time=5A7819FB%P=x86_64-redhat-linux-gnu%r
SF:(oracle-tns,65,"ex04"Y(DESCRIPTION=(TMP=)(VSNNUM=
SF:186647296)(ERR=1189)(ERROR_STACK=(ERROR=(CODE=1189)(EMFI=4))
SF:))");
No exact OS matches for host (If you know what OS is running on it, see http://nmap.org/submit/ ).
TCP/IP fingerprint:
OS:SCAN(V=5.51%D=2/5%OT=22%CT=1%CU=30984%PV=Y%DS=1%DC=I%G=Y%TM=5A781A10%P=x
OS:86_64-redhat-linux-gnu)SEQ(SP=FE%GCD=1%ISR=10D%TI=Z%CI=Z%II=I%TS=A)OPS(O
OS:1=M5B4ST11NW7%O2=M5B4ST11NW7%O3=M5B4NNT11NW7%O4=M5B4ST11NW7%O5=M5B4ST11N
OS:W7%O6=M5B4ST11)WIN(W1=3890%W2=3890%W3=3890%W4=3890%W5=3890%W6=3890)ECN(R
OS:=Y%DF=Y%T=40%W=3908%O=M5B4NNSNW7%CC=Y%Q=)T1(R=Y%DF=Y%T=40%S=O%A=S+%F=AS%
OS:RD=0%Q=)T2(R=N)T3(R=Y%DF=Y%T=40%W=3890%S=O%A=S+%F=AS%O=M5B4ST11NW7%RD=0%
OS:Q=)T4(R=Y%DF=Y%T=40%W=0%S=A%A=Z%F=R%O=%RD=0%Q=)T5(R=Y%DF=Y%T=40%W=0%S=Z%
OS:A=S+%F=AR%O=%RD=0%Q=)T6(R=Y%DF=Y%T=40%W=0%S=A%A=Z%F=R%O=%RD=0%Q=)T7(R=Y%
OS:DF=Y%T=40%W=0%S=Z%A=S+%F=AR%O=%RD=0%Q=)U1(R=Y%DF=N%T=40%IPL=164%UN=0%RIP
OS:L=G%RID=G%RIPCK=G%RUCK=G%RUD=G)IE(R=Y%DFI=N%T=40%CD=S)

Network Distance: 1 hop

OS and Service detection performed. Please report any incorrect results at http://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 28.22 seconds

1.21 命令tcpdump:监听网络流量

【功能说明】

命令tcpdump是一个截获网络数据包的包分析工具,tcpdump可以将网络中传输的数据包的“头”完全截获下来以提供分析,它支持针对网络层,协议,主机,端口等的过滤,并支持与、或、非逻辑语协助过滤有效信息。

工具tcpdump工作时要先把网卡的工作模式切换到混杂模式,因为要修改网络接口的工作模式,所以tcpdump命令需要以root的身份运行。

 【语法格式】

tcpdump [option] [expression]
tcpdump [选项] [表达式]

【选项说明】

命令tcpdump的参数选项及说明

参数选项

解释说明(带*的为重点)

-A

ASCII码的方式显示每一个数据包(不会显示数据包中链路层的头部信息)。在抓取包含网页数据的数据包时,可方便查看数据

-c<数据包数目 >

接收到指定的数据包数目后退出命令

-e

每行的打印输出中将包括数据包的数据链路层头部信息

-i< 网络接口>

指定要监听数据包的网络接口(*

-n

不会进行DNS解析,加快显示速度(*

-nn

不将协议和端口数字等转换成名字(*

-q

以快速输出的方式运行,此选项仅显示数据包的协议概要信息,输出信息较短(*

-s<数据包大小 >

设置数据包抓取长度,如果不设置则默认为68字节,设置为0则自动选择合适的长度来抓取数据包

-t

在每一行输出信息中不显示的时间戳标记

-tt

在每一行输出信息中显示无格式的时间戳标记

-ttt

显示当前行与前一行的延迟

-tttt

在每行打印的时间戳之前添加日期

-ttttt

显示当前行与第一行的延迟

-v

显示命令执行的详细信息

-vv

显示比-v选项更加详细的信息

-vvv

显示比-vv选项更加详细的输出

【使用范例】

不加参数运行tcpdump命令监听网络

[root@server ~]# tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
01:55:01.561435 ARP, Request who-has 10.28.11.225 tell 10.28.11.237, length 46
01:55:01.566178 IP 10.28.11.233.43943 > ns.jxncptt.net.cn.domain: 38073+ PTR? 225.11.28.10.in-addr.arpa. (43)
01:55:01.567161 IP ns.jxncptt.net.cn.domain > 10.28.11.233.43943: 38073 NXDomain* 0/1/0 (102)
01:55:01.567443 IP 10.28.11.233.51473 > ns.jxncptt.net.cn.domain: 57868+ PTR? 237.11.28.10.in-addr.arpa. (43)
01:55:01.568225 IP ns.jxncptt.net.cn.domain > 10.28.11.233.51473: 57868 NXDomain* 0/1/0 (102)
01:55:01.568426 IP 10.28.11.233.46700 > ns.jxncptt.net.cn.domain: 17487+ PTR? 69.224.101.202.in-addr.arpa. (45)
01:55:01.569096 IP ns.jxncptt.net.cn.domain > 10.28.11.233.46700: 17487 1/0/0 PTR ns.jxncptt.net.cn. (76)
01:55:01.569268 IP 10.28.11.233.48771 > ns.jxncptt.net.cn.domain: 21013+ PTR? 233.11.28.10.in-addr.arpa. (43)
01:55:01.569985 IP ns.jxncptt.net.cn.domain > 10.28.11.233.48771: 21013 NXDomain* 0/1/0 (102)
01:55:01.685659 IP 10.28.11.233.53490 > ns.jxncptt.net.cn.domain: 32786+ A? time.nist.gov. (31)
01:55:01.685696 IP 10.28.11.233.53490 > ns.jxncptt.net.cn.domain: 13423+ AAAA? time.nist.gov. (31)
01:55:01.690705 IP ns.jxncptt.net.cn.domain > 10.28.11.233.53490: 32786 2/0/0 CNAME ntp1.glb.nist.gov., A 129.6.15.27 (70)

精简输出信息

[root@server ~]# tcpdump -q
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
03:20:01.895627 IP 10.28.11.233.45159 > ns.jxncptt.net.cn.domain: UDP, length 31
03:20:01.895677 IP 10.28.11.233.45159 > ns.jxncptt.net.cn.domain: UDP, length 31
03:20:01.896146 IP 10.28.11.233.52190 > ns.jxncptt.net.cn.domain: UDP, length 45
03:20:01.896576 IP ns.jxncptt.net.cn.domain > 10.28.11.233.45159: UDP, length 70
03:20:01.896620 IP ns.jxncptt.net.cn.domain > 10.28.11.233.45159: UDP, length 82
03:20:01.896855 IP ns.jxncptt.net.cn.domain > 10.28.11.233.52190: UDP, length 76
03:20:01.896990 IP 10.28.11.233.42686 > ns.jxncptt.net.cn.domain: UDP, length 43

监听指定网卡收到的数据包

[root@server ~]# tcpdump -i eth0
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
09:55:01.940703 IP 10.28.11.233.36492 > ns.jxncptt.net.cn.domain: 18550+ A? time.nist.gov. (31)
09:55:01.940835 IP 10.28.11.233.36492 > ns.jxncptt.net.cn.domain: 55091+ AAAA? time.nist.gov. (31)
09:55:01.941369 IP 10.28.11.233.48116 > ns.jxncptt.net.cn.domain: 48039+ PTR? 69.224.101.202.in-addr.arpa. (45)
09:55:01.941468 IP ns.jxncptt.net.cn.domain > 10.28.11.233.36492: 18550 2/0/0 CNAME ntp1.glb.nist.gov., A 129.6.15.29 (70)
09:55:01.941517 IP ns.jxncptt.net.cn.domain > 10.28.11.233.36492: 55091 2/0/0 CNAME ntp1.glb.nist.gov., AAAA 2610:20:6f15:15::27 (82)
09:55:01.942132 IP ns.jxncptt.net.cn.domain > 10.28.11.233.48116: 48039 1/0/0 PTR ns.jxncptt.net.cn. (76)
09:55:01.942282 IP 10.28.11.233.50720 > ns.jxncptt.net.cn.domain: 19350+ PTR? 233.11.28.10.in-addr.arpa. (43)
09:55:01.943112 IP ns.jxncptt.net.cn.domain > 10.28.11.233.50720: 19350 NXDomain* 0/1/0 (102)
09:55:02.042078 IP 10.28.11.233.ntp > time-b-g.nist.gov.ntp: NTPv4, Client, length 48
09:55:02.042347 IP 10.28.11.233.37257 > ns.jxncptt.net.cn.domain: 21440+ PTR? 29.15.6.129.in-addr.arpa. (42)
09:55:02.043137 IP ns.jxncptt.net.cn.domain > 10.28.11.233.37257: 21440 1/0/0 PTR time-b-g.nist.gov. (73)
09:55:03.042111 IP 10.28.11.233.ntp > time-b-g.nist.gov.ntp: NTPv4, Client, length 48
09:55:04.042078 IP 10.28.11.233.ntp > time-b-g.nist.gov.ntp: NTPv4, Client, length 48
09:55:05.042039 IP 10.28.11.233.ntp > time-b-g.nist.gov.ntp: NTPv4, Client, length 48
09:55:06.939818 ARP, Request who-has 10.28.11.225 tell 10.28.11.233, length 28

监听指定协议的数据包

[root@server ~]# tcpdump -n arp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
10:00:06.061927 ARP, Request who-has 10.28.11.225 tell 10.28.11.233, length 28
10:00:06.062813 ARP, Reply 10.28.11.225 is-at 3c:8c:40:02:1a:84, length 46
10:05:06.246797 ARP, Request who-has 10.28.11.225 tell 10.28.11.233, length 28
10:05:06.248446 ARP, Reply 10.28.11.225 is-at 3c:8c:40:02:1a:84, length 46

监听指定端口的数据包

[root@bachup ~]# tcpdump -nn port 22
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
10:10:48.256086 IP 10.28.11.236.40595 > 10.28.11.238.22: Flags [P.], seq 2264351756:2264351804, ack 2825070066, win 167, options [nop,nop,TS val 1637329723 ecr 94058442], length 48
10:10:48.258421 IP 10.28.11.238.22 > 10.28.11.236.40595: Flags [P.], seq 1:49, ack 48, win 189, options [nop,nop,TS val 94177158 ecr 1637329723], length 48
10:10:48.258628 IP 10.28.11.236.40595 > 10.28.11.238.22: Flags [.], ack 49, win 167, options [nop,nop,TS val 1637329725 ecr 94177158], length 0
10:10:48.477102 IP 10.28.11.236.40595 > 10.28.11.238.22: Flags [P.], seq 48:96, ack 49, win 167, options [nop,nop,TS val 1637329944 ecr 94177158], length 48
10:10:48.478342 IP 10.28.11.238.22 > 10.28.11.236.40595: Flags [P.], seq 49:97, ack 96, win 189, options [nop,nop,TS val 94177378 ecr 1637329944], length 48
10:10:48.478571 IP 10.28.11.236.40595 > 10.28.11.238.22: Flags [.], ack 97, win 167, options [nop,nop,TS val 1637329945 ecr 94177378], length 0
10:10:48.904133 IP 10.28.11.236.40595 > 10.28.11.238.22: Flags [P.], seq 96:144, ack 97, win 167, options [nop,nop,TS val 1637330371 ecr 94177378], length 48
10:10:48.906339 IP 10.28.11.238.22 > 10.28.11.236.40595: Flags [P.], seq 97:145, ack 144, win 189, options [nop,nop,TS val 94177806 ecr 1637330371], length 48
10:10:48.906547 IP 10.28.11.236.40595 > 10.28.11.238.22: Flags [.], ack 145, win 167, options [nop,nop,TS val 1637330373 ecr 94177806], length 0

多个条件组合

[root@bachup ~]# tcpdump -n ip host 10.28.11.236 and ! 10.28.11.235
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
10:15:20.310409 IP 10.28.11.236.40596 > 10.28.11.238.ssh: Flags [P.], seq 3679872749:3679872797, ack 3759909486, win 154, options [nop,nop,TS val 1637601770 ecr 94189319], length 48
10:15:20.312409 IP 10.28.11.238.ssh > 10.28.11.236.40596: Flags [P.], seq 1:49, ack 48, win 189, options [nop,nop,TS val 94449212 ecr 1637601770], length 48
10:15:20.312597 IP 10.28.11.236.40596 > 10.28.11.238.ssh: Flags [.], ack 49, win 154, options [nop,nop,TS val 1637601772 ecr 94449212], length 0
10:15:20.696592 IP 10.28.11.236.40596 > 10.28.11.238.ssh: Flags [P.], seq 48:96, ack 49, win 154, options [nop,nop,TS val 1637602156 ecr 94449212], length 48
10:15:20.698425 IP 10.28.11.238.ssh > 10.28.11.236.40596: Flags [P.], seq 49:97, ack 96, win 189, options [nop,nop,TS val 94449598 ecr 1637602156], length 48
10:15:20.698636 IP 10.28.11.236.40596 > 10.28.11.238.ssh: Flags [.], ack 97, win 154, options [nop,nop,TS val 1637602158 ecr 94449598], length 0
10:15:20.987739 IP 10.28.11.236.40596 > 10.28.11.238.ssh: Flags [P.], seq 96:144, ack 97, win 154, options [nop,nop,TS val 1637602447 ecr 94449598], length 48
10:15:20.989417 IP 10.28.11.238.ssh > 10.28.11.236.40596: Flags [P.], seq 97:145, ack 144, win 189, options [nop,nop,TS val 94449889 ecr 1637602447], length 48
10:15:20.989618 IP 10.28.11.236.40596 > 10.28.11.238.ssh: Flags [.], ack 145, win 154, options [nop,nop,TS val 1637602449 ecr 94449889], length 0
原文地址:https://www.cnblogs.com/huihuangyan/p/13704560.html