LINUX核心命令實戰總結十一——網路管理命令
1.1命令ifconfig:配置或顯示網路介面資訊
【功能說明】
命令ifconfig用於配置網絡卡IP地址等,網路引數或顯示當前網路的介面狀態,其類似於Windows的ipconfig命令
如果沒有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:View Code24123 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)
顯示指定網絡卡的資訊
[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=noView 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:網路配置工具
【功能說明】
命令ip是iproute軟體包中的一個強大的網路配置工具,用於顯示或管理linux系統的路由,網路裝置、策略路由和隧道。
【語法格式】
ip [ OPTIONS ] OBJECT COMMAND
ip [ 選項 ] 網路物件 操作命令
【選項說明】
引數選項 |
解釋說明(帶*的為重點) |
-s |
輸出更詳細的資訊,為了顯示更詳細的資訊,可重複使用此選項 |
-r |
顯示主機時,不使用IP地址,而是使用主機的域名 |
網路物件 |
指定要管理的網路物件 支援的網路物件如下: link 網路裝置 address 裝置的協議地址(IP地址) addrlabel 協議地址標籤管理 neighbour arp或ndisc快取表 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物件支援的操作命令:add、del、list、flush; neighbour物件支援的操作命令:add、change、replace、delete、show、flush; route物件支援的操作命令:add、change、replace、delete、show、flush、get; rule物件支援的操作命令:add、delete、flush、show; maddress物件支援的操作命令:show、add、delete; mroute物件支援操作命令:show; tunnel物件支援的操作命令:state、policy、monitor; |
【使用範例】
顯示網路裝置屬性
[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 |
顯示路由器資訊,該功能類似於前面學過的route和ip 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 :::40118View 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 工具,它能用來檢視網路狀態資訊,包括TCP、UDP連線、埠等。它的優點是能夠顯示更多更詳細的有關網路連線狀態的資訊,而且比netstat更快速更高效。
如果系統沒有ss命令,那就需要安裝一下,ss命令屬於iproute包,因此安裝命令是 yum -y install iproute
【語法格式】
ss [options] [ FILTER ]
ss [options] [ FILTER ]
【選項說明】
引數選項 |
解釋說明(帶*的為重點) |
-n |
顯示路由器資訊,該功能類似於前面學過的route和ip 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 |
僅顯示PACKET的socket |
-t |
僅顯示TCP的socket(*) |
-u |
僅顯示UCP的socket |
-d |
僅顯示DCCP的 socket |
-w |
僅顯示RAW的socket |
-x |
僅顯示UNIX的socket |
【使用案例】
常用選項組合
[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資料包,監聽任意的TCP和UDP埠,進行埠掃描,處理IPV4和IPV6資料包。
如果系統沒有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:安全地遠端登入主機
【功能說明】
命令ssh是openssh套件中的客戶連線工具,可以使用ssh加密協議實現安全的遠端登入伺服器,實現對伺服器的遠端管理,windows中的替代工具為Xshell、putty、secureCRT.
【語法格式】
ssh [option] [user@hostname] [command]
ssh [選項] [使用者@主機名或IP地址] [遠端執行命令]
【選項說明】
引數選項 |
解釋說明(帶*的為重點) |
-p |
指定ssh登入埠,如果忽略則預設埠為22(SSH伺服器的預設埠) |
-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-[email protected] 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.241View 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:顯示郵件傳輸佇列
【功能說明】
命令是maliq是mail 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= 型別名 指定域名查詢的型別(例如:A,HINFO,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資料型別,如A、MX和PTR等。預設的查詢型別為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