目錄
故障排查
tcpdump抓包工具
傳輸層(TCP和UDP協(xié)議)
傳輸層的作用
應(yīng)用程序和端口號有什么關(guān)系?
傳輸層端對端連接實(shí)現(xiàn)拓?fù)鋱D
如何查看自己的linux機(jī)器開放了哪些端口?
1、netstat(network ? status 網(wǎng)絡(luò)的狀態(tài))
netstat查看本機(jī)開放的端口:
socket 槽、套接字(接口)
2、ss命令:
3、lsof命令
如何查看其他機(jī)器開放了哪些端口?
nc、nmap命令
如何知道這個ip地址175.6.49.131(不是本機(jī))對應(yīng)的主機(jī)開放了哪些端口?
nc命令
nmap命令
TCP(Transmission Control Protocol) 傳輸控制協(xié)議
UDP(User Datagram Protocol) 用戶數(shù)據(jù)報協(xié)議
TCP的封裝格式
數(shù)據(jù)傳輸?shù)倪^程:
UDP的封裝格式:
故障排查
在進(jìn)行網(wǎng)絡(luò)故障排查時,需要有耐心和系統(tǒng)性地逐步排查。根據(jù)具體情況,也可能需要使用其他工具和技術(shù)來幫助診斷和解決問題。
tcpdump抓包工具
tcpdump 進(jìn)行網(wǎng)絡(luò)診斷的時候的工具(類似于醫(yī)院里的CT、B超、核磁共振)
tcpdump抓包命令:?
tcpdump -i ens33 tcp dst port 80 and src host 192.168.1.142
#抓取從ens33接口進(jìn)來的數(shù)據(jù)包,協(xié)議是tcp協(xié)議 目的端口是80 同時 源ip地址是192.168.1.142
src host 192.168.1.142 ?源ip地址是192.168.1.142 ?source 源
dst port 80 目的端口是80and ?前面的條件和后面的條件都要滿足
or ?2個條件滿足一個就可以了
tcpdump -i ens33 arp
tcpdump -i ens33 icmp
tcpdump -i ens33 arp or icmp
ping 和 tcpdump 是網(wǎng)絡(luò)診斷的2大神器。
傳輸層(TCP和UDP協(xié)議)
傳輸層的作用
(網(wǎng)絡(luò)層)IP層提供點(diǎn)到點(diǎn)的連接,而傳輸層提供端到端的連接。
它主要負(fù)責(zé)在源主機(jī)和目標(biāo)主機(jī)之間提供端到端的數(shù)據(jù)傳輸服務(wù),并確??煽?、有序地傳輸數(shù)據(jù)。
應(yīng)用程序和端口號有什么關(guān)系?
應(yīng)用程序其實(shí)就是運(yùn)行一個程序提供某個服務(wù)
一個應(yīng)用程序 --》 背后就是一個服務(wù) --》落實(shí)到進(jìn)程來實(shí)現(xiàn) --》進(jìn)程會監(jiān)聽某個端口?
(可以理解為打開一個窗戶或者一扇門(端口),這樣別人(數(shù)據(jù))就可以進(jìn)來了)
QQ --》實(shí)現(xiàn)即時通信(聊天發(fā)消息)--》UDP協(xié)議
傳輸層端對端連接實(shí)現(xiàn)拓?fù)鋱D
一臺電腦里可以同時運(yùn)行很多的應(yīng)用程序
不同的程序不能占用相同的端口號
上圖中的 應(yīng)用程序 和 端口號 綁定了
我們不知道QQ的端口號,我們需要使用抓包工具(科萊),找到QQ的TCP包或者UDP包,從而獲取QQ的端口號,IP地址和協(xié)議
qq server --》8007 --》UDP
如何查看自己的linux機(jī)器開放了哪些端口?
使用netstat、ss(不如netstat好用)、lsof等工具
1、netstat(network ? status 網(wǎng)絡(luò)的狀態(tài))
netstat可以查看本機(jī)開放了哪些端口
應(yīng)用程序--》服務(wù)---》進(jìn)程--》占用一個端口(監(jiān)聽某個端口)
下載netstat:
[root@sc-server ~]# yum install net-tools -y
netstat查看本機(jī)開放的端口:
[root@sc-server ~]# netstat -anplut
-a ?all
-n ?--numeric 數(shù)字的形式顯示
-p ?--program
?[--tcp|-t] ? tcp信息
?[--udp|-u] ? udp信息
?[--listening|-l] ?處于監(jiān)聽狀態(tài)的信息
[root@sc-server ~]# netstat -anplut
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:23 0.0.0.0:* LISTEN 967/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1677/sendmail: acce
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1257/mysqld
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1670/nginx: master
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1670/nginx: master
tcp 0 36 192.168.50.1:23 192.168.50.9:60013 ESTABLISHED 3845/sshd: root@pts
tcp6 0 0 :::23 :::* LISTEN 967/sshd
tcp6 0 0 :::80 :::* LISTEN 1670/nginx: master
udp 0 0 127.0.0.1:323 0.0.0.0:* 693/chronyd
udp6 0 0 ::1:323 :::* 693/chronyd
其中的名詞解釋:
ESTABLISHED ?建立連接
LISTEN ?聽(監(jiān)聽) 監(jiān)控某個端口看是否有人連接過來(監(jiān)聽)Proto 代表使用的是什么協(xié)議
Recv-Q 接受數(shù)據(jù)的隊(duì)列里有多少數(shù)據(jù) receive queue ? -->表示我們的應(yīng)用程序還沒有處理的數(shù)據(jù)
Send-Q 發(fā)送數(shù)據(jù)的隊(duì)列里有多少數(shù)據(jù) ?Send queue ? ?-->我們發(fā)生出去的數(shù)據(jù),遠(yuǎn)程主機(jī)沒有確認(rèn)的數(shù)據(jù)Local Address 表示本地的IP地址和端口號
Foreign Address 表示外部機(jī)器的IP地址和端口號
State? 代表現(xiàn)在連接處于什么狀態(tài)
PID/Program PID代表進(jìn)程號 Program代表程序名字
0.0.0.0 ?代表任意ip地址
127.0.0.1:35 ?代表只是在127.0.0.1這個ip上開放35號端口 ?--》代表只能自己訪問
0.0.0.0:* ? 代表任意ip地址和任意端口號 *代表任意端口號
殺死進(jìn)程:
[root@sc-server ~]# kill -9 1677 強(qiáng)制殺死pid是1677進(jìn)程
-9 表示告訴linux內(nèi)核強(qiáng)制殺死某個進(jìn)程
9) SIGKILL 本質(zhì)上屬于一種信號signal kill
狀態(tài)解釋:?
Recv-Q
Established: The count of bytes not copied by the user program connected to this socket. Listening: Since
Kernel 2.6.18 this column contains the current syn backlog.
Send-Q
Established: The count of bytes not acknowledged by the remote host. Listening: Since Kernel 2.6.18 this col‐
umn contains the maximum size of the syn backlog.
Local Address
Address and port number of the local end of the socket. Unless the --numeric (-n) option is specified, the
socket address is resolved to its canonical host name (FQDN), and the port number is translated into the cor‐
responding service name.
Foreign Address
Address and port number of the remote end of the socket. Analogous to "Local Address."
State
The state of the socket. Since there are no states in raw mode and usually no states used in UDP and UDPLite,
this column may be left blank. Normally this can be one of several values:
ESTABLISHED
The socket has an established connection.
SYN_SENT
The socket is actively attempting to establish a connection.
SYN_RECV
A connection request has been received from the network.
FIN_WAIT1
The socket is closed, and the connection is shutting down.
FIN_WAIT2
Connection is closed, and the socket is waiting for a shutdown from the remote end.
TIME_WAIT
The socket is waiting after close to handle packets still in the network.
Recv-Q
Established:連接到此套接字的用戶程序未復(fù)制的字節(jié)數(shù)。聆聽:自
內(nèi)核2.6.18本列包含當(dāng)前的syn積壓。
Send-Q
Established:未被遠(yuǎn)程主機(jī)確認(rèn)的字節(jié)數(shù)。監(jiān)聽:自內(nèi)核2.6.18起,此參數(shù)為0
Umn包含syn積壓的最大大小。
Local Address
socket本端地址和端口號。除非指定了——numeric (-n)選項(xiàng)
socket地址被解析為它的規(guī)范主機(jī)名(FQDN),端口號被轉(zhuǎn)換成響應(yīng)的服務(wù)名稱。
Foreign Address
套接字遠(yuǎn)端地址和端口號。類似于“本地地址”。
State
socket的狀態(tài)。由于在原始模式下沒有狀態(tài),通常在UDP和UDPLite中沒有使用狀態(tài),
這一欄可以留空。通常,這可以是以下幾個值之一:
ESTABLISHED
socket已建立連接。
SYN_SENT
socket正在積極嘗試建立連接。
SYN_RECV
已從網(wǎng)絡(luò)接收到連接請求。
FIN_WAIT1
socket已關(guān)閉,連接正在關(guān)閉。
FIN_WAIT2
連接已關(guān)閉,socket正在等待遠(yuǎn)程端關(guān)閉。
TIME_WAIT
socket在關(guān)閉后等待處理仍在網(wǎng)絡(luò)中的數(shù)據(jù)包。
socket 槽、套接字(接口)
socket? = ip+port?--》接口
Socket(套接字)是計(jì)算機(jī)網(wǎng)絡(luò)編程中的一種編程接口,用于實(shí)現(xiàn)網(wǎng)絡(luò)通信
它提供了一種機(jī)制,使不同計(jì)算機(jī)之間或同一計(jì)算機(jī)上的不同進(jìn)程之間可以進(jìn)行數(shù)據(jù)交換和通信。
它基于網(wǎng)絡(luò)協(xié)議棧(如TCP/IP協(xié)議棧)實(shí)現(xiàn)了數(shù)據(jù)傳輸?shù)牡讓蛹?xì)節(jié)。
2、ss命令:
[root@sc-server ~]# ss -anptul
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 127.0.0.1:323 *:* users:(("chronyd",pid=693,fd=5))
udp UNCONN 0 0 [::1]:323 [::]:* users:(("chronyd",pid=693,fd=6))
tcp LISTEN 0 128 *:23 *:* users:(("sshd",pid=967,fd=3))
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=1672,fd=8),("nginx",pid=1671,fd=8),("nginx",pid=1670,fd=8))
tcp LISTEN 0 128 *:8080 *:* users:(("nginx",pid=1672,fd=7),("nginx",pid=1671,fd=7),("nginx",pid=1670,fd=7))
tcp LISTEN 0 128 [::]:23 [::]:* users:(("sshd",pid=967,fd=4))
tcp LISTEN 0 128 [::]:80 [::]:* users:(("nginx",pid=1672,fd=9),("nginx",pid=1671,fd=9),("nginx",pid=1670,fd=9))
[root@sc-server ~]#
3、lsof命令
查看單個端口是否被占據(jù)了:使用lsof命令:
下載lsof:
[root@sc-server ~]# yum install lsof net-tools -y
?lsof的使用:
[root@sc-server ~]# lsof -i:80 查看80端口被那個進(jìn)程占用了
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 1670 root 8u IPv4 21644 0t0 TCP *:http (LISTEN)
nginx 1670 root 9u IPv6 21645 0t0 TCP *:http (LISTEN)
nginx 1671 nginx 8u IPv4 21644 0t0 TCP *:http (LISTEN)
nginx 1671 nginx 9u IPv6 21645 0t0 TCP *:http (LISTEN)
nginx 1672 nginx 8u IPv4 21644 0t0 TCP *:http (LISTEN)
nginx 1672 nginx 9u IPv6 21645 0t0 TCP *:http (LISTEN)
[root@sc-server ~]# lsof -i:22
[root@sc-server ~]# lsof -i:23
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 967 root 3u IPv4 20037 0t0 TCP *:telnet (LISTEN)
sshd 967 root 4u IPv6 20039 0t0 TCP *:telnet (LISTEN)
sshd 3845 root 3u IPv4 72900 0t0 TCP sc-server:telnet->192.168.50.9:60013 (ESTABLISHED)
[root@sc-server ~]#
如何查看其他機(jī)器開放了哪些端口?
nc、nmap命令
如何知道這個ip地址175.6.49.131(不是本機(jī))對應(yīng)的主機(jī)開放了哪些端口?
下載nc 和 namp:
[root@sc-server ~]# yum install nc nmap -y
nc命令
nc(netcat)是一個強(qiáng)大的網(wǎng)絡(luò)工具,可以在命令行下進(jìn)行網(wǎng)絡(luò)連接、數(shù)據(jù)傳輸和端口掃描等操作
連接到指定主機(jī)和端口:
nc <host> <port>
示例:連接到主機(jī) example.com 的 80 端口
nc example.com 80
等待連接并監(jiān)聽指定端口:
nc -l <port>
示例:監(jiān)聽本地主機(jī)的 12345 端口
nc -l 12345
發(fā)送文件:
nc -w <timeout> <host> <port> < <file>
示例:將本地文件 example.txt 發(fā)送到遠(yuǎn)程主機(jī)的 12345 端口
nc -w 3 example.com 12345 < example.txt
接收文件:
nc -l <port> > <file>
示例:接收來自遠(yuǎn)程主機(jī)的文件并保存為 example.txt
nc -l 12345 > example.txt
端口掃描:
nc -zv <host> <start-port>-<end-port>
示例:掃描 example.com 主機(jī)的 1 到 1000 端口
nc -zv example.com 1-1000
?-z 表示沒有返回值,但是我們可以通過echo $?來判斷該機(jī)器的端口是否打開了
nmap命令
nmap 會對某個ip地址進(jìn)行常用端口號的掃描,一個一個端口去試是否開放 0~1024+常用端口(對該臺機(jī)器的端口進(jìn)行遍歷查詢)
端口號的范圍: 0~65535
[root@sc-server ~]# nmap 120.24.223.108
Starting Nmap 6.40 ( http://nmap.org ) at 2023-07-19 15:17 CST
Nmap scan report for 120.24.223.108
Host is up (0.025s latency).
Not shown: 995 filtered ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
443/tcp open https
3306/tcp open mysql
3389/tcp closed ms-wbt-server
Nmap done: 1 IP address (1 host up) scanned in 9.44 seconds
[root@sc-server ~]#
TCP(Transmission Control Protocol) 傳輸控制協(xié)議
可靠的、面向連接的協(xié)議
傳輸效率低
UDP(User Datagram Protocol) 用戶數(shù)據(jù)報協(xié)議
不可靠的、無連接的服務(wù)
傳輸效率高
TCP的封裝格式
源端口號:發(fā)送TCP進(jìn)程對應(yīng)的端口號
目標(biāo)端口號:目標(biāo)端接收進(jìn)程的端口號
32位序列號:0 ~ 2^32-1范圍內(nèi),數(shù)據(jù)段標(biāo)記,用于到目的端對到達(dá)包的重組
32位確認(rèn)號:0 ~ 2^32-1范圍內(nèi),對發(fā)送端的確認(rèn)信息,告訴發(fā)送端這個序號之前的數(shù)據(jù)段都收到了
6個控制位:(標(biāo)志位:作用就是表達(dá)某個意思,雙方溝通的時候使用)
????????U R G:緊急指針有效位,與16位緊急指針配合使用(緊急指針位)
????????A C K:確認(rèn)序列號有效位,表明該數(shù)據(jù)包包含確認(rèn)信息(確認(rèn)位)
????????P S H:通知接收端立即將數(shù)據(jù)提交給用戶進(jìn)程,不在緩存中停留,等待更多的數(shù)據(jù)(push上推位)
????????R S T:為1時,請求重新建立TCP連接(重置連接位)
????????S Y N:為1時,請求建立連接(同步序列號位)
????????F? I?N:為1時,數(shù)據(jù)發(fā)送完畢,請求斷開連接(結(jié)束位、終止位)
16位窗口大小:滑動窗口的大小,指明本地可接收數(shù)據(jù)的字節(jié)數(shù)
16位校驗(yàn)和:用于驗(yàn)證數(shù)據(jù)的完整性和檢測傳輸過程中是否出現(xiàn)錯誤
16位緊急指針:與控制位U R G有關(guān)
TCP頭部:20個字節(jié)
IP頭部:20個字節(jié)
幀頭部:18個字節(jié)
MTU:最大字節(jié)數(shù)為1500(包含了頭部信息)
數(shù)據(jù):1500-20-20=1460字節(jié)
抓包(TCP包)?
數(shù)據(jù)傳輸?shù)倪^程:
NIC 網(wǎng)卡(network interface card)
DMA 直接內(nèi)存訪問(directly memory access):直接將網(wǎng)卡存儲芯片里的數(shù)據(jù)復(fù)制到內(nèi)存里?
UDP的封裝格式:
16位源端口號:發(fā)送端的UDP進(jìn)程端口號?
16位目標(biāo)端口號:接收端的UDP進(jìn)程端口號
16位UDP長度:包含數(shù)據(jù)的長度,可以算出數(shù)據(jù)的結(jié)束位置文章來源:http://www.zghlxwxcb.cn/news/detail-608216.html
16位UDP校驗(yàn)和:UDP的差錯控制(可選)文章來源地址http://www.zghlxwxcb.cn/news/detail-608216.html
到了這里,關(guān)于計(jì)算機(jī)網(wǎng)絡(luò) day11 tcpdump - 傳輸層 - netstat - socket - nc - TCP/UDP頭部的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!