1、網絡的意義centos
時至今日,互聯網已經成了人類發展中最重要的資源,在人類尚未足夠充分的準備來迎接網絡時代的來臨時,它就已經成了咱們生活中與一切行動緊密相關的內容。咱們已沒法離開網絡,就象生活少不了吃飯同樣。雖然沒有網絡時代的人也存在了上萬年,創造出了種種偉大的文明和輝煌成就,可是人的慾望和智慧老是相依相伴的,慾望一旦促使智慧獲得開啓後,會變得一發不可收拾,沒法再回頭。安全
隨着網絡的發展和壯大。各互聯網公司對網絡的安全,可靠要求也愈來愈大。網絡管理已經成爲咱們必須熟悉掌握的一項基本技能。
bash
2、Centos 6的網絡管理(以太網)服務器
centos 6網絡接口的命名方式:eth[0,1,2],centos 6的網路接口命名根據mac地址來識別,第一個識別到的網卡命名爲eth0,第二個識別的爲eth1,以此類推。這種狀況可能會致使當你在eth0配置好了網絡配置文件後。當eth0這塊網卡壞掉了。咱們買了一塊新的網卡仍是插回原來的槽中,可是網卡名字已經更改成eth1。這會致使以前的網絡配置文件沒法使用。這個狀況在centos 7上獲得了改善,這個咱們第三章再作說明。網絡
ifconfig命令(當即生效,可是重啓系統後即失效)dom
ifconfig [interface]ssh
# ifconfig -asocket
[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]tcp
#禁用或啓用指定的網卡eth0 [root@localhost ~]# ifconfig eth0 down [root@localhost ~]# ifconfig eth0 up
ifconfig interface [aftype] options | address ...ide
# 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
DNS服務器指定:
編輯:/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