Jump to content

Systemd/networkd: Difference between revisions

m
no edit summary
imported>Broizter
m (vlans needs to be quoted or else you get an undefined variable error)
mNo edit summary
 
(14 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Systemd/breadcrumb}}
{{DISPLAYTITLE:systemd-networkd}}
{{DISPLAYTITLE:systemd-networkd}}
[https://www.freedesktop.org/software/systemd/man/systemd-networkd.html systemd-networkd] is the network[[Category: Networking]] configuration component of the [[systemd]][[Category:systemd]] software suite. It is well integrated into NixOS below {{Nixos:option|systemd.network}} and should be preferred over {{Nixos:option|networking.interfaces}} options for most use cases, since it receives far superior maintenance.
[https://www.freedesktop.org/software/systemd/man/systemd-networkd.html systemd-networkd] is the network configuration component of the [[systemd]][[Category:systemd]] software suite. It is well integrated into NixOS below {{Nixos:option|systemd.network}} and should be preferred over {{Nixos:option|networking.interfaces}} options for most use cases, since it receives far superior maintenance.


Configuration for networkd is split into three sections.
Configuration for networkd is split into three sections.
Line 70: Line 71:
</nowiki>}}
</nowiki>}}


Note that we usually prefix the configuration file with a number. This can be important, because networkd collects all available configuration files, then sorts them alphabetically, and uses the first match for each interface as its configuration. This happens separately for <code>.link</code>, <code>.netdev</code> and <code>.network</code> files, so that you can have one configuration of each type per interface.
Note that we usually prefix the configuration file with a number. This can be important, because networkd collects all available configuration files, then sorts them alphanumerically, and uses the first match for each interface as its configuration. This happens separately for <code>.link</code>, <code>.netdev</code> and <code>.network</code> files, so that you can have one configuration of each type per interface.


=== Debugging ===
=== Debugging ===
Line 117: Line 118:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
systemd.network."50-enp3s0" = {
systemd.network.networks."50-enp3s0" = {
   matchConfig.Name = "enp3s0";
   matchConfig.Name = "enp3s0";
   # acquire a DHCP lease on link up
   # acquire a DHCP lease on link up
Line 177: Line 178:
     matchConfig.Name = "enp1s0";
     matchConfig.Name = "enp1s0";
     address = [
     address = [
        # configure addresses including subnet mask
      # configure addresses including subnet mask
        "192.0.2.100/24"
      "192.0.2.100/24"
        "2001:DB8::2/64"
      "2001:DB8::2/64"
     ];
     ];
     routes = [
     routes = [
Line 186: Line 187:
       { routeConfig.Gateway = "192.0.2.1"; }
       { routeConfig.Gateway = "192.0.2.1"; }
       # or when the gateway is not on the same network
       # or when the gateway is not on the same network
       { routeConfig = {
       {
        Gateway = "172.31.1.1";
        routeConfig = {
        GatewayOnLink = true;
          Gateway = "172.31.1.1";
      }; }
          GatewayOnLink = true;
        };
      }
     ];
     ];
     # make the routes on this interface a dependency for network-online.target
     # make the routes on this interface a dependency for network-online.target
Line 200: Line 203:
VLANs can be configured on top of hardlinks as well as virtual links, like bonding interfaces. They provide separate logical networks over physical links.
VLANs can be configured on top of hardlinks as well as virtual links, like bonding interfaces. They provide separate logical networks over physical links.


In this example we tag two VLANs with Ids 10 and 20 on a physical link <code>enp1s0</code>. The VLAN interfaces become available <code>vlan10</code> and <code>vlan20</code> and can receive additional configuration.
In this example we tag two VLANs with Ids 10 and 20 on a physical link <code>enp1s0</code>. The VLAN interfaces become available as <code>vlan10</code> and <code>vlan20</code> and can receive additional configuration.


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 229: Line 232:
           "vlan20"
           "vlan20"
         ];
         ];
        networkConfig.LinkLocalAddressing = "no";
        linkConfig.RequiredForOnline = "carrier";
       };
       };
       "40-vlan10" = {
       "40-vlan10" = {
Line 276: Line 281:
       # Configure the bridge for its desired function
       # Configure the bridge for its desired function
       "40-br0" = {
       "40-br0" = {
         matchConfig.Name ="br0";
         matchConfig.Name = "br0";
         bridgeConfig = {};
         bridgeConfig = {};
        # Disable address autoconfig when no IP configuration is required
        #networkConfig.LinkLocalAddressing = "no";
         linkConfig = {
         linkConfig = {
           # or "routable" with IP addresses configured
           # or "routable" with IP addresses configured
Line 289: Line 296:
=== Bonding ===
=== Bonding ===


Given two hardlinks <code>enp2s0</code> and <code>enp3s0</code> create a virtual <code>bond0</code> interface using Dynamic LACP (802.3ad), hashing outgoing packets using a packet's Layer3/4 (OSI Layer) information.
Given two hardlinks <code>enp2s0</code> and <code>enp3s0</code> create a virtual <code>bond0</code> interface using Dynamic LACP (802.3ad), hashing outgoing packets using a packet's layer 3/4 (network/transport layer in the OSI model) information.


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 339: Line 346:
       "30-lan" = {
       "30-lan" = {
         matchConfig.Name = "lan";
         matchConfig.Name = "lan";
         address = [
         address = [ "2001:db8:1122:3344::1/64" ];
          "2001:db8:1122:3344::1/64"
        ];
         networkConfig = {
         networkConfig = {
           IPv6SendRA = true;
           IPv6SendRA = true;
         };
         };
         ipv6Prefixes = [ {
         ipv6Prefixes = [
          # Announce a static prefix
          {
          ipv6PrefixConfig.Prefix = "2001:db8:1122:3344::/64";
            # Announce a static prefix
         } ];
            ipv6PrefixConfig.Prefix = "2001:db8:1122:3344::/64";
          }
         ];
         ipv6SendRAConfig = {
         ipv6SendRAConfig = {
           # Provide a DNS resolver
           # Provide a DNS resolver
Line 355: Line 362:
         };
         };
       };
       };
    };
   };
   };
</syntaxhighlight>
</syntaxhighlight>
Line 372: Line 380:


* [https://gist.github.com/mweinelt/b78f7046145dbaeab4e42bf55663ef44 NixOS 22.11 VDSL Router (VLANs on top of Bonding, IPv6 Prefix-Delegation, pppd Integration)] by [https://github.com/mweinelt mweinelt]
* [https://gist.github.com/mweinelt/b78f7046145dbaeab4e42bf55663ef44 NixOS 22.11 VDSL Router (VLANs on top of Bonding, IPv6 Prefix-Delegation, pppd Integration)] by [https://github.com/mweinelt mweinelt]
[[Category:systemd]]
[[Category:Networking]]
trusted
596

edits