【Linux】【Basis】網絡

Linux網絡屬性配置            
         
     計算機網絡: 
         TCP/IP:協議棧(使用)
             ISO,OSI:協議棧(學習)
             
         MAC:Media Access Control
             48bits:
                 ICANN:24bits, 2^24
                     地址塊:2^24
                     
                 網橋(bridge):MAC地址表
                     靜態指定:
                     動態學習:根據原地址學習;
                     
                 交換機(switch):多端口網橋; 
                     
         IP(Internet protocol)地址:網絡號+主機號
                 A<-->B
                     網絡?
                     主機?
                 
                 IPv4:32bits
                     8bits.8bits.8bits.8bits
                         0-255
                         0.0.0.0-255.255.255.255
                         
                     IP地址分類:
                         A類:
                             第一段爲網絡號,後三段爲主機號
                             網絡號:
                                 0 000 0000 - 0 111 1111:1-127
                             網絡數量:126,127
                             每一個網絡中的主機數量:2^24-2
                             默認子網掩碼:255.0.0.0,/8
                                 用於與IP地址按位進行「與」運算,從而取出其網絡地址;
                                     1.3.2.1/255.0.0.0 = 1.0.0.0
                                     1.3.2.1/255.255.0.0= 1.3.0.0   
                             私網地址:10.0.0.0/255.0.0.0
                         B類:
                             前兩段爲網絡號,後兩段爲主機號
                             網絡號:
                                 10 00 0000 - 10 11 1111:128-191
                                 網絡數:2^14
                                 每一個網絡中的主機數量:2^16-2
                                 默認子網掩碼:255.255.0.0,/16
                                 私網地址:172.16.0.0-172.31.0.0                             
                         C類:
                             前三段爲網絡號,最後一段爲主機號
                             網絡號:
                                 110 0 0000 - 110 1 1111:192-223
                             網絡數:2^21
                             每一個網絡中的主機數量:2^8-2
                             默認子網掩碼:255.255.255.0,  /24
                         D類:組播
                             1110 0000 - 1110 1111:224-239
                         E類:科研
                             240-255
                 IPv6:128bits
                 
                     路由器:router
                         路由表:
                             靜態指定
                             動態學習:rip2, ospf
                             
                         路由條目:
                             目標地址  下一跳(nexthop)
                                 目標地址的類別:
                                     主機:主機路由
                                     網絡:網絡路由
                                     0.0.0.0/0.0.0.0:默認路由                           
     OS:多用戶,多任務
         多任務:多進程
             chrome:
             QQ:
             QQ Music:
             
         通訊時,進程的數字標識:
             16bits:
                 0-65535:1-65535
                     1-1023:固定分配,並且只有管理員有權限啓用;
                     1024-4W:半固定,
                     4W+:臨時;
         進程地址:
             IP:PORT,  socket
             
     總結:
         MAC:本地通訊;範圍:本地局域網;
         IP:界定通訊主機,源和目標;範圍:互聯網;
         Port:界定進程;範圍:主機 ;
         
     將Linux主機接入到網絡中:
         IP/NETMASK:本地通訊
         路由(網關):跨網絡通訊
         DNS服務器地址:基於主機名的通訊
             主DNS服務器地址
             備用DNS服務器地址
             第三備份DNS服務器地址
             
         配置方式:
             靜態指定:
                 命令:
                     ifcfg家族:
                         ifconfig:配置IP,NETMASK
                         route:路由
                         netstat:狀態及統計數據查看
                     iproute2家族:
                         ip OBJECT:
                             addr:地址和掩碼;
                             link:接口
                             route:路由
                         ss:狀態及統計數據查看
                     CentOS 7:nm(Network Manager)家族
                         nmcli:命令行工具
                         nmtui:text window 工具
                         
                     注意:
                         (1) DNS服務器指定   
                             配置文件:/etc/resolv.conf
                         (2) 本地主機名配置
                             hostname
                             配置文件:/etc/sysconfig/network
                             CentOS 7:hostnamectl                   
                 配置文件:
                     RedHat及相關發行版
                         /etc/sysconfig/network-scripts/ifcfg-NETCARD_NAME
                     
             動態分配:依賴於本地網絡中有DHCP服務
                 DHCP:Dynamic Host Configure Procotol
                 
     網絡接口命名方式:
         傳統命名:
             以太網:ethX, [0,oo),例如eth0, eth1, ...
             PPP網絡:pppX, [0,...], 例如,ppp0, ppp1, ...
         
         可預測命名方案(CentOS):
             支持多種不一樣的命名機制:
                 Fireware, 拓撲結構
                 
             (1) 若是Firmware或BIOS爲主板上集成的設備提供的索引信息可用,則根據此索引進行命名,如eno1, eno2, ...
             (2) 若是Firmware或BIOS爲PCI-E擴展槽所提供的索引信息可用,且可預測,則根據此索引進行命名,如ens1, ens2, ...
             (3) 若是硬件接口的物理位置信息可用,則根據此信息命名,如enp2s0, ...
             (4) 若是用戶顯式定義,也可根據MAC地址命名,例如enx122161ab2e10, ...
             上述均不可用,則仍使用傳統方式命名;
             
             命名格式的組成:
                 en:ethernet
                 wl:wlan
                 ww:wwan
                 
                 名稱類型:
                     o<index>:集成設備的設備索引號;
                     s<slot>:擴展槽的索引號;
                     x<MAC>:基於MAC地址的命名;
                     p<bus>s<slot>:基於總線及槽的拓撲結構進行命名;
                     
回顧:計算機網絡基礎、Linux網絡屬性配置
     TCP/IP協議棧:物理層、互聯網層、傳輸層、應用層
         互聯網層:IP
         傳輸層:TCP, UDP
         應用層:http, https, ftp, ldap, ...
         
     連接路層:以太網幀
     互聯網層:IP報文
     
     以太網幀:MTU(1500)
         
     Linux網絡屬性配置:命令,配置文件;
         
Linux網絡屬性配置(2)
 
     ifcfg命令家族: ifconfig, route, netstat
         
         ifconfig命令:接口及地址查看和管理
             ifconfig  [INTERFACE]
                 # ifconfig -a:顯示全部接口,包括inactive狀態的接口;
             
             ifconfig interface [aftype] options | address ...
                 # ifconfig  IFACE  IP/MASK  [up|down]
                 # ifconfig  IFACE  IP  netmask  NETMASK 
                 
                 options:
                     [-]promisc
                 
                 注意:當即送往內核中的TCP/IP協議棧,並生效;
                 
             管理IPv6地址:
                 add addr/prefixlen
                 del  addr/prefixlen
                 
         route命令:路由查看及管理
         
             路由條目類型:
                 主機路由:目標地址爲單個IP;
                 網絡路由:目標地址爲IP網絡;
                 默認路由:目標爲任意網絡,0.0.0.0/0.0.0.0
                 
             查看:
                 # route  -n
             添加:
                 route  add  [-net|-host]  target  [netmask  Nm]  [gw GW]  [[dev] If]
                 
                     示例:route add -net  10.0.0.0/8  gw  192.168.10.1  dev  eth1
                                 route add  -net  0.0.0.0/0.0.0.0  gw 192.168.10.1 
                                 route add  default  gw 192.168.10.1 
                                 
             刪除:
                 route  del  [-net|-host] target  [gw Gw]  [netmask Nm]  [[dev] If]
                     
                     示例: route  del  -net  10.0.0.0/8  gw 192.168.10.1
                              route  del  default
                              
         netstat命令:
             Print network connections, routing tables, interface statistics, masquerade connections, and multicast  memberships
             
             顯示路由表:netstat  -rn
                 -r:顯示內核路由表
                 -n:數字格式
                 
             顯示網絡鏈接:
                 netstat  [--tcp|-t]  [--udp|-u]  [--udplite|-U]  [--sctp|-S]  [--raw|-w]  [--listening|-l]  [--all|-a]  [--numeric|-n]   [--extend|-e[--extend|-e]]  [--program|-p]
                     -t:TCP協議的相關鏈接,鏈接均有其狀態;FSM(Finate State Machine);
                     -u:UDP相關的鏈接
                     -w:raw socket相關的鏈接
                     -l:處於監聽狀態的鏈接
                     -a:全部狀態
                     -n:以數字格式顯示IP和Port;
                     -e:擴展格式
                     -p:顯示相關的進程及PID;
                     
                 經常使用組合:
                     -tan,  -uan,  -tnl,  -unl,  -tunlp
                                 
                 傳輸層協議:
                     tcp:面向鏈接的協議;通訊開始以前,要創建一個虛鏈路;通訊完成後還要拆除鏈接;
                     udp:無鏈接的協議;直接發送數據報文;
                     
             顯示接口的統計數據:
                 netstat    {--interfaces|-I|-i}    [iface]   [--all|-a]   [--extend|-e]   [--verbose|-v]   [--program|-p]  [--numeric|-n]
                     
                 全部接口:
                     netstat  -i
                 指定接口:
                     netstat  -I<IFace>
                     
         ifup/ifdown命令:
             注意:經過配置文件/etc/sysconfig/network-scripts/ifcfg-IFACE來識別接口並完成配置;
     
     配置主機名:
     
         hostname命令:
             查看:hostname
             配置:hostname  HOSTNAME
                 當前系統有效,重啓後無效;
             
         hostnamectl命令(CentOS 7):
             hostnamectl  status:顯示當前主機名信息;
             hostnamectl  set-hostname:設定主機名,永久有效;
             
         配置文件:/etc/sysconfig/network
             HOSTNAME=<HOSTNAME>
             
             注意:此方法的設置不會當即生效; 但之後會一直有效;
             
     配置DNS服務器指向:
         
         配置文件:/etc/resolv.conf
             nameserver   DNS_SERVER_IP
             
             如何測試(host/nslookup/dig):
                 # dig  -t  A  FQDN
                     FQDN --> IP
                     
                 # dig  -x  IP
                     IP --> FQDN
                     
     iproute家族:
         ip命令:
             show / manipulate routing, devices, policy routing and tunnels
             
             ip [ OPTIONS ] OBJECT { COMMAND | help }
                 OBJECT := { link | addr | route | netns  }
             
             注意: OBJECT可簡寫,各OBJECT的子命令也可簡寫;
                 
             ip  OBJECT:
                 
                 ip link: network device configuration
                 
                     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,用於將接口移動到指定的網絡名稱空間;
                         
                     ip  link  show  - display device attributes
                     
                     ip  link  help -  顯示簡要使用幫助;
                     
                 ip netns:  - manage network namespaces.
                 
                     ip  netns  list:列出全部的netns
                     ip  netns  add  NAME:建立指定的netns
                     ip  netns  del  NAME:刪除指定的netns
                     ip  netns   exec  NAME  COMMAND:在指定的netns中運行命令
                     
                 ip address - protocol address management.
                     
                     ip address add - add new protocol address
                         ip  addr  add  IFADDR  dev  IFACE
                             [label NAME]:爲額外添加的地址指明接口別名;
                             [broadcast ADDRESS]:廣播地址;會根據IP和NETMASK自動計算獲得;
                             [scope SCOPE_VALUE]:
                                 global:全局可用;
                                 link:接口可用;
                                 host:僅本機可用;                                            
                         
                     ip address delete - delete protocol address
                         ip addr  delete  IFADDR  dev  IFACE
                             
                     ip address show - look at protocol addresses
                         ip  addr   list  [IFACE]:顯示接口的地址;
                         
                     ip address flush - flush protocol addresses
                         ip  addr  flush  dev  IFACE
                         
                 ip route - routing table management
                 
                     ip route add - add new route
                     ip route change - change route
                     ip route replace - change or add new one
                         ip  route   add  TYPE PREFIX  via GW  [dev  IFACE]  [src SOURCE_IP]
                         
                         示例:
                             # ip route add 192.168.0.0/24  via 10.0.0.1  dev eth1 src  10.0.20.100
                             # ip  route  add default  via  GW                      
                         
                     ip route delete - delete route
                         ip  route  del  TYPE PRIFIX
                         
                         示例:
                             # ip  route delete  192.168.1.0/24
                             
                     ip route show - list routes
                         TYPE PRIFIX 
                     ip route flush - flush routing tables
                         TYPE  PRIFIX
                     
                     ip route get - get a single route
                         ip  route  get  TYPE PRIFIX
                         
                         示例:ip route  get  192.168.0.0/24
                     
         ss命令:
             ss  [options]  [ FILTER ]
                 選項:
                     -t:TCP協議的相關鏈接
                     -u:UDP相關的鏈接
                     -w:raw socket相關的鏈接
                     -l:監聽狀態的鏈接
                     -a:全部狀態的鏈接
                     -n:數字格式
                     -p:相關的程序及其PID
                     -e:擴展格式信息
                     -m:內存用量
                     -o:計時器信息
                     
                 FILTER := [ state TCP-STATE ]  [ EXPRESSION ]
                 
                     TCP的常見狀態:
                         TCP FSM:
                             LISTEN:監聽
                             ESTABLISEHD:創建的鏈接
                             FIN_WAIT_1:
                             FIN_WAIT_2:
                             SYN_SENT:
                             SYN_RECV:
                             CLOSED:
                         
                     EXPRESSION:
                         dport =
                         sport =
                             示例:'( dport = :22 or sport = :22)'
                                 ~]# ss   -tan    '(  dport = :22 or sport = :22  )'
                                 ~]# ss  -tan  state  ESTABLISHED
                                 
     配置文件:
         IP/NETMASK/GW/DNS等屬性的配置文件:/etc/sysconfig/network-scripts/ifcfg-IFACE
             IFACE:接口名稱;
         路由的相關配置文件:/etc/sysconfig/networkj-scripts/route-IFACE
                     
         配置文件/etc/sysconfig/network-scripts/ifcfg-IFACE經過大量參數來定義接口的屬性;其可經過vim等文本編輯器直接修改,也能夠使用專用的命令的進行修改(CentOS 6:system-config-network (setup),CentOS 7: nmtui)
         
             ifcfg-IFACE配置文件參數:
                 DEVICE:此配置文件對應的設備的名稱;
                 ONBOOT:在系統引導過程當中,是否激活此接口;
                 UUID:此設備的唯一標識;
                 IPV6INIT:是否初始化IPv6;
                 BOOTPROTO:激活此接口時使用什麼協議來配置接口屬性,經常使用的有dhcp、bootp、static、none;
                 TYPE:接口類型,常見的有Ethernet, Bridge;
                 DNS1:第一DNS服務器指向;
                 DNS2:備用DNS服務器指向;
                 DOMAIN:DNS搜索域;
                 IPADDR: IP地址;
                 NETMASK:子網掩碼;CentOS 7支持使用PREFIX以長度方式指明子網掩碼;
                 GATEWAY:默認網關;
                 USERCTL:是否容許普通用戶控制此設備;
                 PEERDNS:若是BOOTPROTO的值爲「dhcp」,是否容許dhcp server分配的dns服務器指向覆蓋本地手動指定的DNS服務器指向;默認爲容許;
                 HWADDR:設備的MAC地址;
                 
                 NM_CONTROLLED:是否使用NetworkManager服務來控制接口;
                 
             網絡服務:
                 network
                 NetworkManager
                 
                 管理網絡服務:
                     CentOS 6:  service  SERVICE  {start|stop|restart|status}
                     CentOS 7:systemctl  {start|stop|restart|status}  SERVICE[.service]
                     
                 配置文件修改以後,若是要生效,須要重啓網絡服務;
                     CentOS 6:# service  network  restart
                     CentOS 7:# systemctl  restart  network.service
                     
         用到非默認網關路由:/etc/sysconfig/network-scripts/route-IFACE
             支持兩種配置方式,但不可混用;
                 (1) 每行一個路由條目:
                     TARGET  via  GW
                     
                 (2) 每三行一個路由條目:
                     ADDRESS#=TARGET
                     NETMASK#=MASK
                     GATEWAY#=NEXTHOP
                     
     給接口配置多個地址:
         ip addr以外,ifconfig或配置文件均可以;
         
         (1) ifconfig  IFACE_LABEL  IPADDR/NETMASK
         
             IFACE_LABEL: eth0:0, eth0:1, ...
             
         (2) 爲別名添加配置文件;
             DEVICE=IFACE_LABEL
             BOOTPROTO:網上別名不支持動態獲取地址;
                 static, none
                 
     nmcli命令:
         nmcli  [ OPTIONS ] OBJECT { COMMAND | help }
             
             device - show and manage network interfaces
                 COMMAND := { status | show | connect | disconnect | delete | wifi | wimax }
             
             connection - start, stop, and manage network connections
                 COMMAND := { show | up | down | add | edit | modify | delete | reload | load }
                 
                 modify [ id | uuid | path ] <ID> [+|-]<setting>.<property> <value>
                 
                 如何修改IP地址等屬性:
                     # nmcli  conn  modify  IFACE  [+|-]setting.property  value
                         ipv4.address
                         ipv4.gateway
                         ipv4.dns1
                         ipv4.method
                             manual
                             
     博客做業:上述全部內容;
         ifcfg, ip/ss,配置文件
         
     課外做業:nmap, ncat, tcpdump命令;
 
網絡客戶端工具:
     
     ping/lftp/ftp/lftpget/wget等;
     
     ping命令:
         send ICMP ECHO_REQUEST to network hosts
         ICMP:Internet Control Message Protocol     
         
         ping  [OPTION]  destination
             -c #:發送的ping包個數;
             -w #:ping命令超時時長;
             -W #:一次ping操做中,等待對方響應的超時時長;
             -s #:指明ping包報文大小;
         
     hping命令: (package: hping3)
         send (almost) arbitrary TCP/IP packets to network hosts
         
             --fast
             --faster
             --flood
             -i uX
         
     traceroute命令:
         - print the route packets trace to network host
         
         跟蹤從源主機到目標主機之間通過的網關;
         
     ftp命令:
         ftp: File Transfer Protocol
             
             ftp服務命令行客戶端工具;
             
     lftp命令:
         lftp  [-p port]  [-u user[,pass]] [site]
         
         get, mget
         put, mput
         rm, mrm
         
     lftpget命令:
         lftpget [-c] [-d] [-v] URL [URL...]
             -c:繼續此前的下載;
             
     wget命令:
         The non-interactive network downloader.
         
         wget [option]... [URL]...
             -b:在後臺執行下載操做;
             -q:靜默模式,不顯示下載進度;
             -O file:下載的文件的保存位置;
             -c:續傳;
             --limit-rate=amount:以指定的速率傳輸文件;
相關文章
相關標籤/搜索