本文將講述網絡相關命令,做者假定讀者具有TCP/IP協議棧的基礎知識。對於相關命令及其輸出只介紹它的基本的使用方法和大概的描述,具體協議將不做詳細解釋。linux
現在網絡無疑是很重要的,linux系統中提供了豐富的網絡測試與管理命令。咱們來一塊兒看看它們。web
ping
發送TCMP回顯請求報文,並等待返回TCMP回顯應答。ping [OPTIONS]... destination
這裏的目標destination
能夠是目的IP地址或者域名/主機名docker
選項-c
指定發送請求報文的次數,當ping沒有任何選項時,在linux中默認將一直髮送請求報文直到手動終止。shell
[root@centos7 ~]# ping -c 3 www.baidu.com PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data. 64 bytes from 61.135.169.121: icmp_seq=1 ttl=52 time=1.35 ms 64 bytes from 61.135.169.121: icmp_seq=2 ttl=52 time=1.32 ms 64 bytes from 61.135.169.121: icmp_seq=3 ttl=52 time=1.22 ms --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 1.225/1.303/1.359/0.064 ms
首先,ping程序會向域名服務器(DNS)發送請求,解析域名www.baidu.com
的IP地址。DNS
返回域名的一個別名www.a.shifen.com
以及對應的IP地址61.135.169.121
。以後ping程序開始向這個地址發送請求報文,每1s發送一個,ping收到TCMP回顯應答並將結果顯示在終端上,包括ICMP序列號(icmp_seq),生存時間(ttl)和數據包往返時間(time)。最後,給出彙總信息,包括報文總收發狀況,總時間,往返時間最小值、平均值、最大值、平均誤差(越大說明網絡越不穩定)。express
[root@centos7 ~]# ping www.a.com ping: unknown host www.a.com
當目的域名沒法解析出IP地址時,會報未知主機的錯後端
[root@centos7 ~]# ping 192.168.0.1 PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data. ^C #這裏按CTRL+C鍵手動終止了進程 --- 192.168.0.1 ping statistics --- 6 packets transmitted, 0 received, 100% packet loss, time 4999ms
當目的IP地址沒有路由時不會收到任何ICMP回顯報文centos
[root@centos7 ~]# ping -c2 10.0.1.2 PING 10.0.1.2 (10.0.1.2) 56(84) bytes of data. From 10.0.1.254 icmp_seq=1 Destination Host Unreachable From 10.0.1.254 icmp_seq=2 Destination Host Unreachable --- 10.0.1.2 ping statistics --- 2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 999ms pipe 2
當有目的IP的路由但沒法達到時顯示目標不可達錯誤(Destination Host Unreachable)。ICMP
回顯應答還包括超時(request time out)等其餘類型。緩存
hostname
顯示或設置系統主機名hostname [OPTIONS]... [NAME]
直接執行命令hostname
時將顯示主機名:安全
[root@centos7 temp]# hostname centos7 [root@centos7 temp]#
這個主機名是系統的gethostname(2)函數返回的。
能夠經過執行命令hostname NAME
來臨時改變主機名:服務器
[root@centos7 temp]# hostname NAME [root@centos7 temp]# hostname NAME
這個臨時修改其實是修改了linux kernel中一個同爲hostname
的內核參數,它保存在/proc/sys/kernel/hostname
中。若是須要永久修改則須要修改配置文件/etc/sysconfig/network
,centos7中須要修改/etc/hostname
。須要注意的是,若是配置文件中的主機名是localhost
或localhost.localdomain
時,系統會取得網絡接口的IP地址,並用這個地址找出/etc/hosts
文件中對應的主機名,而後將其設置成最終的hostname
。
host
DNS查詢host name
host
命令經過配置文件/etc/resolv.conf
中指定的DNS服務器查詢name
的IP地址:
[root@centos7 temp]# host www.baidu.com www.baidu.com is an alias for www.a.shifen.com. www.a.shifen.com has address 61.135.169.121 www.a.shifen.com has address 61.135.169.125
dig
DNS查詢dig
和host
命令的語法一致,但提供了更詳細的信息和更多的選項:
[root@centos7 ~]# dig www.baidu.com ; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.2 <<>> www.baidu.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22125 ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;www.baidu.com. IN A ;; ANSWER SECTION: www.baidu.com. 113 IN CNAME www.a.shifen.com. www.a.shifen.com. 113 IN A 61.135.169.125 www.a.shifen.com. 113 IN A 61.135.169.121 ;; Query time: 2 msec ;; SERVER: 223.5.5.5#53(223.5.5.5) ;; WHEN: 四 11月 10 12:31:20 CST 2016 ;; MSG SIZE rcvd: 90 [root@centos7 ~]#
如只查詢域名的A記錄並以短格式顯示:
[root@centos7 ~]# dig www.baidu.com A +short www.a.shifen.com. 61.135.169.125 61.135.169.121 [root@centos7 ~]#
或者:
[root@centos7 ~]# dig +nocmd www.baidu.com A +noall +answer www.baidu.com. 252 IN CNAME www.a.shifen.com. www.a.shifen.com. 252 IN A 61.135.169.125 www.a.shifen.com. 252 IN A 61.135.169.121
還能夠用@server
的方式指定DNS服務器:
[root@centos7 ~]# dig +noall +answer www.baidu.com A @8.8.8.8 www.baidu.com. 21 IN CNAME www.a.shifen.com. www.a.shifen.com. 263 IN A 61.135.169.125 www.a.shifen.com. 263 IN A 61.135.169.121
更多的命令及選項請自行man
traceroute
或tracepath
路由跟蹤[root@centos7 ~]# tracepath www.baidu.com 1?: [LOCALHOST] pmtu 1500 1: 10.0.1.103 0.396ms 1: 10.0.1.103 0.350ms 2: 210.51.161.1 1.187ms asymm 3 3: 210.51.161.1 8.186ms 4: 210.51.175.81 1.117ms 5: 61.148.142.61 8.554ms asymm 12 6: 61.148.147.13 1.694ms asymm 12 7: 123.126.8.117 3.934ms asymm 10 8: 61.148.155.46 2.703ms asymm 10 ....
這裏只列出部分輸出,表示跟蹤到目的地址的路由,每一跳都返回。
ifconfig
配置網絡接口當命令沒有任何參數時顯示全部網絡接口的信息:
[root@centos7 ~]# ifconfig ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.20.71.254 netmask 255.255.255.0 broadcast 172.20.71.255 inet6 fe80::250:56ff:fea4:fe34 prefixlen 64 scopeid 0x20<link> ether 00:50:56:a4:fe:34 txqueuelen 1000 (Ethernet) RX packets 11996157 bytes 775368588 (739.4 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 12 bytes 888 (888.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.1.254 netmask 255.255.255.0 broadcast 10.0.1.255 inet6 fe80::250:56ff:fea4:a09 prefixlen 64 scopeid 0x20<link> ether 00:50:56:a4:0a:09 txqueuelen 1000 (Ethernet) RX packets 20941185 bytes 1307830447 (1.2 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 147552 bytes 11833605 (11.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@centos7 ~]#
本例中顯示了兩個網卡ens32
和ens33
以及環回口lo
的信息,包括mtu,ip地址,掩碼,mac地址,傳輸和接收數據量等等。
選項-s
顯示精簡的信息:
[root@idc-v-71253 ~]# ifconfig -s ens32 Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg ens32 1500 11996951 0 0 0 12 0 0 0 BMRU
如給ens33增長一個新地址10.0.1.4:
[root@centos7 ~]# ifconfig ens33:0 10.0.1.4/24 up [root@centos7 ~]# ifconfig ens33:0 ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.1.4 netmask 255.255.255.0 broadcast 10.0.1.255 ether 00:50:56:a4:0a:09 txqueuelen 1000 (Ethernet)
命令中/24
代表接口地址的掩碼,up
表示啓用此接口。注意若是ip地址已經被使用,這裏依然會被設置成功,但此地址被訪問時,可能會有衝突。
停用某接口:
[root@centos7 ~]# ifconfig ens33:0 down
若是須要永久增長或修改當前接口的地址,最好直接編輯網卡配置文件/etc/sysconfig/network-scripts/ifcfg-ens33
(其餘系統換成相應文件)中IPADDR
字段,而後重啓網絡systemctl restart network
或service network restart
生效。
arp
和arping
命令arp
顯示系統的arp緩存,命令arping
給鄰居主機發送ARP請求。
[root@idc-v-71253 ~]# arp -a ? (10.0.1.1) at 68:8f:84:01:f1:ff [ether] on ens33 ? (10.0.1.102) at 00:50:56:a4:18:9a [ether] on ens33 ? (10.0.1.254) at 00:50:56:a4:a9:16 [ether] on ens33 ? (10.0.1.10) at 00:50:56:a4:d2:e4 [ether] on ens33 ? (10.0.1.104) at 00:50:56:a4:37:a7 [ether] on ens33
?
表示未知域名,最後的網卡名錶示arp表項對應的網絡接口
如發現某地址不穩定,可使用arping測試該地址是否爲MAC地址衝突:
[root@centos7 ~]# arping 10.0.1.252 -I ens33 ARPING 10.0.1.252 from 10.0.1.254 ens33 Unicast reply from 10.0.1.252 [00:50:56:A4:65:71] 0.843ms Unicast reply from 10.0.1.252 [00:50:56:A4:0A:09] 1.034ms
這裏兩條返回信息中的MAC地址不一樣,說明有兩塊網卡配置了相同的IP地址。選項-I
指定發送arp請求的網絡接口。
若是剛剛更改了網卡的IP地址,但上游設備(如交換機)的arp表項仍是老的,可使用arping
來強制刷新:
[root@centos7 ~]# arping -c3 -I ens33 -s 10.0.1.254 10.0.1.1 ARPING 10.0.1.1 from 10.0.1.254 ens33 Unicast reply from 10.0.1.1 [68:8F:84:01:F1:FF] 19.466ms Unicast reply from 10.0.1.1 [68:8F:84:01:F1:FF] 2.358ms Unicast reply from 10.0.1.1 [68:8F:84:01:F1:FF] 24.305ms Sent 3 probes (1 broadcast(s)) Received 3 response(s)
-c
指定發送arp請求次數,-s
指定源地址,最後的IP表示發送目標(這裏是網關地址)。
route
顯示或更改路由表[root@centos7 ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.0.1.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 link-local 0.0.0.0 255.255.0.0 U 1002 0 0 ens32 link-local 0.0.0.0 255.255.0.0 U 1003 0 0 ens33 172.20.71.0 0.0.0.0 255.255.255.0 U 0 0 0 ens32 192.168.78.0 10.0.1.104 255.255.255.0 UG 0 0 0 ens33
其中Destination
表示目的網段或目標主機;Gateway
表示網關地址;Genmask
表示目的網段的掩碼;Flags
表示路由標誌:U表示路由是啓用(up)的、G表示網關;Metric
表示目標距離,一般用跳數表示;Ref
表示路由的引用數;Use
表示路由查找計數;Iface
表示此條路由的出口。
選項-n
表示用數字形式顯示目的網段
選項add
和del
表示添加或刪除一條路由。
選項-net
和netmask
表示指定目的網段及掩碼。
選項gw
表示指定網關。
選項dev IF
表示指定出口網卡
如增長一條到192.56.76.x的路由,使它的出口爲ens32:
route add -net 192.56.76.0 netmask 255.255.255.0 dev ens32
如增長一條默認路由,指明它的網關爲10.0.1.1
route add default gw 10.0.1.1
如增長一條到172.20.70.0的路由,網關爲10.0.1.2
route add -net 172.20.70.0/24 gw 10.0.1.2
如刪除默認路由
route del default
telnet
提供遠程登陸功能因爲telnet協議使用明文傳輸,在要求安全登陸的環境中並不適用。如今一般用它來進行網絡服務的端口測試:
[root@centos7 ~]# telnet 10.0.1.251 80 Trying 10.0.1.251... Connected to 10.0.1.251. Escape character is '^]'. ^] #這裏按了CTRL+],也能夠按CTRL+C強行退出。 telnet> quit Connection closed.
這裏對方的80端口是開啓並容許通訊的。
當對端端口沒有開啓時:
[root@centos7 ~]# telnet 10.0.1.251 81 Trying 10.0.1.251... telnet: connect to address 10.0.1.251: No route to host
當對端拒絕鏈接時:
[root@centos7 ~]# telnet 10.0.1.251 8085 Trying 10.0.1.251... telnet: connect to address 10.0.1.251: Connection refused
ssh
遠程登陸程序ssh [OPTIONS]... [user@]hostname [command]
ssh
的全稱是Secure Shell,在不安全的網絡主機間提供安全加密的通訊,旨在代替其餘遠程登陸協議。
[root@centos7 ~]# ssh 10.0.1.253 The authenticity of host '10.0.1.253 (10.0.1.253)' can't be established. ECDSA key fingerprint is 96:bd:a3:a7:87:09:1b:53:44:4c:9b:b9:5f:b2:97:89. Are you sure you want to continue connecting (yes/no)? yes #這裏輸入yes Warning: Permanently added '10.0.1.253' (ECDSA) to the list of known hosts. root@10.0.1.253's password: #這裏輸入密碼 Last login: Fri Nov 11 09:04:01 2016 from 192.168.78.137 [root@idc-v-71253 ~]# #已登陸
當命令ssh
後直接跟主機IP時表示使用默認用戶root
登陸,若是是首次登陸,須要確認添加該主機的認證key,當輸入yes後,即會在本機/root/.ssh/known_hosts
中增長一條該主機的記錄,下一次登陸時就不用再次確認了。而後須要輸入用戶密碼,經過驗證以後,咱們就得到了目的主機的一個shell,咱們就能夠在這個shell中執行命令了。
在新shell中輸入exit
便可退回到原shell。
若是須要頻繁登陸某主機,但不想每次都輸入密碼,能夠設置免密碼登陸:
[root@centos7 ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): #回車 Enter passphrase (empty for no passphrase): #回車 Enter same passphrase again: #回車 Your identification has been saved in /root/.ssh/id_rsa. #私鑰 Your public key has been saved in /root/.ssh/id_rsa.pub. #公鑰 The key fingerprint is: be:c3:d0:02:50:35:35:fe:60:d6:2f:26:96:f0:e1:e6 root@centos7 The key's randomart image is: +--[ RSA 2048]----+ | ...o.o | | . o o | | . . * . | | . * = . | | . .S + . | | o=.o . | | +E | | o. | | .. | +-----------------+ [root@centos7 ~]# [root@centos7 ~]# ssh-copy-id 10.0.1.253 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@10.0.1.253's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '10.0.1.253'" and check to make sure that only the key(s) you wanted were added. [root@centos7 ~]#
其中命令ssh-keygen
用來生成公鑰私鑰,選項-t
指明密鑰類型。以後使用命令ssh-copy-id
將公鑰發送至目標主機,這裏須要輸入目標主機用戶密碼。而後就能夠免密碼登陸了:
[root@centos7 ~]# ssh 10.0.1.253 Last login: Fri Nov 11 11:08:37 2016 from 10.0.1.254 [root@idc-v-71253 ~]#
還能夠經過ssh遠程執行命令:
[root@centos7 ~]# ssh 10.0.1.252 "hostname" root@10.0.1.252's password: #輸入密碼 idc-v-71252 #顯示命令結果 [root@centos7 ~]# #並不登陸
或者手動將公鑰拷貝至目標主機:
[root@centos7 ~]# cat /root/.ssh/id_rsa.pub | ssh 10.0.1.252 "cat - >> /root/.ssh/authorized_keys" root@10.0.1.252's password: #輸入密碼 [root@centos7 ~]# ssh 10.0.1.252 #免密登陸 Last login: Thu Nov 10 14:42:11 2016 from 192.168.78.135 [root@idc-v-71252 ~]#
選項-p
爲登陸指定端口:
[root@centos7 temp]# ssh -p22 10.0.1.252 Last login: Fri Nov 11 11:44:31 2016 from 10.0.1.254 [root@idc-v-71252 ~]#
端口設置在服務端配置文件/etc/ssh/sshd_config
中,默認端口號爲22,如更改需將#Port 22
去掉註釋並將22更改成須要的端口,而後重啓sshd服務service sshd restart
或systemctl restart sshd
。
若是須要使用另外的用戶登陸系統則執行ssh user@host
咱們能夠用tar
命令結合ssh
和管道,將本地(遠程)文件備份到遠程(本地):
tar zc /home/temp | ssh user@host "tar xz" #本地temp目錄備份到遠程 ssh user@host "tar cz /home/temp" | tar xz #遠程temp目錄備份到本地
選項-L [bind_address:]port:host:hostport
設置本地端口轉發
[root@centos7 ~]# ssh -L 2222:10.0.1.252:22 10.0.1.253 Last login: Mon Nov 14 10:34:43 2016 from 10.0.1.254 [root@idc-v-71253 ~]# #注意若是這裏exit斷開鏈接,則此轉發也將終止。
此命令的意思是綁定本地端口2222
,並將全部發送至此端口的數據經過中間主機10.0.1.253
轉發至目標主機10.0.1.252
的22
端口,此時若是用ssh
登陸本機的2222端口,則實際登陸的是主機10.0.1.252
[root@centos7 ~]# ssh -p 2222 127.0.0.1 Last login: Mon Nov 14 10:34:56 2016 from 10.0.1.253 [root@idc-v-71252 ~]#
這裏默認綁定的是本機的環回口127.0.0.1
,如綁定到其餘地址,則根據語法設置bind_address
。
選項-N
表示不執行命令,只設置端口轉發時有用
因爲上述端口轉發命令ssh -L 2222:10.0.1.252:22 10.0.1.253
會登陸到中間主機,而且退出後端口轉發也會終止,使用-N
選項將不會登陸,再配合shell後臺執行,將會是一個不錯的設置端口轉發的選擇(但要注意對中間主機須要免密碼登陸):
[root@centos7 ~]# ssh -N -L 2222:10.0.1.252:22 10.0.1.253 & [1] 12432 [root@centos7 ~]#
命令最後的符號&
表示此命令將在後臺執行,返回的信息中[1]
表示後臺命令編號,12432
表示命令的PID。(關於shell後臺命令,之後的文章中會有敘述)
選項-R [bind_address:]port:host:hostport
設置遠程端口轉發
如咱們在10.0.1.253
上執行:
ssh -R 2222:10.0.1.252:22 10.0.1.254
而後在10.0.1.254
上登陸:
[root@centos7 ~]# ssh -p 2222 localhost Last login: Mon Nov 14 10:40:44 2016 from 10.0.1.253 [root@idc-v-71252 ~]#
這裏的意思是使遠程主機10.0.1.254
(相對10.0.1.253來講)監聽端口2222
,而後將全部發送至此端口的數據轉發至目標主機10.0.1.252
的端口22
。以後再在10.0.1.254
登陸本地(localhost)的2222
端口時,實際經過中間主機10.0.1.253
登陸目標主機10.0.1.252
。
選項-o OPTION
指定配置文件(如/etc/ssh/sshd_config
)內選項
如避免第一次登陸時輸入yes
確認,可增長-o StrictHostKeyChecking=no
。
scp
遠程複製文件scp [OPTIONS]... [[user@]host1:]file1 ... [[user@]host2:]file2
scp
命令經過ssh
協議將數據加密傳輸,和ssh
登陸相似,須要輸入遠程主機用戶密碼。
如將遠程主機10.0.1.251
中文件/root/a.txt複製到本地當前目錄下:
[root@centos7 ~]# scp root@10.0.1.251:/root/a.txt ./ root@10.0.1.251's password: a.txt 100% 125 0.1KB/s 00:00 [root@centos7 ~]#
命令會顯示傳輸狀態(傳輸百分比,大小,速度,用時)。
將本地文件複製到遠程無非是將源和目的調換位置。
選項-P
指定遠端鏈接端口(ssh服務端口),-o ssh_option
使用ssh選項。
選項-l limit
傳輸限速,limit
單位爲Kbit/s。
和命令cp
相似,選項-r
表示複製目錄,-p
表示保留文件權限時間等
netstat
打印網絡信息選項-a
顯示全部端口信息:
[root@centos7 ~]# netstat -a Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN tcp 0 52 10.0.1.254:ssh 192.168.78.143:49583 ESTABLISHED tcp6 0 0 [::]:commplex-main [::]:* LISTEN tcp6 0 0 [::]:4243 [::]:* LISTEN tcp6 0 0 [::]:ssh [::]:* LISTEN tcp6 0 0 localhost:smtp [::]:* LISTEN raw6 0 0 [::]:ipv6-icmp [::]:* 7 raw6 0 0 [::]:ipv6-icmp [::]:* 7 Active UNIX domain sockets (servers and established) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 12807 /run/systemd/private unix 2 [ ACC ] STREAM LISTENING 12815 /run/lvm/lvmpolld.socket unix 2 [ ] DGRAM 12818 /run/systemd/shutdownd unix 2 [ ACC ] STREAM LISTENING 16403 /var/run/dbus/system_bus_socket ....
這裏只顯示部分信息
選項-t
顯示TCP鏈接信息
選項-n
顯示IP地址而不進行域名轉換
選項-p
顯示PID和程序名
[root@centos7 ~]# netstat -antp Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1358/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2162/master tcp 0 52 10.0.1.254:22 192.168.78.143:49583 ESTABLISHED 12044/sshd: root@pt tcp6 0 0 :::5000 :::* LISTEN 17222/docker-proxy tcp6 0 0 :::4243 :::* LISTEN 16983/docker tcp6 0 0 :::22 :::* LISTEN 1358/sshd tcp6 0 0 ::1:25 :::* LISTEN 2162/master [root@centos7 ~]#
其中Proto
表示協議(包括TCP、UDP等);Recv-Q
和Send-Q
表示接收和發送隊列,通常都爲0,若是非0則表示本地的接收或發送緩存區有數據等待處理;Local Address
和Foreign Address
分別表示本地地址和遠端地址;State
表示鏈接狀態,對應於TCP各類鏈接狀態;PID/Program name
表示進程號和程序名。
選項-l
表示只顯示狀態爲LISTEN
的鏈接
[root@centos7 ~]# netstat -ntl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp6 0 0 :::5000 :::* LISTEN tcp6 0 0 :::4243 :::* LISTEN tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 ::1:25 :::* LISTEN [root@centos7 ~]#
選項-u
表示顯示UDP鏈接信息
選項-r
表示顯示路由信息
[root@centos7 ~]# netstat -r Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface default 10.0.1.103 0.0.0.0 UG 0 0 0 ens33 10.0.1.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 172.20.71.0 0.0.0.0 255.255.255.0 U 0 0 0 ens32 192.168.78.0 10.0.1.104 255.255.255.0 UG 0 0 0 ens33
選項-i
顯示接口信息
[root@centos7 ~]# netstat -i Kernel Interface table Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg ens32 1500 13196107 0 77 0 3246 0 0 0 BMRU ens33 1500 25312388 0 88 0 2516050 0 0 0 BMRU lo 65536 2503589 0 0 0 2503589 0 0 0 LRU
tcpdump
網絡抓包工具命令tcpdump
捕獲某網絡接口符合表達式expression
的數據包,並打印出數據包內容的描述信息。
選項-i
指定網卡:
[root@idc-v-71253 ~]# tcpdump -i ens33 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on ens33, link-type EN10MB (Ethernet), capture size 65535 bytes 15:41:59.121948 IP 10.0.1.108.3693 > 239.100.1.1.websm: UDP, length 58 15:41:59.122191 IP 10.0.1.109.35673 > 239.100.1.1.websm: UDP, length 57 15:41:59.128282 IP 10.0.1.253.ssh > 192.168.78.143.51694: Flags [P.], seq 749565300:749565496, ack 3522345564, win 255, length 196 15:41:59.134127 IP 192.168.78.143.51694 > 10.0.1.253.ssh: Flags [.], ack 196, win 3977, length 0 15:41:59.140319 ARP, Request who-has 10.0.1.31 tell 10.0.1.102, length 46 15:41:59.168328 ARP, Request who-has 10.0.1.37 tell 10.0.1.102, length 46 15:41:59.262235 ARP, Request who-has 192.168.10.150 tell 192.168.10.151, length 46 15:41:59.622090 IP 10.0.1.108.3693 > 239.100.1.1.websm: UDP, length 58 15:41:59.622178 IP 10.0.1.109.35673 > 239.100.1.1.websm: UDP, length 57 ....
啓動命令以後顯示出可使用-v
或-vv
顯示更詳細的信息,開始從ens33捕獲數據包。輸出顯示出各個發送或接收數據包包頭信息(包括ARP、IP、TCP、UDP等等協議)。此命令並未指定expression
,因此默認將捕獲全部數據包。
若是須要將數據包捕獲而後經過其餘程序(如wireshark)分析,可使用選項-w file
將數據寫入文件,同時還須要使用選項-s 0
指定可以捕獲的數據包大小爲65535字節,以免數據包被截斷而沒法被分析。
真實環境中,流經網卡的數據包量是巨大的。可使用表達式來對數據包進行過濾,對於每一個數據包,都要通過表達式的過濾,只有表達式的值爲true時,纔會輸出。expression
中能夠包含一到多個關鍵字指定的條件,可使用and
(或&&
)、or
(或||
)、not
(或!
)和括號()
表示各個關鍵字間的邏輯關係,能夠用>
、<
表示比較,還能夠進行計算。其中關鍵字包括:type
類型關鍵字,如host
、net
、port
和portrange
,分別表示主機、網段、端口號、端口段。direction
方向關鍵字,如src
、dst
分別表示源和目的。proto
協議關鍵字,如fddi
、arp
、ip
、tcp
、udp
等分別表示各類網絡協議。
因爲篇幅所限,下面的例子中將只描述選項和表達式所起到的做用,再也不解釋輸出內容:
tcpdump -i ens33 dst host 10.0.1.251 #監視全部從端口ens33發送到主機10.0.1.251的數據包,主機也能夠是主機名 tcpdump -i eth0 host ! 211.161.223.70 and ! 211.161.223.71 and dst port 80 #監聽端口eth0,抓取不是來自或去到主機211.161.223.70和211.161.223.71而且目標端口爲80的包 tcpdump tcp port 23 host 210.27.48.1 #獲取主機210.27.48.1接收或發出的telnet包 tcpdump 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0) and src net (183.60.190 or 122.13.220)' -s0 -i eth0 -w ipdump #抓取源或目的端口是80,且源網絡是(183.60.190.0/24 或者 122.13.220.0/24),而且含有數據,而不是SYN,FIN以及ACK-only等不含數據的TCP數據包寫入文件ipdump #注意這裏表達式使用單引號引發來以免其中的特殊字符被shell解析而形成語法錯誤 tcpdump 'tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and ! src and dst net 10.0.0' #只打印TCP的開始和結束包(SYN和FIN標記),而且源和目標網段均不是10.0.0.0/24 tcpdump 'gateway 10.0.1.1 and ip[2:2] > 576' #表示抓取發送至網關10.0.1.1而且大於576字節的IP數據包
網絡相關命令內容較多,下一篇將繼續介紹。