您好,登錄后才能下訂單哦!
一、網絡的意義
時至今日,互聯網已經成了人類發展中最重要的資源,在人類還沒有足夠充分的準備來迎接網絡時代的來臨時,它就已經成了我們生活中與一切行動緊密相關的內容。我們已無法離開網絡,就象生活少不了吃飯一樣。雖然沒有網絡時代的人也存在了上萬年,創造出了種種偉大的文明和輝煌成就,但是人的欲望和智慧總是相依相伴的,欲望一旦促使智慧得到開啟后,會變得一發不可收拾,無法再回頭。
隨著網絡的發展和壯大。各互聯網公司對網絡的安全,可靠要求也越來越大。網絡管理已經成為我們必須熟悉掌握的一項基本技能。
二、Centos 6的網絡管理(以太網)
centos 6網絡接口的命名方式:eth[0,1,2],centos 6的網路接口命名根據mac地址來識別,第一個識別到的網卡命名為eth0,第二個識別的為eth2,以此類推。這種情況可能會導致當你在eth0配置好了網絡配置文件后。當eth0這塊網卡壞掉了。我們買了一塊新的網卡還是插回原來的槽中,但是網卡名字已經更改為eth2。這會導致之前的網絡配置文件無法使用。這個情況在centos 7上得到了改善,這個我們第三章再做說明。
ifconfig命令(立即生效,但是重啟系統后即失效)
ifconfig [interface]
# ifconfig -a
[root@localhost ~]# ifconfig -a eth0 Link encap:Ethernet HWaddr 08:00:27:78:CB:FC inet addr:192.168.0.114 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe78:cbfc/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:189 errors:0 dropped:0 overruns:0 frame:0 TX packets:308 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:20800 (20.3 KiB) TX bytes:53808 (52.5 KiB) Base address:0xd010 Memory:f0000000-f0020000 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:16436 Metric:1 RX packets:8 errors:0 dropped:0 overruns:0 frame:0 TX packets:8 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:560 (560.0 b) TX bytes:560 (560.0 b) #顯示所有網卡信息
# ifconfig IFACE [up|down]
#禁用或啟用指定的網卡eth0 [root@localhost ~]# ifconfig eth0 down [root@localhost ~]# ifconfig eth0 up
ifconfig interface [aftype] options | address ...
# ifconfig IFACE IP/mask [up]
#為eth0配置IP地址,后面直接指定掩碼位數 [root@localhost ~]# ifconfig eth0 192.168.1.1/24
# ifconfig IFACE IP netmask MASK
#為eth0配置IP地址,后面直接輸入netmask+掩碼 [root@localhost ~]# ifconfig eth0 192.168.1.1 netmask 255.255.255.0
route命令(路由管理命令)
查看:route -n
[root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
添加:route add
route add [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]
#指定去往192.168.0.88的目標主機網關為192.168.0.1,經由eth0網卡轉發 [root@localhost ~]# route add -host 192.168.0.88 gw 192.168.0.1 dev eth0 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.88 192.168.0.1 255.255.255.255 UGH 0 0 0 eth0
其中Flags為路由標志,標記當前網絡節點的狀態:
U Up表示此路由當前為啟動狀態
H Host,表示此網關為一主機
G Gateway,表示此網關為一路由器
R Reinstate Route,使用動態路由重新初始化的路由
D Dynamically,此路由是動態性地寫入
M Modified,此路由是由路由守護程序或導向器動態修改
! 表示此路由當前為關閉狀態
#指定去往192.168.0.0的目標網絡網關為192.168.0.1,經由eth0網卡轉發 [root@localhost ~]# route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.0.1 dev eth0 [root@localhost ~]# route add -net 192.168.0.0/24 gw 192.168.0.1 dev eth0 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 192.168.0.1 255.255.255.0 UG 0 0 0 eth0
#指定網關為192.168.0.1的默認路由,默認路由只需要添加一條,添加多條以第一條為準 [root@localhost ~]# route add -net 0.0.0.0/32 gw 192.168.0.1 [root@localhost ~]# route add default gw 192.168.0.1 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
刪除:route del
route del [-net|-host] target [gw Gw] [netmask Nm] [[dev] If]
#刪除主機路由 [root@localhost ~]# route del -host 192.168.0.88 #刪除網絡路由 [root@localhost ~]# route del -net 192.168.0.0 netmask 255.255.255.0 #刪除默認路由 [root@localhost ~]# route del default
編輯:/etc/resolv.conf
nameserver DNS_SERVER_IP1(nameserver 8.8.8.8)
nameserver DNS_SERVER_IP2
nameserver DNS_SERVER_IP3
FQDN:(Fully Qualified Domain Name)完全合格域名/全稱域名,是指主機名加上全路徑,全路徑中列出了序列中所有域成員。全域名可以從邏輯上準確地表示出主機在什么地方,也可以說全域名是主機名的一種完全表示形式。從全域名中包含的信息可以看出主機在域名樹中的位置。DNS解析流程:首先查找本機HOSTS表,有的直接使用表中定義,沒有查找網絡連接中設置的DNS 服務器由他來解析。
正解:FQDN-->IP(www.google.com-->216.58.221.36)
# dig -t A FQDN
[root@localhost ~]# dig -t A www.google.com ; <<>> DiG 9.3.4-P1 <<>> -t A www.google.com ;; global options: printcmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 38677 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 4 ;; QUESTION SECTION(這里是我們疑問的問題): ;www.google.com. IN A ;; ANSWER SECTION(這里是我們回答我們的問題): www.google.com. 370 IN A 216.58.221.36 ;; AUTHORITY SECTION: google.com. 11188 IN NS ns2.google.com. google.com. 11188 IN NS ns1.google.com. google.com. 11188 IN NS ns4.google.com. google.com. 11188 IN NS ns3.google.com. ;; ADDITIONAL SECTION: ns1.google.com. 30626 IN A 216.239.32.10 ns2.google.com. 12121 IN A 216.239.34.10 ns3.google.com. 21582 IN A 216.239.36.10 ns4.google.com. 10738 IN A 216.239.38.10 ;; Query time: 12 msec ;; SERVER: 8.8.8.8#53(8.8.8.8)(這里是我們指定為我們做解析的的DNS服務器) ;; WHEN: Thu Sep 3 17:27:02 2015 ;; MSG SIZE rcvd: 184
# host -t A FQDN
[root@localhost ~]# host -t A www.google.com www.google.com has address 216.58.221.36
反解:IP-->FQDN(216.58.221.36-->www.google.com)
# dig -x IP
[root@localhost ~]# dig -x 216.58.221.36 ; <<>> DiG 9.3.4-P1 <<>> -x 216.58.221.36 ;; global options: printcmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 46451 ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 4, ADDITIONAL: 4 ;; QUESTION SECTION: ;36.221.58.216.in-addr.arpa. IN PTR ;; ANSWER SECTION: 36.221.58.216.in-addr.arpa. 281 IN PTR hkg08s13-in-f4.1e100.net. 36.221.58.216.in-addr.arpa. 281 IN PTR hkg08s13-in-f36.1e100.net. ;; AUTHORITY SECTION: 221.58.216.in-addr.arpa. 38624 IN NS ns1.google.com. 221.58.216.in-addr.arpa. 38624 IN NS ns3.google.com. 221.58.216.in-addr.arpa. 38624 IN NS ns2.google.com. 221.58.216.in-addr.arpa. 38624 IN NS ns4.google.com. ;; ADDITIONAL SECTION: ns1.google.com. 30015 IN A 216.239.32.10 ns2.google.com. 11510 IN A 216.239.34.10 ns3.google.com. 20971 IN A 216.239.36.10 ns4.google.com. 10127 IN A 216.239.38.10 ;; Query time: 13 msec ;; SERVER: 8.8.8.8#53(8.8.8.8) ;; WHEN: Thu Sep 3 17:37:14 2015 ;; MSG SIZE rcvd: 258
# host -t PTR IP
[root@localhost ~]# host -t PTR 216.58.221.36 36.221.58.216.in-addr.arpa domain name pointer hkg08s13-in-f36.1e100.net. 36.221.58.216.in-addr.arpa domain name pointer hkg08s13-in-f4.1e100.net
netstat命令:
netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
顯示網絡連接:
netstat [--tcp|-t] [--udp|-u] [--raw|-w] [--listening|-l] [--all|-a] [--numeric|-n] [--extend|-e[--extend|-e]] [--program|-p]
-t: tcp協議相關
-u: udp協議相關
-w: raw socket相關
-l: 處于監聽狀態
-a: 所有狀態
-n: 以數字顯示IP和端口;
-e:擴展格式
-p: 顯示相關進程及PID
常用組合:
-tan, -uan, -tnl, -unl
#顯示tcp協議相關的所有連接狀態信息,以數字顯示IP和端口 [root@localhost ~]# netstat -tan Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:833 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 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 127.0.0.1:6010 0.0.0.0:* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:6010 :::* LISTEN tcp 0 0 ::ffff:192.168.0.114:22 ::ffff:192.168.0.107:62362 ESTABLISHED
#顯示tcp協議相關的所有連接狀態信息的相關進程及PID,以數字顯示IP和端口 [root@localhost ~]# netstat -tanp 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:833 0.0.0.0:* LISTEN 1923/rpc.statd tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1894/portmap tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 2182/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2206/sendmail: acce tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 2508/0 tcp 0 0 :::22 :::* LISTEN 2167/sshd tcp 0 0 ::1:6010 :::* LISTEN 2508/0 tcp 0 0 ::ffff:192.168.0.114:22 ::ffff:192.168.0.107:62362 ESTABLISHED 2508/0
顯示路由表:
netstat {--route|-r} [--numeric|-n]
-r: 顯示內核路由表
-n: 數字格式
[root@localhost ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
顯示接口統計數據:
netstat {--interfaces|-I|-i} [iface] [--all|-a] [--extend|-e] [--program|-p] [--numeric|-n]
-i:顯示所有接口的統計數據
[root@localhost ~]# 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 8234 0 0 0 6545 0 0 0 BMRU lo 16436 0 34 0 0 0 34 0 0 0 LRU
-I IFACE:顯示IFACE接口的統計數據(-I接口,參數和接口之間不用空格,直接連著打)
[root@localhost ~]# netstat -Ieth0 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 8265 0 0 0 6559 0 0 0 BMRU
ip命令:
ip - show / manipulate routing, devices, policy routing and tunnels
ip [ OPTIONS ] OBJECT { COMMAND | help }
OBJECT := { link | addr | route }
link OBJECT:
ip link - network device configuration
set dev IFACE up:禁用指定接口
set dev IFACE down:禁用指定接口
[root@localhost ~]# ip link set dev eth0 down [root@localhost ~]# ip link set dev eth0 up
show
[dev IFACE]:指定接口
[root@localhost ~]# ip link show eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 08:00:27:4d:f1:47 brd ff:ff:ff:ff:ff:ff
ip addr - protocol address management
ip addr { add | del } IFADDR dev STRING
[label LABEL]:添加地址時指明網卡別名
[scope {global(全局可用)|link(僅鏈接可用)|host(本機可用)}]:指明作用域
[broadcast ADDRESS]:指明廣播地址
#add為添加多一個新IP地址 [root@localhost ~]# ip addr add 192.168.0.222/24 label eth0:0 scope global dev eth0 [root@localhost ~]# ip addr show eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 08:00:27:78:cb:fc brd ff:ff:ff:ff:ff:ff inet 192.168.0.114/24 brd 192.168.0.255 scope global eth0 inet 192.168.0.222/24 scope global secondary eth0:0 inet6 fe80::a00:27ff:fe78:cbfc/64 scope link valid_lft forever preferred_lft forever
ip addr show - look at protocol addresses
[dev DEVICE]
[label PATTERN]
[root@localhost ~]# ip addr show eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 08:00:27:78:cb:fc brd ff:ff:ff:ff:ff:ff inet 192.168.0.114/24 brd 192.168.0.255 scope global eth0 inet 192.168.0.222/24 scope global secondary eth0:0 inet6 fe80::a00:27ff:fe78:cbfc/64 scope link valid_lft forever preferred_lft forever
ip addr flush - flush protocol addresses
[dev DEVICE]:清空指定設備的所有IP地址,慎用,一執行所有IP地址都清空
ip route - routing table management
添加路由:ip route add TARGET via GW dev IFACE src SOURCE_IP
TARGET:主機路由直接輸入IP。網絡路由:NETWORK/MASK,地址加掩碼
[root@localhost ~]# ip route add 192.168.0.88 via 192.168.0.1 [root@localhost ~]# ip route show 192.168.0.88 via 192.168.0.1 dev eth0
添加網關:ip route add defalt via GW dev IFACE
ip route delete
刪除路由:ip route del TARGET
[root@localhost ~]# ip route del 192.168.0.99
ip route show
[root@localhost ~]# ip route show 192.168.0.88 via 192.168.0.1 dev eth0
ip route flush
dev IFACE:清空指定設備的所有路由條目
ss和netstat都是用來查看網絡狀態的。但是在連接數異常多的時候ss的性能會比netstat快幾倍。所以在連接數很多的時候建議使用ss
ss查看網絡狀態工具命令:
格式:ss [OPTION]... [FILTER]
選項:
-t: tcp協議相關
-u: udp協議相關
-w: 裸套接字相關
-x:unix sock相關
-l: listen狀態的連接
-a: 所有
-n: 數字格式
-p: 相關的程序及PID
-e: 擴展的信息
-m:內存用量
-o:計時器信息
FILTER := [ state TCP-STATE ] [ EXPRESSION ]
TCP的常見狀態:
tcp finite state machine:
LISTEN: 監聽
ESTABLISHED:已建立的連接
FIN_WAIT_1
FIN_WAIT_2
SYN_SENT
SYN_RECV
CLOSED
EXPRESSION:
dport =
sport =
示例:’( dport = :ssh or sport = :ssh )’
常用組合:
-tan, -tanl, -tanlp, -uan
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。