How to configure network using systemd-networkd in Yocto

From KoanSoftware Wiki
Revision as of 15:53, 16 March 2025 by Koan (talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

How to configure network using systemd-networkd in Yocto

When using Yocto Project it sometimes may be necessary to setup a static IP address via a custom recipe.

When the network manager is systemd-networkd the file usually responsible for that is /lib/systemd/network/80-wired.network

Simply change it from the original

   # cat /lib/systemd/network/80-wired.network
   
   [Match]
   Type=ether
   Name=!veth*
   KernelCommandLine=!nfsroot
   KernelCommandLine=!ip
   
   [Network]
   DHCP=yes
   
   [DHCP]
   UseMTU=yes
   RouteMetric=10
   ClientIdentifier=mac

To the following

   # cat /lib/systemd/network/80-wired.network
   
   [Match]
   Type=ether
   Name=eth0
   KernelCommandLine=!nfsroot
   KernelCommandLine=!ip
   
   [Network]
   Address=192.168.0.184/24
   Gateway=192.168.0.1
   DNS=1.1.1.1
   
   [DHCP]
   UseMTU=yes
   RouteMetric=10
   ClientIdentifier=mac

Below is an example of a possible solution

Create a .bbappend recipe to manage that and replace the existing wired.network file with your custom one

 meta-custom/
  └─ recipes-core/
     └── systemd-conf
         ├── systemd-conf
         │   └── wired.network
         └── systemd-conf_%.bbappend


The recipe systemd-conf_%.bbappend

   # recipes-core/systemd-conf/systemd-conf_%.bbappend 
   
   FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"


The network file

    # wired.network 
   
   [Match]
   Type=ether
   Name=eth0
   KernelCommandLine=!nfsroot
   KernelCommandLine=!ip
   
   [Network]
   Address=192.168.0.184/24
   Gateway=192.168.0.1
   DNS=1.1.1.1
       
   [DHCP]
   UseMTU=yes
   RouteMetric=10
   ClientIdentifier=mac