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,"\0e\0\0\x04\0\0\0\"\0\0Y\(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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM