1. 程式人生 > >基於iproute命令集配置Linux網路(ip命令)

基於iproute命令集配置Linux網路(ip命令)

iproute是Linux下一個網路管理工具包合集,用於取代先前的如ifconfig,route,ifup,ifdown,netstat等歷史網路管理工具。該工具包功能強大,它通過網路鏈路套接字介面與核心進行聯絡。iproute的使用者介面比net-tools的使用者介面要更直觀。對網路資源比如鏈路、IP地址、路由和隧道等用“物件”抽象進行了恰當的定義,因此可以使用一致的語法來管理不同的物件。本文主要描述使用該工具包的ip命令來配置Linux網路。

一、iproute工具包集

檢視iproute工具包集

# more /etc/redhat-release 
CentOS Linux release 7.2
.1511 (Core) ###檢視當前環境下已經安裝的iproute包 # rpm -qa|grep iproute iproute-3.10.0-54.el7.x86_64 ###檢視iproute包生成的檔案 # rpm -ql iproute ###檢視iproute包配置檔案 # rpm -qc iproute ###檢視iproute包生成的二進位制檔案 # rpm -ql iproute|grep "bin" /usr/sbin/arpd /usr/sbin/bridge /usr/sbin/cbq /usr/sbin/ctstat /usr/sbin/genl /usr/sbin/ifcfg /usr/sbin/ifstat /usr/sbin/ip /usr/sbin/lnstat /usr/sbin/nstat /usr/sbin/routef /usr/sbin/routel /usr/sbin/rtacct /usr/sbin/rtmon /usr/sbin/rtpr /usr/sbin/rtstat /usr/sbin/ss /usr/sbin/tc

iproute與net-tools命令比對圖
這裡寫圖片描述

二、ip命令幫助及模組功能

獲取ip命令幫助

# ip help
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
      ip [ -force ] -batch filename
where  OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |
                  tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |
netns | l2tp | tcp_metrics | token } OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] | -h[uman-readable] | -iec | -f[amily] { inet | inet6 | ipx | dnet | bridge | link } | -4 | -6 | -I | -D | -B | -0 | -l[oops] { maximum-addr-flush-attempts } | -o[neline] | -t[imestamp] | -b[atch] [filename] | -rc[vbuf] [size] | -n[etns] name | -a[ll] } ###如果要獲取某個子模組的幫助,如獲取ip addr的具體用法,則 # ip addr help Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ] [ CONFFLAG-LIST ] ip addr del IFADDR dev STRING [mngtmpaddr] ip addr {show|save|flush} [ dev STRING ] [ scope SCOPE-ID ] [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ] [up] ip addr {showdump|restore} IFADDR := PREFIX | ADDR peer PREFIX [ broadcast ADDR ] [ anycast ADDR ] [ label STRING ] [ scope SCOPE-ID ] SCOPE-ID := [ host | link | global | NUMBER ] FLAG-LIST := [ FLAG-LIST ] FLAG FLAG := [ permanent | dynamic | secondary | primary | tentative | deprecated | dadfailed | temporary | CONFFLAG-LIST ] CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG CONFFLAG := [ home | nodad | mngtmpaddr | noprefixroute ] LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ] LFT := forever | SECONDS # man ip //獲取詳細幫助

iproute各子模組功能

ip link
        網路裝置配置命令,如可以啟用/禁用某個網路裝置,改變mtu及mac地址等

ip addr
        用於管理某個網路裝置與協議(ip或ipv6)有關的地址。
        與ip link類似,不過增加了協議有關的管理(ip地址管理)

ip addrlabel 
        ipv6的地址標籤,主要用於RFC3484中描述的ipv6地址的選擇。
        RFC3484主要介紹了2個演算法,用於ipv6地址(源地址和目標地址)的選擇策略

ip route    
        管理路由,如新增,刪除

ip rule    
        管理路由策略資料庫。這裡邊有一個演算法,用來控制路由的選擇策略

ip neigh    
        用於neighbor/ARP表的管理,如顯示,插入,刪除等

ip tunel
        隧道配置
        隧道的作用是將資料(可以是不同協議)封裝成ip包然後再網際網路傳輸

ip maddr
        多播地址管理

ip mroute
        多播路由管理

ip monitor
        狀態監控。如可以持續監控ip地址和路由的狀態

ip xfrm
        設定xfrm。xfrm是一個ip框架,可以轉換資料包的格式,如用某個演算法對資料包加密        

三、頻繁使用的幾個子模組常用方法

子模組用法

ip link 
        ip link show 檢視預設網路連線資訊,不包括ip地址
        ip link set 介面 [up|down] [multicast on|off]:

ip addr
    可以在一個介面配置多個地址而不使用介面別名:顯示這些地址
    ip addr show    
        ip addr add dev 介面 ip地址/掩碼 [ ladel 別名 ]
      ip addr add dev 介面 ip地址 [ ladel 別名 ]
      ip addr flush 介面 [to 網路地址]

ip route            
        ip route add 目標 via 下一跳 src 源地址 [dev 裝置]
        ip route del 目標
        ip route list                                                                        

啟用/禁用介面:
        ip link set 介面 up|down
        ifconfig 介面 up|down
        ifdown 介面,ifup 介面
    重置網路連線

TUI或GUI
        CentOS 6
                system-config-network-tui
            配置結束後將儲存配置檔案中
                setup --> Network Configuration

        CentOS 7
                nmtui                

四、使用示例

檢視當前主機網路連線資訊
    # 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: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
        link/ether 00:0c:29:57:26:9d 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:57:26:a7 brd ff:ff:ff:ff:ff:ff

檢視當前主機指定網路連線資訊    
    # ip link show eno16777728
    2: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
        link/ether 00:0c:29:57:26:9d brd ff:ff:ff:ff:ff:ff        

多播的啟用與關閉
    # ip link set eno16777728 multicast off
    # ip link show eno16777728  //如下,沒有出項MULTICAST
    2: eno16777728: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
        link/ether 00:0c:29:57:26:9d brd ff:ff:ff:ff:ff:ff
    # ip link set eno16777728 multicast on    

網絡卡的啟用與關閉
    # ip link set eno33554960 down
    # ip link set eno33554960 up

顯示主機ip地址資訊
    # ip addr show
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
          valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host 
          valid_lft forever preferred_lft forever
    2: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:9d brd ff:ff:ff:ff:ff:ff
        inet 172.24.8.131/24 brd 172.24.8.255 scope global dynamic eno16777728
          valid_lft 1196sec preferred_lft 1196sec           ### Author : Leshami
        inet6 fe80::20c:29ff:fe57:269d/64 scope link     ### Blog : http://blog.csdn.net/leshami 
          valid_lft forever preferred_lft forever                ### QQ/Weixin : 645746311
    3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:a7 brd ff:ff:ff:ff:ff:ff
        inet 192.168.81.144/24 brd 192.168.81.255 scope global dynamic eno33554960
          valid_lft 1380sec preferred_lft 1380sec

為指定網絡卡刪除ip地址
    # ip addr del dev eno33554960 192.168.81.144/24
    # ip addr show eno33554960                    
    3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:a7 brd ff:ff:ff:ff:ff:ff

為指定網絡卡新增ip地址
    # ip addr add dev eno33554960 192.168.81.189/24  
    # ip addr show eno33554960                    
    3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:a7 brd ff:ff:ff:ff:ff:ff
        inet 192.168.81.189/24 scope global eno33554960
          valid_lft forever preferred_lft forever      

為指定網絡卡新增多ip地址      
    # ip addr add dev eno33554960 192.168.81.150/24
    # ip addr show eno33554960
    3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:a7 brd ff:ff:ff:ff:ff:ff
        inet 192.168.81.189/24 scope global eno33554960
          valid_lft forever preferred_lft forever
        inet 192.168.81.150/24 scope global secondary eno33554960
          valid_lft forever preferred_lft forever

為指定網絡卡新增多ip及使用別名
    # ip addr add dev eno33554960 192.168.81.199/24 label eno33554960:0

    # ip addr show label eno33554960:0
        inet 192.168.81.199/24 scope global secondary eno33554960:0
          valid_lft forever preferred_lft forever

    # ifconfig|grep eno33554960:0 -A2  
    eno33554960:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.81.199  netmask 255.255.255.0  broadcast 0.0.0.0
            ether 00:0c:29:57:26:a7  txqueuelen 1000  (Ethernet)              

釋放特定網絡卡ip地址
    # ip addr show eno33554960 
    3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:a7 brd ff:ff:ff:ff:ff:ff
        inet 192.168.81.144/24 scope global dynamic eno33554960
          valid_lft 1364sec preferred_lft 1364sec
        inet 192.168.81.199/24 scope global secondary eno33554960:0
          valid_lft forever preferred_lft forever
    # ip addr flush eno33554960 //如果不指定特定網絡卡,則表示當前主機所有網絡卡ip地址被釋放
    # ip addr show eno33554960
    3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:a7 brd ff:ff:ff:ff:ff:ff

使用ip route新增網路路由
    ###當前本機IP地址為172.24.8.131
    ###假定要到達目標地址為:192.168.10.0/24,下一跳為 172.24.8.2 則新增路由命令如下

    # route -n  //首先檢視當前的路由
    Kernel IP routing table
    Destination    Gateway        Genmask        Flags Metric Ref    Use Iface
    0.0.0.0        192.168.81.2    0.0.0.0        UG    100    0        0 eno33554960
    172.24.8.0      0.0.0.0        255.255.255.0  U    100    0        0 eno16777728
    192.168.81.0    0.0.0.0        255.255.255.0  U    100    0        0 eno33554960
    # ip route list //首先檢視當前的路由
    default via 192.168.81.2 dev eno33554960  proto static  metric 100 
    172.24.8.0/24 dev eno16777728  proto kernel  scope link  src 172.24.8.131  metric 100 
    192.168.81.0/24 dev eno33554960  proto kernel  scope link  src 192.168.81.144  metric 100

    # ip route add 192.168.10.0/24 via 172.24.8.2 dev eno16777728
    # ip route list |grep 192.168.10
    192.168.10.0/24 via 172.24.8.2 dev eno16777728 

使用ip route新增主機路由
    ###假定要到達目標地址為:192.168.20.1,下一跳為: 172.24.8.254 
    # ip route add 192.168.20.1 via 172.24.8.254
    # ip route list |grep 192.168.20.1
    192.168.20.1 via 172.24.8.254 dev eno16777728

    刪除之前新增的網路路由和主機路由
    # ip route del 192.168.10.0/24  ###對於網路路由應指定掩碼
    # ip route del 192.168.20.1

    # ip route list
    default via 192.168.81.2 dev eno33554960  proto static  metric 100 
    172.24.8.0/24 dev eno16777728  proto kernel  scope link  src 172.24.8.131  metric 100 
    192.168.81.0/24 dev eno33554960  proto kernel  scope link  src 192.168.81.144  metric 100 

刪除預設閘道器
    # ip route del default
    # ip route list|grep default

新增預設閘道器
    # ip route add default via 172.24.8.2 dev eno16777728
    # ip route list|grep default
    default via 172.24.8.2 dev eno16777728 

配置特定網絡卡指定IP路由
    # ip addr add dev eno33554960 172.27.8.150/24  ###為eno33554960新增一個新ip
    # ip addr show eno33554960
    3: eno33554960: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:a7 brd ff:ff:ff:ff:ff:ff
        inet 192.168.81.144/24 scope global dynamic eno33554960
          valid_lft 1246sec preferred_lft 1246sec
        inet 172.27.8.150/24 scope global eno33554960
          valid_lft forever preferred_lft forever

    ###配置到達網路192.168.10.0經由172.27.8.2路由並且從這個ip 172.27.8.150收發資料包
    # ip route add 192.168.10.0 via 172.27.8.2 src 172.27.8.150 
    # ip route list |grep 172.27.8.150
    172.27.8.0/24 dev eno33554960  proto kernel  scope link  src 172.27.8.150 
    192.168.10.0 via 172.27.8.2 dev eno33554960  src 172.27.8.150

DBA牛鵬社(SQL/NOSQL/LINUX)

這裡寫圖片描述