一伙狐、簡述osi七層模型和TCP/IP五層模型
OSI七層模型:
(圖摘自華為官網(wǎng)HCNA教材)
簡述:(從下往上)
物理層:在設備之間傳輸比特流剥纷,規(guī)定了電平痹籍、速度和電纜針腳』扌————(數(shù)據(jù)形式:比特流)
數(shù)據(jù)鏈路層:將比特組合成字節(jié)蹲缠,再將字節(jié)組合成幀,使用鏈路層地址(以太網(wǎng)使用MAC地址)來訪問介質(zhì)悠垛,并進行差錯檢測线定。————(數(shù)據(jù)形式:數(shù)據(jù)幀)
網(wǎng)絡層:提供邏輯地址确买,供路由器確定路徑斤讥。 ————(數(shù)據(jù)形式:數(shù)據(jù)包)
傳輸層:提供面向連接或非面向連接的數(shù)據(jù)傳遞以及進行重傳前的差錯檢測∧赐铮————(數(shù)據(jù)形式:數(shù)據(jù)段)
會話層:負責建立周偎、管理和終止表示層實體之間的通信會話抹剩。該層的通信由不同設備中的應用程序之間的服務請求和響應組成撑帖。
表示層:提供各種用于應用層數(shù)據(jù)的編碼和轉(zhuǎn)換功能,確保一個系統(tǒng)的應用層發(fā)送的數(shù)據(jù)能被另一個系統(tǒng)的應用層識別澳眷。
應用層:OSI參考模型中最靠近用戶的一層胡嘿,為應用程序提供網(wǎng)絡服務。
TCP/IP五層模型:
數(shù)據(jù)的封裝過程:
應用數(shù)據(jù)需要經(jīng)過TCP/IP每一層處理之后才能通過網(wǎng)絡傳輸?shù)侥康亩饲唬恳粚由隙际褂迷搶拥膮f(xié)議數(shù)據(jù)單元PDU(Protocol Data Unit)彼此交換信息衷敌。不同層的PDU中包含有不同的信息勿侯,因此PDU在不同層被賦予了不同的名稱。
上層數(shù)據(jù)在傳輸層添加TCP報頭后得到的PDU被稱為Segment(數(shù)據(jù)段 )缴罗;
數(shù)據(jù)段被傳遞給網(wǎng)絡層助琐,網(wǎng)絡層添加IP報頭得到的PDU被稱為Packet(數(shù)據(jù)包);
數(shù)據(jù)包被傳遞到數(shù)據(jù)鏈路層面氓,封裝數(shù)據(jù)鏈路層報頭得到的PDU被稱為Frame(數(shù)據(jù)幀)兵钮;
最后,幀被轉(zhuǎn)換為比特舌界,通過網(wǎng)絡介質(zhì)傳輸掘譬。
OSI七層模型和TCP/IP五層模型的對應關系
二、簡述iproute家族命令
ip命令
ip [ OPTIONS ] OBJECT { COMMAND | help }
OBJECT = { link | addr | route | netns }
注:OBJECT的命令可簡寫呻拌,其子命令也可簡寫葱轩。
OBJECT = { link | addr | route | netns }
ip link
ip link show - display device attributes
ip link help - 顯示簡要使用幫助;
ip link set - change device attributes
dev NAME(default):指明要管理的設備藐握,dev關鍵字可省略靴拱;
up和down:啟用和禁用接口
multicast on或multicast off:啟用或禁用多播功能;
name NAME:重命名接口
mtu NUMBER:設置MTU的大小猾普,默認為1500缭嫡;
netns PID:ns為namespace,用于將接口移動到指定的網(wǎng)絡名稱空間抬闷;
示例:
1妇蛀、查看當前所有網(wǎng)絡連接屬性
[root@centos7u2 ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:d6:cb:13 brd ff:ff:ff:ff:ff:ff
3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:d6:cb:1d brd ff:ff:ff:ff:ff:ff
2、禁用網(wǎng)絡連接及重命名連接:
[root@centos7u2 ~]# ip link set dev eno33554960 name eth1
RTNETLINK answers: Device or resource busy
[root@centos7u2 ~]# ip link set down dev eno33554960
[root@centos7u2 ~]# ip link set dev eno33554960 name eth1
[root@centos7u2 ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:d6:cb:13 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 1000
link/ether 00:0c:29:d6:cb:1d brd ff:ff:ff:ff:ff:ff
ip netns
ip netns:ip - manage network namespaces
ip netns list:列出所有的netns
ip netns add NAME:創(chuàng)建指定的netns
ip netns del NAME:刪除指定的netns
ip netns exec NAME COMMAND:在指定的netns中運行命令
示例:
1笤成、添加并查看網(wǎng)絡名稱空間
[root@centos7u2 ~]# ip netns add ns1
[root@centos7u2 ~]# ip netns add ns2
[root@centos7u2 ~]# ip netns list
ns2
ns1
2评架、管理指定網(wǎng)絡名稱空間中的網(wǎng)絡連接
[root@centos7u2 ~]# ip netns exec ns1 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
[root@centos7u2 ~]# ip link set eth1 netns ns2
[root@centos7u2 ~]# ip netns exec ns2 ip link set up eth1
[root@centos7u2 ~]# ip netns exec ns2 ip addr
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d6:cb:1d brd ff:ff:ff:ff:ff:ff
inet6 fe80::20c:29ff:fed6:cb1d/64 scope link
valid_lft forever preferred_lft forever
ip addr
ip address - protocol address management
ip address add IP/MASK dev IFACE
[label NAME]:為額外添加的地址指明接口別名;
[broadcast ADDRESS]:廣播地址炕泳;會根據(jù)IP和netmask自動計算得到纵诞;
[scope SCORE_VALUE]:
global:全局可用;
link:接口可用培遵;
host:僅本機可用浙芙;
ip address delete IP/MASK dev IFACE
ip address list [IFACE]:顯示接口的地址;
ip address flush dev IFACE
示例:
1籽腕、查看指定的網(wǎng)絡連接的信息
[root@centos7u2 ~]# ip addr list eno33554960
3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d6:cb:1d brd ff:ff:ff:ff:ff:ff
inet 192.168.200.142/24 brd 192.168.200.255 scope global dynamic eno33554960
valid_lft 1714sec preferred_lft 1714sec
inet6 fe80::20c:29ff:fed6:cb1d/64 scope link
valid_lft forever preferred_lft forever
2嗡呼、修改指定網(wǎng)絡連接的IP地址
[root@centos7u2 ~]# ip addr delete 192.168.200.142/24 dev eno33554960
[root@centos7u2 ~]# ip addr list eno33554960
3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d6:cb:1d brd ff:ff:ff:ff:ff:ff
inet6 fe80::20c:29ff:fed6:cb1d/64 scope link
valid_lft forever preferred_lft forever
[root@centos7u2 ~]# ip addr add 192.168.200.200/24 dev eno33554960
[root@centos7u2 ~]# ip addr list eno33554960
3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:d6:cb:1d brd ff:ff:ff:ff:ff:ff
inet 192.168.200.200/24 scope global eno33554960
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed6:cb1d/64 scope link
valid_lft forever preferred_lft forever
ip route
ip route - routing tables management
ip route add - add new route
ip route add TYPE/PREFIX via <NEXTHOP> [dev IFACE] [src SOURCE_IP]
ip route change - change route
ip route replace - change or add new one
ip route delete - delete route
ip route show - list routes
ip route flush - flush routing tables
ip route get - get a single route
示例:
1、添加一條路由條目
[root@centos7u2 ~]# ip route add 192.168.100.0/24 via 192.168.200.2 dev eno33554960 src 192.168.200.200
[root@centos7u2 ~]# ip route show
default via 192.168.2.1 dev eno16777736 proto static metric 100
192.168.2.0/24 dev eno16777736 proto kernel scope link src 192.168.2.104 metric 100
192.168.100.0/24 via 192.168.200.2 dev eno33554960 src 192.168.200.200
192.168.200.0/24 dev eno33554960 proto kernel scope link src 192.168.200.200
ss命令
同netstat作用相同皇耗,但功能更為強大和高效南窗,故可用來替換netstat
ss [options] [ FILTER ]
options:
-t:TCP協(xié)議的相關連接
-u:UDP協(xié)議的相關連接
-w:raw socket相關的連接
-l:監(jiān)聽的連接
-a:所有狀態(tài)
-n:以數(shù)字格式顯示IP和Port
-e:擴展格式
-p:顯示相關的進程及PID
-m:內(nèi)存用量
-o:計時器信息
FILTER:= [state TCP-STATE] [EXPRESSION]
TCP的常見狀態(tài):
TCP FSM:
LISTEN:監(jiān)聽
ESTABLISHED:建立的連接
FIN_WAIT_1:
FIN_WAIT_2:
SYN_SENT:
SYN_RECV:
CLOSED:
EXPRESSION:
dport=
sport=
示例:
1、查看當前系統(tǒng)偵聽了哪些端口
[root@centos7u2 ~]# ss -tunlp
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 *:59355 *:* users:(("dhclient",pid=5047,fd=20))
udp UNCONN 0 0 *:68 *:* users:(("dhclient",pid=5047,fd=6))
udp UNCONN 0 0 *:68 *:* users:(("dhclient",pid=4162,fd=6))
udp UNCONN 0 0 *:37808 *:* users:(("dhclient",pid=4162,fd=20))
udp UNCONN 0 0 :::26566 :::* users:(("dhclient",pid=4162,fd=21))
udp UNCONN 0 0 :::30106 :::* users:(("dhclient",pid=5047,fd=21))
tcp LISTEN 0 128 *:22 *:* users:(("sshd",pid=1791,fd=3))
tcp LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=2940,fd=13))
tcp LISTEN 0 128 :::80 :::* users:(("httpd",pid=2958,fd=4),("httpd",pid=2957,fd=4),("httpd",pid=2956,fd=4),("httpd",pid=2955,fd=4),("httpd",pid=2954,fd=4),("httpd",pid=1792,fd=4))
tcp LISTEN 0 128 :::22 :::* users:(("sshd",pid=1791,fd=4))
tcp LISTEN 0 100 ::1:25 :::* users:(("master",pid=2940,fd=14))
tcp LISTEN 0 128 :::9090 :::* users:(("systemd",pid=1,fd=36))
2、查看指定的源端口或目的端口的偵聽狀態(tài)
[root@centos7u2 ~]# ss -tan '( dport = :22 or sport = :22 )'
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
ESTAB 0 52 192.168.2.104:22 192.168.2.103:49368
ESTAB 0 136 192.168.2.104:22 192.168.2.103:65250
LISTEN 0 128 :::22 :::*
3万伤、查看處于指定的TCP協(xié)議狀態(tài)的端口的相關信息
[root@centos7u2 ~]# ss -tan state ESTABLISHED
Recv-Q Send-Q Local Address:Port Peer Address:Port
0 52 192.168.2.104:22 192.168.2.103:49368
0 136 192.168.2.104:22 192.168.2.103:65250
三窒悔、詳細說明進程管理工具htop、vmstat等相關命令敌买,并舉例
htop命令
選項:
-d #:指定延遲時間間隔简珠;
-u UserName:僅顯示指定用戶的進程;
-s COLUME:以指定字段進行排序虹钮;
子命令:
l:列出選定的進程所占用的文件列表北救;(需要lsof命令)
s:跟蹤選定的進程的系統(tǒng)調(diào)用;(需要trace命令)
t:以層級關系顯示各進程狀態(tài)芜抒;
a:將選定的進程綁定至指定的CPU核心珍策;
[root@centos7u2 ~]# htop
直接使用htop命令,進入進程管理視圖宅倒,可動態(tài)展示當前系統(tǒng)的進程及資源狀態(tài)攘宙;該界面可使用鼠標點擊操作:
按F1可獲取相關操作的幫助
常用子命令:
l:列出選定的進程所打開的文件列表
s:跟蹤選定的進程的系統(tǒng)調(diào)用
t:以層級關系顯示各進程狀態(tài)
a:將選定的進程綁定至指定的CPU核心
vmstat命令
vmstat - Report virtual memory statistics
vmstat [options] [delay [count]]
Options:
-a, --active active/inactive memory
-f, --forks number of forks since boot
-m, --slabs slabinfo
-n, --one-header do not redisplay header
-s, --stats event counter statistics
-d, --disk disk statistics
-D, --disk-sum summarize disk statistics
-p, --partition <dev> partition specific statistics
-S, --unit <char> define display unit
-w, --wide wide output
-t, --timestamp show timestamp
狀態(tài)信息說明:
procs
r:等待運行的進程的個數(shù);CPU上等待運行的任務的隊列長度拐迁;
b:處于不可中斷睡眠態(tài)的進程個數(shù)蹭劈;被阻塞的任務隊列的長度;
memory
swpd:交換內(nèi)存使用總量线召;
free:空閑的物理內(nèi)存總量铺韧;
buffer:用于buffer的內(nèi)存總量;
cache:用于cache的內(nèi)存總量缓淹;
swap
si:數(shù)據(jù)進入swap中的數(shù)據(jù)速率(kb/s)
so:數(shù)據(jù)離開swap的速率(kb/s)
io
bi:從塊設備讀入數(shù)據(jù)到系統(tǒng)的速度(kb/s)
bo:保存數(shù)據(jù)至塊設備的速率(kb/s)
system
in:interrupts哈打,中斷速率
cs:context switch,上下文切換的速率
cpu
us:user space
sy:system space
id:idle
wa:wait
st:stolen
示例:
1讯壶、每隔2s讀取一次系統(tǒng)狀態(tài)料仗,總共讀取3次
[root@centos7u2 ~]# vmstat 2 3
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 0 3293188 3160 363404 0 0 11 13 36 57 0 0 99 1 0
1 0 0 3293296 3160 363404 0 0 0 7 105 174 0 0 97 3 0
0 0 0 3293296 3160 363404 0 0 0 7 94 145 0 0 100 0 0
[root@centos7u2 ~]#
2、查看當前磁盤IO狀態(tài)
[root@centos7u2 ~]# vmstat -d
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
sda 5750 15 440346 89485 28752 911 529907 221277 0 176
sr0 41 0 1848 952 0 0 0 0 0 0
dm-0 4651 0 380269 87044 29154 0 525787 233259 0 175
dm-1 184 0 3040 120 0 0 0 0 0 0
loop0 0 0 0 0 0 0 0 0 0 0
3伏蚊、查看內(nèi)存統(tǒng)計數(shù)據(jù)
[root@centos7u2 ~]# vmstat -s
3866948 K total memory
207236 K used memory
250156 K active memory
152388 K inactive memory
3293044 K free memory
3160 K buffer memory
363508 K swap cache
2097148 K total swap
0 K used swap
2097148 K free swap
807 non-nice user cpu ticks
2 nice user cpu ticks
4831 system cpu ticks
2172135 idle cpu ticks
13302 IO-wait cpu ticks
0 IRQ cpu ticks
70 softirq cpu ticks
0 stolen cpu ticks
221602 pages paged in
279446 pages paged out
0 pages swapped in
0 pages swapped out
787861 interrupts
1241506 CPU context switches
1538640948 boot time
6771 forks
glances命令
glances - A cross-platform curses-based system monitoring tool
常用選項:
-b:以Byte為單位顯示網(wǎng)上數(shù)據(jù)速率立轧;
-d:關閉磁盤I/O模塊;
-m:關閉mount模塊躏吊;
-n:關閉network模塊氛改;
-t #:刷新時間間隔;
-1:每個cpu的相關數(shù)據(jù)單獨顯示比伏;
-o {HTML|CSV}:輸出格式胜卤;
-f /PATH/TO/SOMEDIR:設定輸出文件的位置;
此時凳怨,鍵入子命令h即可進入幫助界面:
除以上使用外瑰艘,glances還支持C/S模式,即可獲取遠端服務器的狀態(tài)信息肤舞,使用方法如下:
C/S模式下運行glances命令:
服務模式:
glances -s -B IPADDR
IPADDR:本機的某地址紫新,用于監(jiān)聽;
客戶端模式:
glances -c IPADDR
IPADDR:遠程服務器的IP地址
dstat命令
融合了以上所有工具的特點
dstat - versatile tool for generating system resource statistics
dstat [-afv] [options..] [delay [count]]
選項:
-c李剖,--cpu:顯示cpu相關信息芒率;
-C#,#,...,total
-d,--disk:顯示磁盤的相關信息篙顺;
-D sda,sdb,...,total
-g:顯示page相關的速率數(shù)據(jù)偶芍;
-m:Memory的相關統(tǒng)計數(shù)據(jù);
-n:Interface的相關統(tǒng)計數(shù)據(jù)德玫;
-p:顯示process的相關統(tǒng)計數(shù)據(jù)匪蟀;
-r:顯示io請求的相關統(tǒng)計數(shù)據(jù);
-s:顯示swapped的相關統(tǒng)計數(shù)據(jù)宰僧;
--tcp
--udp
--raw
--socket
--ipc
--top-cpu:顯示最占用CPU的進程材彪;
--top-io:顯示最占用io的進程;
--top-mem:顯示最占用內(nèi)存的進程琴儿;
--top-latency:顯示延遲最高的進程段化;
示例:
1、動態(tài)展示當前系統(tǒng)狀態(tài)信息造成,默認選項:-cdngy
[root@centos7u2 ~]# dstat
You did not select any stats, using -cdngy by default.
----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai hiq siq| read writ| recv send| in out | int csw
0 0 99 1 0 0| 37k 48k| 0 0 | 0 0 | 148 229
0 0 100 0 0 0| 0 0 | 162B 948B| 0 0 | 77 128
0 0 100 0 0 0| 0 26k| 162B 420B| 0 0 | 119 186
0 0 100 0 0 0| 0 0 | 162B 420B| 0 0 | 74 128
0 1 99 0 0 0| 0 26k| 162B 420B| 0 0 | 196 297
0 0 96 3 0 0| 0 34k| 530B 494B| 0 0 | 215 336
0 0 100 0 0 0| 0 26k| 346B 420B| 0 0 | 136 216 ^C
[root@centos7u2 ~]#
2显熏、動態(tài)展示當前系統(tǒng)的各協(xié)議等狀態(tài)
[root@centos7u2 ~]# dstat --tcp --udp --raw --socket
----tcp-sockets---- --udp-- raw ------sockets------
lis act syn tim clo|lis act|raw|tot tcp udp raw frg
6 1 1 0 1| 6 0| 0|578 5 4 0 0
6 1 2 0 1| 6 0| 0|579 6 4 0 0
6 1 2 0 1| 6 0| 0|579 6 4 0 0
6 1 2 0 1| 6 0| 0|579 6 4 0 0
6 1 2 0 1| 6 0| 0|579 6 4 0 0
6 1 2 0 1| 6 0| 0|579 6 4 0 0^C
3、動態(tài)展示當前系統(tǒng)最占用相關資源的進程
[root@centos7u2 ~]# dstat --top-cpu --top-io --top-mem
-most-expensive- ----most-expensive---- --most-expensive-
cpu process | i/o process | memory process
360entclient 0.1|360entclien 74k 31k|360entclient32.0M
360entclient 0.2|360entclien 85k 128k|360entclient32.0M
rcuos/0 0.2|irqbalance 7362B 0 |360entclient32.0M
|360entclien7400B 6692B|360entclient32.0M
rcu_sched 0.2|sshd: root@ 155B 196B|360entclient32.0M
360entclient 0.2|360entclien7400B 7070B|360entclient32.0M
|sshd: root@ 162B 212B|360entclient32.0M
|360entclien7400B 6692B|360entclient32.0M
sshd: root@pt0.2|360entclien 62k 74B|360entclient32.0M
kworker/2:0 0.2|360entclien 19k 6692B|360entclient32.0Mq
|360entclien 39k 24k|360entclient32.0M
360entclient 0.2|360entclien7400B 6692B|360entclient32.0M
xfsaild/dm-0 0.2|irqbalance 7362B 0 |360entclient32.0M^C
四晒屎、使用until和while分別實現(xiàn)192.168.0.0/24網(wǎng)段內(nèi)喘蟆,地址是否能夠ping通,若ping通則輸出"success!"鼓鲁,若ping不通則輸出"fail!"
while方式:
[root@centos7u2 ~]# cat while.sh
#!/bin/bash
#
i=1
while [ $i -le 254 ];do
ping -c 2 192.168.0.$i &> /dev/null
if [ $? -eq 0 ];then
echo "success!"
else
echo "fail!"
fi
let i++
done
until方式:
[root@centos7u2 ~]# cat until.sh
#!/bin/bash
#
i=1
until [ $i -gt 254 ];do
ping -c 2 192.168.0.$i &> /dev/null
if [ $? -eq 0 ];then
echo "success!"
else
echo "fail!"
fi
let i++
done