欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ubuntu 18.04 netplan 配置多网卡、多路由、多ip

程序员文章站 2022-06-03 21:27:38
...

ubuntu 18.04 netplan 配置多网卡、多路由、多ip、bonding

Ubuntu 18.04采用Netplan 来管理网络配置,下面说下netplan 多网卡的复杂配置
1.单网卡 DHCP配置

network:
    version: 2
    renderer: networkd
    ethernets:
        enp3s0:
            dhcp4: true

2.配置静态ip

network:
    version: 2
    renderer: networkd
    ethernets:
        enp3s0:
            addresses:
                - 10.10.10.2/24
            gateway4: 10.10.10.1
            nameservers:
                search: [mydomain, otherdomain]
                addresses: [10.10.10.1, 1.1.1.1]

3.多网口DHCP配置

network:
    version: 2
    ethernets:
        enred:
            dhcp4: yes
            dhcp4-overrides:
                route-metric: 100
        engreen:
            dhcp4: yes
            dhcp4-overrides:
                route-metric: 200

4.配置无密码的开放wifi

network:
    version: 2
    wifis:
        wl0:
            access-points:
                opennetwork: {}
            dhcp4: yes

5.配置静态地址wifi,使用WAP和WAP2加密的wifi

network:
    version: 2
    renderer: networkd
    wifis:
        wlp2s0b1:
            dhcp4: no
            dhcp6: no
            addresses: [192.168.0.21/24]
            gateway4: 192.168.0.1
            nameservers:
                addresses: [192.168.0.1, 8.8.8.8]
            access-points:
                "network_ssid_name":
                    password: "**********"

6.一个网口上配置多个地址:

network:
    version: 2
    renderer: networkd
    ethernets:
        enp3s0:
         addresses:
             - 10.100.1.38/24
             - 10.100.1.39/24
         gateway4: 10.100.1.1

7.一个网口上面有多个地址和多个网关

network:
    version: 2
    renderer: networkd
    ethernets:
        enp3s0:
         addresses:
             - 9.0.0.9/24
             - 10.0.0.10/24
             - 11.0.0.11/24
         #gateway4:  # unset, since we configure routes below
         routes:
             - to: 0.0.0.0/0 #路由
                 via: 9.0.0.1 #网关
                 metric: 100 #优先级
             - to: 0.0.0.0/0 #路由
                 via: 10.0.0.1 #网关
                 metric: 100 #优先级
             - to: 0.0.0.0/0 #路由
                 via: 11.0.0.1 #网关
                 metric: 100 #优先级      

8.bonding 主备模式

network:
    version: 2
    renderer: networkd
    bonds:
        bond0:
            dhcp4: yes
            interfaces:
                - enp3s0
                - enp4s0
            parameters:
                mode: active-backup
                primary: enp3s0

9.bonding 802.3ad模式

network:
    version: 2
    renderer: networkd
    ethernets:
        enp2s0:
            dhcp4: no
        enp3s0:
            dhcp4: no
            optional: true
   bonds:
        bond-lan:
            interfaces: [enp2s0, enp3s0]
            addresses: [192.168.93.2/24]
            parameters:
                mode: 802.3ad
                mii-monitor-interval: 1

交换机配置 以华为交换机为例

<Quidway> system-view
[SwitchA] interface eth-trunk 1
[SwitchA-Eth-Trunk1] bpdu enable
[SwitchA-Eth-Trunk1] mode lacp-static
[SwitchA-Eth-Trunk1] q

[SwitchA] interface gigabitethernet 0/0/1
[SwitchA-GigabitEthernet0/0/1] eth-trunk 1
[SwitchA-GigabitEthernet0/0/1] quit
[SwitchA] interface gigabitethernet 0/0/2
[SwitchA-GigabitEthernet0/0/2] eth-trunk 1
[SwitchA-GigabitEthernet0/0/2] q
[SwitchA] display eth-trunk 1 #查看是否协商成功

服务器 cat /proc/net/bonding/bond4-lan #查看是否协商成功

  1. 单网口 多网段 接交换机truk口 打vlan标签
network:
    version: 2
    renderer: networkd
    ethernets:
        mainif:
            match:
                macaddress: "de:ad:be:ef:ca:fe"
            set-name: mainif
            addresses: [ "10.3.0.5/23" ]
            gateway4: 10.3.0.1
            nameservers:
                addresses: [ "8.8.8.8", "8.8.4.4" ]
                search: [ example.com ]
    vlans:
        vlan15:
            id: 15
            link: mainif
            addresses: [ "10.3.99.5/24" ]
        vlan10:
            id: 10
            link: mainif
            addresses: [ "10.3.98.5/24" ]
            nameservers:
                addresses: [ "114.114.114.114" ]
  1. 配置多网卡多网口多ip多路由
 network:
    version: 2
    renderer: networkd
    ethernets:
        ens3:
            addresses:
             - 192.168.3.30/24
            dhcp4: no
            routes:
             - to: 192.168.3.0/24
                 via: 192.168.3.1
                 table: 101
            routing-policy:
             - from: 192.168.3.0/24
                 table: 101
        ens5:
            addresses:
             - 192.168.5.24/24
            dhcp4: no
            gateway4: 192.168.5.1
            routes:
             - to: 192.168.5.0/24
                 via: 192.168.5.1
                 table: 102
            routing-policy:
            - from: 192.168.5.0/24
                table: 102
network:
    ethernets:
        ens33:
            dhcp4: false
            dhcp6: false
            addresses: [192.168.1.20/24]
            #gateway4: 192.168.1.253
            routes:
                    - to: 192.168.0.0/24
                      via: 192.168.1.253

                    - to: 192.168.1.0/24
                      via: 192.168.1.253

                    - to: 192.168.2.0/24
                      via: 192.168.1.253

                    - to: 192.168.3.0/24
                      via: 192.168.1.253

                    - to: 192.168.4.0/24
                      via: 192.168.1.253

                    - to: 192.168.5.0/24
                      via: 192.168.1.253

                      table: 101

            routing-policy:
                    - from: 0.0.0.0/24
                      table: 101
            nameservers:
                    addresses: [192.168.1.1, 192.168.1.2]
        ens34:
            dhcp4: false
            dhcp6: false
            addresses: [172.16.0.10/24]
            gateway4: 172.16.0.253
            nameservers:
                    addresses: [172.16.0.1, 192.168.1.1]
    version: 2