大綱:linux
1.1-網絡協議的應用和TCP鏈接web
瞭解常見的網絡相關協議算法
TCP三次握手和四次揮手express
1.2-RHEL7.網絡管理 vim
網絡相關的調試命令安全
實戰tcpdump和tshark抓包bash
=================================================================網絡
網絡協議的應用和鏈接app
常見網絡協議:ssh
ARP(Address Resolution Protocol)協議:IP地址解析爲MAC地址
UDP(User Datagram Protocol)協議:用戶數據包協議
DHCP(Dynamic Host Configuration Protocol)協議:動態主機配置協議
HTTP(Hypertext Transfer Protocol)協議:超文本傳輸協議
HTTPS(Secure Hypertext Transfer Protocol)協議:安全超文本傳輸協議
ICMP(Internet Control Message Protocol)協議:數據控制協議
DNS協議:域名解析協議 (Domain Name System Protocol)
FTP(File Transfer Protocol)協議:文件傳輸協議
SMTP(Simple Mail Transfer Protocol)協議:簡單郵件傳輸協議
SNMP(Simple Network Managerment Protocol)協議:網絡管理協議
TFTP(Trivial File Transfer Protocol)協議:小文件傳輸協議
Telnet協議:虛擬終端協議
===================================================================
TCP/IP協議簇
TCP/IP協議是一個協議的集合,包含不少協議,具體以下:
HTTP、FTP、TFTP、Telnet、SNMP、TCP、UDP、IP、ICMP、ARP、RARP等。
這裏咱們重點了解TCP和UDP
TCP(Transmission Control Protocol,傳輸控制協議)
是面向鏈接的協議
在收發數居前,必須創建可靠的數據鏈接。
一個TCP鏈接必須通過三次握手才能創建起來,(過程很複雜)
解釋:
一、主機A向主機B發送鏈接請求
二、主機B向主機A發送收到請求確認並要求同步(同步就是兩臺主機,一個在發送,一個在接受)
三、主機A向主機B發送確認同步信息
總結:三次握手的目的是使數據包的發送和接收同步,通過三次握手後,主機A才向主機B正式發送數據
UDP(User Datagram Protocol,用戶數據包協議)
一個非鏈接的協議,傳輸數據前,源端和終端不創建鏈接
UDP信息包的標題很短,只有8個字節,(相對於TCP的20個字節信息包)額外開銷很小。
吞吐量不收擁擠控制算法的調解,只受應用軟件生成數據的速率、傳輸帶寬、源端和終端主機性能的限制。
UDP只管盡最大努力傳輸數據,可是不保證數據是否交付給終端,所以主機不須要維持複雜的鏈接狀態。
總結:UDP傳輸速度很快,但不可靠!
總結:TCP和UDP的區別
一、相對於鏈接:TCP基於鏈接、UDP無鏈接
二、相對於系統資源:TCP佔用較多,UDP較少
三、相對於結構:TCP複雜、UDP簡單
四、相對於數據正確性和順序:TCP保證數據正確性及順序、UDP不保證
###############################
###############################
###############################
TCP三次握手創建的過程
Seq:序列號---根據序列號表示操做
Ack:確認標誌---確認編號
SYN:同步標誌
A-->seq=x
B-->ack+seq,SYN=1 (其中ack=A的seq+1)
A-->ack+seq,SYN=1 (其中ack=B的seq+1)
(主機A第一次向主機B發送數據是否有SYN同步標誌?)
TCP四次揮手斷開鏈接過程
FIN:結束標誌
A-->FIN,ack,seq
B-->ack,seq (其中ack=A的seq+1)
B-->FIN,seq,ack (其中ack=A的seq)
A-->ack,seq(其中seq爲第一次的seq,ack爲上一條B發送結束標誌的seq+1)
過程解析:
一、主機A向主機B發送結束請求
二、主機B向主機A發送回覆,收到結束請求確認
三、主機B向主機A發送結束請求
四、主機A向主機B發送恢復,收到結束請求確認
爲何鏈接的時候是三次握手,關閉的時候倒是四次握手?
由於當Server端收到Client端的SYN鏈接請求報文後,能夠直接發送SYN+ACK報文。
其中ACK報文是用來應答的,SYN報文是用來同步的。
可是關閉鏈接時,當Server端收到FIN報文時,極可能並不會當即關閉SOCKET,因此只能先回復一個ACK報文,告訴Client端,"你發的FIN報文我收到了"。只有等到我Server端全部的報文都發送完了,我才能發送FIN報文,所以不能一塊兒發送。故須要四步握手。
############################################
############################################
############################################
Linux下端口號的分配
TCP和UDP採用16位的端口號來識別應用程序
TCP/IP的臨時分配1024-5000之間的端口號
大於5000的端口號爲其餘服務保留
TCP端口分配
20、21 ftp 文件傳輸協議
22 ssh 安全遠程鏈接服務
23 telnet 遠程鏈接服務
25 smtp 電子郵件服務
53 DNS 域名解析服務
80 http web服務
443 https安全web服務
UDP端口分配
69 tftp 簡單文件傳輸協議
123 ntp 時間同步服務
161 snmp 簡單網絡管理
如何查詢服務對應端口號?
當咱們知道服務名稱時,可以使用netstat命令查看
netstat -anlpt | grep [服務名] #TCP
netstat -anlpu | grep [服務名] #UDP
當咱們不知道服務名稱時呢?
咱們能夠查看linux配置文件
vim /etc/services #包含全部的端口號
如圖:
咱們會發現有的端口,TCP和UDP都會監聽
############################################
查看端口監聽狀態--->netstat命令
Usage:
netstat -anlpt #顯示全部TCP鏈接
netstat -anlpu #顯示全部UDP鏈接
netstat -anptu #顯示全部TCP、UDP鏈接
-n, --numeric don't resolve names
-p, --programs display PID/Program name for sockets #顯示進程ID
-l, --listening display listening server sockets #顯示監聽端口
-a, --all display all sockets (default: connected) #顯示全部鏈接
-t 顯示TCP鏈接
-u 顯示UDP鏈接
############################################
網絡相關調試命令:
查詢網卡物理鏈接狀態--->mii-tool 命令
(view, manipulate media-independent interface status)
[root@xiaogan122 ~]# mii-tool eno16777736 #查詢網卡物理鏈接狀態
查看網卡硬件相關信息--->ethtool命令
(query or control network driver and hardware settings)
ethtool能夠查詢並設置網絡驅動和硬件設置,詳情見:man ethtool
[root@xiaogan122 ~]# ethtool eno16777736 #查詢網卡硬件相關信息
############################################
############################################
############################################
實戰:爲虛擬機添加一起網卡
第一步:虛擬機軟件中添加硬件---網卡
第二步:查看添加的網卡名稱
第三步:複製一個網卡配置文件爲新網卡的配置文件
cd /etc/sysconfig/network-scripts
cp ifcfg-eno16777736 ifcfg-eno50332216
vim ifcfg-eno50332216
具體配置以下:
成果以下:
第四步:重啓網絡服務,並查看ip狀態
[root@xiaogan122 ~]# systemctl restart NetworkManager
[root@xiaogan122 ~]# systemctl restart network
[root@xiaogan122 ~]# ifconfig
第五步:到此兩塊網卡,添加成功!!!
當發生配置不成功,觸犯規則問題時,可刪除規則文件並重啓便可:
[root@xiaogan122 network-scripts]# rm -fr /etc/udev/rules.d/70-persistent-ipoib.rules
重啓計算機
咱們還能夠使用nmtui-edit圖形界面來配置新添加的網卡,圖形界面這裏再也不闡述。
啓動命令 nmtui-edit eno50332216
注意:若使用nmtui-edit命令,需保證NetworkManager服務啓動;
在RHEL7中強化了NetworkManager服務功能,弱化了network的功能。
[root@xuegod60 ~]# systemctl status NetworkManager
● NetworkManager.service - Network Manager
Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2016-03-07 20:19:42 CST; 1h 36min ago
開啓NetworkManager
[root@xuegod60 ~]# systemctl restart NetworkManager #開啓服務
[root@xuegod60 ~]# systemctl enable NetworkManager #設置服務開機自動啓動
#此服務不開啓,則沒法經過nmtui工具配置網絡
############################################
############################################
############################################
修改主機名配置文件
永久需改主機名,直接修改配置文件便可:
vim /etc/hostname #刪除原始記錄,填寫要修改的名稱
修改後保存退出,執行
bash命令,便可!!
臨時修改:
hostname ddd
查看路由信息: 查看默認網關route命令
Usage: route [-nNvee] [-FC] [<AF>] List kernel routing tables
Usage: route [-nNvee] [-FC] [<AF>] List kernel routing tables
[root@xiaogan ~]# route -n
添加/刪除路由條目:
增長 (add) 與刪除 (del) 路由的相關參數:
-net :網絡ID;
-host :爲鏈接到主機的路由;
netmask :與網域有關,能夠設定 netmask 決定網域的大小;
dev :若是隻是要指定由那一塊網路卡連線出去,則使用這個設定,後面接 eth0 等
route add -net 192.168.2.0 netmask 255.255.255.0 dev eno50332216
eno50332216添加192.168.2.0段,可讓其路由到這個網段
route del -net 192.168.2.0 netmask 255.255.255.0 #直接刪除便可
############################################
############################################
############################################
抓包:(在網絡發生堵塞時,使用抓包,查找問題)
tcpdump命令:(dump traffic on a network)
[root@xiaogan ~]# tcpdump -h
tcpdump version 4.5.1
libpcap version 1.5.3
Usage: tcpdump [-aAbdDefhHIJKlLnNOpqRStuUvxX] [ -B size ] [ -c count ]
[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]
[ -i interface ] [ -j tstamptype ] [ -M secret ]
[ -P in|out|inout ]
[ -r file ] [ -s snaplen ] [ -T type ] [ -V file ] [ -w file ]
[ -W filecount ] [ -y datalinktype ] [ -z command ]
[ -Z user ] [ expression ]
port #監聽端口號
-S Print absolute, rather than relative, TCP sequence numbers.
-i Listen on interface.#指定監聽網卡
-w Write the raw packets to file rather than parsing and printing them out.
-c Exit after receiving count packets.
tcpdump port 22 -i eno50332216 -c 3 -n -S
client端安裝telnet
yum -y Install telnet
第一步:在服務端輸入抓包命令,等待抓包
server端:
[root@xiaogan ~]# tcpdump port 22 -c 3 -n -S -i eno50332216
第二步:在client端,輸入鏈接命令
client端:telnet 192.168.171.136 22
第三步:返回server端查看抓包結果!!!
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eno50332216, link-type EN10MB (Ethernet), capture size 65535 bytes
01:49:55.193118 IP 192.168.229.138.37860 > 192.168.229.136.ssh: Flags [S], seq 979150317, win 29200, options [mss 1460,sackOK,TS val 348880 ecr 0,nop,wscale 7], length 0
01:49:55.193213 IP 192.168.229.136.ssh > 192.168.229.138.37860: Flags [S.], seq 1066911696, ack 979150318, win 28960, options [mss 1460,sackOK,TS val 8874543 ecr 348880,nop,wscale 7], length 0
01:49:55.194170 IP 192.168.229.138.37860 > 192.168.229.136.ssh: Flags [.], ack 1066911697, win 229, options [nop,nop,TS val 348881 ecr 8874543], length 0
3 packets captured
3 packets received by filter
0 packets dropped by kernel
抓包命令-wireshark:
tshark命令,
使用方法:tshark -w a.txt -i eno50332216
查看信息:tshark -r a.txt
第一步:客戶端ping server
第二步:server端,使用tshark命令抓包
第三步:ctrl + c終止後,查看抓到的包,注意不能使用cat命令直接查看,而應使用tshark命令直接查看,以下圖:
[root@xiaogan ~]# tshark --help
tshark: invalid option -- '-'
TShark 1.10.14 (Git Rev Unknown from unknown)
Dump and analyze network traffic.
See http://www.wireshark.org for more information.
Copyright 1998-2015 Gerald Combs <gerald@wireshark.org> and contributors.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Usage: tshark [options] ...
Capture interface:
-i <interface> name or idx of interface (def: first non-loopback)
-f <capture filter> packet filter in libpcap filter syntax
-s <snaplen> packet snapshot length (def: 65535)
-p don't capture in promiscuous mode
-I capture in monitor mode, if available
-B <buffer size> size of kernel buffer (def: 2MB)
-y <link type> link layer type (def: first appropriate)
-D print list of interfaces and exit
-L print list of link-layer types of iface and exit
Capture stop conditions:
-c <packet count> stop after n packets (def: infinite)
-a <autostop cond.> ... duration:NUM - stop after NUM seconds
filesize:NUM - stop this file after NUM KB
files:NUM - stop after NUM files
Capture output:
-b <ringbuffer opt.> ... duration:NUM - switch to next file after NUM secs
filesize:NUM - switch to next file after NUM KB
files:NUM - ringbuffer: replace after NUM files
Input file:
-r <infile> set the filename to read from (no pipes or stdin!)
Processing:
-2 perform a two-pass analysis
-R <read filter> packet Read filter in Wireshark display filter syntax
-Y <display filter> packet displaY filter in Wireshark display filter syntax
-n disable all name resolutions (def: all enabled)
-N <name resolve flags> enable specific name resolution(s): "mnNtC"
-d <layer_type>==<selector>,<decode_as_protocol> ...
"Decode As", see the man page for details
Example: tcp.port==8888,http
-H <hosts file> read a list of entries from a hosts file, which will
then be written to a capture file. (Implies -W n)
Output:
-w <outfile|-> write packets to a pcap-format file named "outfile"
(or to the standard output for "-")
-C <config profile> start with specified configuration profile
-F <output file type> set the output file type, default is pcapng
an empty "-F" option will list the file types
-V add output of packet tree (Packet Details)
-O <protocols> Only show packet details of these protocols, comma
separated
-P print packet summary even when writing to a file
-S <separator> the line separator to print between packets
-x add output of hex and ASCII dump (Packet Bytes)
-T pdml|ps|psml|text|fields
format of text output (def: text)
-e <field> field to print if -Tfields selected (e.g. tcp.port, col.Info);
this option can be repeated to print multiple fields
-E<fieldsoption>=<value> set options for output when -Tfields selected:
header=y|n switch headers on and off
separator=/t|/s|<char> select tab, space, printable character as separator
occurrence=f|l|a print first, last or all occurrences of each field
aggregator=,|/s|<char> select comma, space, printable character as
aggregator
quote=d|s|n select double, single, no quotes for values
-t a|ad|d|dd|e|r|u|ud output format of time stamps (def: r: rel. to first)
-u s|hms output format of seconds (def: s: seconds)
-l flush standard output after each packet
-q be more quiet on stdout (e.g. when using statistics)
-Q only log true errors to stderr (quieter than -q)
-g enable group read access on the output file(s)
-W n Save extra information in the file, if supported.
n = write network address resolution information
-X <key>:<value> eXtension options, see the man page for details
-z <statistics> various statistics, see the man page for details
Miscellaneous:
-h display this help and exit
-v display version info and exit
-o <name>:<value> ... override preference setting
-K <keytab> keytab file to use for kerberos decryption
-G [report] dump one of several available reports and exit
default report="fields"
use "-G ?" for more help