labunix's blog

labunixのラボUnix

Raspberry Pi 4B(Ubuntu 20.04)をブリッジ設定に変えてみる。

■Raspberry Pi 4B(Ubuntu 20.04)をブリッジ設定に変えてみる。

$ lsb_release -d;uname -r -m
Description:	Ubuntu 20.04.1 LTS
5.4.0-1018-raspi aarch64

■現在のIP設定の確認。

$ ip a s dev eth0 | awk '{gsub(":[a-f0-9][a-f0-9]",":XX",$2);print $0}'
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether dc:XX:XX:XX:XX:XX brd ff:ff:ff:ff:ff:ff
    inet 10.26.7.225/24 brd 10.26.7.255 scope global dynamic eth0
       valid_lft 169871sec preferred_lft 169871sec
inet6 fe80::XXa6:XXff:XXb6:XX90/64 scope link
       valid_lft forever preferred_lft forever

$ ip nei | awk '!/STALE/{gsub("[a-f0-9]","X",$(NF-1));print $0}'
10.26.7.254 dev eth0 lladdr XX:XX:XX:XX:XX:XX REACHABLE

$ ip route show | grep eth0
default via 10.26.7.254 dev eth0 proto dhcp src 10.26.7.225 metric 100 
10.26.7.0/24 dev eth0 proto kernel scope link src 10.26.7.225 
10.26.7.254 dev eth0 proto dhcp scope link src 10.26.7.225 metric 100 

$ systemd-resolve --status | awk '/^Link|DNS Server/'
Link 5 (virbr0-nic)
Link 4 (virbr0)
Link 3 (wlan0)
  Current DNS Server: 10.26.7.254
         DNS Servers: 10.26.7.254
Link 2 (eth0)
  Current DNS Server: 10.26.7.254
         DNS Servers: 10.26.7.254

■もう一つの方法ではDNSサーバ設定も見える。

$ networkctl status eth0 | awk '{gsub(":[a-f0-9][a-f0-9]",":XX",$0);print $0}'
● 2: eth0                                                           
             Link File: /usr/lib/systemd/network/99-default.link    
          Network File: /run/systemd/network/10-netplan-eth0.network
                  Type: ether                                       
                 State: routable (configured)                       
                  Path: platform-fd580000.ethernet                  
                Driver: bcmgenet                                    
            HW Address: dc:XX:XX:XX:XX:XX (Raspberry Pi Trading Ltd)
                   MTU: 1500 (min: 68, max: 1500)                   
  Queue Length (Tx/Rx): 5/5                                         
      Auto negotiation: yes                                         
                 Speed: 100Mbps                                     
                Duplex: full                                        
                  Port: mii                                         
               Address: 10.26.7.225 (DHCP4)                         
                        fe80::XXa6:XXff:XXb6:XX90                   
               Gateway: 10.26.7.254 (BUFFALO.INC)                   
                   DNS: 10.26.7.254

■現状は「50-cloud-init.yaml」でDHCP設定がされている。

$ find /etc/netplan/ -type f -exec ls -l --time-style='+%Y%m%d-%H%M%S' {} +
-rw-r--r-- 1 root root 416 20200402-022348 /etc/netplan/50-cloud-init.yaml

$ cat /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        eth0:
            dhcp4: true
            optional: true
    version: 2

■現状の設定の確認は、何も変更せずに以下を実行すれば良い。

$ sudo netplan --debug apply 2>&1 | awk '/yaml/'
** (generate:23680): DEBUG: 01:21:34.887: Processing input file /etc/netplan/50-cloud-init.yaml..

■同じ設定のまま、99を作成する。

$ sudo cp /etc/netplan/{50,99}-cloud-init.yaml

$ sudo netplan --debug apply 2>&1 | awk '/yaml/'
** (generate:23818): DEBUG: 01:45:02.053: Processing input file /etc/netplan/50-cloud-init.yaml..
** (generate:23818): DEBUG: 01:45:02.054: Processing input file /etc/netplan/99-cloud-init.yaml..

■ブリッジ構成(br0 -> eth0)の固定IPに設定変更

$ cat /etc/netplan/99-cloud-init.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
  bridges:
    br0:
      interfaces: [eth0]
      dhcp4: false
      dhcp6: false
      addresses: [10.26.7.225/24]
      gateway4: 10.26.7.254
      nameservers:
        addresses: [10.26.7.254]
      parameters:
        forward-delay: 0
        stp: false
      optional: true

■tryオプションはブリッジ構成に対応していない。

$ sudo netplan try --timeout 10
br0: reverting custom parameters for bridges and bonds is not supported

Please carefully review the configuration and use 'netplan apply' directly.

■--debugオプション付きで変更する。

$ sudo netplan --debug apply 2>&1 | awk '/yaml/'
** (generate:23920): DEBUG: 01:57:49.468: Processing input file /etc/netplan/50-cloud-init.yaml..
** (generate:23920): DEBUG: 01:57:49.468: Processing input file /etc/netplan/99-cloud-init.yaml..

■変更後の確認。

$ ip a s dev eth0 | awk '{gsub(":[a-f0-9][a-f0-9]",":XX",$2);print $0}'
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br0 state UP group default qlen 1000
link/ether dc:XX:XX:XX:XX:XX brd ff:ff:ff:ff:ff:ff

$ ip a s dev br0 | awk '{gsub(":[a-f0-9][a-f0-9]",":XX",$2);print $0}'
6: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether dc:XX:XX:XX:XX:XX brd ff:ff:ff:ff:ff:ff
    inet 10.26.7.225/24 brd 10.26.7.255 scope global br0
       valid_lft forever preferred_lft forever
inet6 fe80::XXa6:XXff:XXb6:XX90/64 scope link
       valid_lft forever preferred_lft forever

$ ip nei | awk '!/STALE/{gsub("[a-f0-9]","X",$(NF-1));print $0}'
10.26.7.254 dev br0 lladdr XX:XX:XX:XX:XX:XX REACHABLE

$ ip route show | grep "eth0\|br0"
default via 10.26.7.254 dev br0 proto static 
10.26.7.0/24 dev br0 proto kernel scope link src 10.26.7.225 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown 

$ ip route show | awk '/eth0|br0/&& $0 !~ /virbr/'
default via 10.26.7.254 dev br0 proto static 
10.26.7.0/24 dev br0 proto kernel scope link src 10.26.7.225 

$ systemd-resolve --status | awk '/^Link|DNS Server/'
Link 6 (br0)
  Current DNS Server: 10.26.7.254
         DNS Servers: 10.26.7.254
Link 5 (virbr0-nic)
Link 4 (virbr0)
Link 3 (wlan0)
  Current DNS Server: 10.26.7.254
         DNS Servers: 10.26.7.254
Link 2 (eth0)

$ networkctl status eth0 | awk '{gsub(":[a-f0-9][a-f0-9]",":XX",$0);print $0}'
● 2: eth0                                                           
             Link File: /usr/lib/systemd/network/99-default.link    
          Network File: /run/systemd/network/10-netplan-eth0.network
                  Type: ether                                       
                 State: enslaved (configured)                       
                  Path: platform-fd580000.ethernet                  
                Driver: bcmgenet                                    
            HW Address: dc:XX:XX:XX:XX:XX (Raspberry Pi Trading Ltd)
                   MTU: 1500 (min: 68, max: 1500)                   
  Queue Length (Tx/Rx): 5/5                                         
      Auto negotiation: yes                                         
                 Speed: 100Mbps                                     
                Duplex: full                                        
                  Port: mii        

$ networkctl status br0 | awk '{gsub(":[a-f0-9][a-f0-9]",":XX",$0);print $0}'
● 6: br0                                                              
               Link File: /usr/lib/systemd/network/99-default.link    
            Network File: /run/systemd/network/10-netplan-br0.network 
                    Type: bridge                                      
                   State: routable (configured)                       
                  Driver: bridge                                      
              HW Address: dc:XX:XX:XX:XX:XX (Raspberry Pi Trading Ltd)
                     MTU: 1500 (min: 68, max: 65535)                  
           Forward Delay: 0                                           
              Hello Time: 2s                                          
                 Max Age: 20s                                         
             Ageing Time: 5min                                        
                Priority: 32768                                       
                     STP: no                                          
  Multicast IGMP Version: 2                                           
    Queue Length (Tx/Rx): 1/1                                         
                 Address: 10.26.7.225                                 
                          fe80::XXa6:XXff:XXb6:XX90                   
                 Gateway: 10.26.7.254 (BUFFALO.INC)                   
                     DNS: 10.26.7.254                  


■不測の事態に備え、バックアップを取っておく。

$ tar zcvf netplan-bridge.tar.gz /etc/netplan/
tar: メンバ名から先頭の `/' を取り除きます
/etc/netplan/
/etc/netplan/50-cloud-init.yaml
/etc/netplan/99-cloud-init.yaml

$ tar ztvf netplan-bridge.tar.gz
drwxr-xr-x root/root         0 2020-09-22 01:55 etc/netplan/
-rw-r--r-- root/root       416 2020-04-02 02:23 etc/netplan/50-cloud-init.yaml
-rw-r--r-- root/root       384 2020-09-22 01:53 etc/netplan/99-cloud-init.yaml