Systemd/networkd: Difference between revisions

Debugging: note runtime log level changing
DHCP (talk | contribs)
m remove unneeded indent at the beginning of each line in nix blocks; use console highlight for shell command snippets
 
Line 54: Line 54:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
  systemd.network.networks."10-lan" = {
systemd.network.networks."10-lan" = {
    matchConfig.Name = "lan";
  matchConfig.Name = "lan";
    networkConfig.DHCP = "ipv4";
  networkConfig.DHCP = "ipv4";
  };
};
</syntaxhighlight>
</syntaxhighlight>


Line 78: Line 78:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
systemd.services."systemd-networkd".environment.SYSTEMD_LOG_LEVEL = "debug";
systemd.services."systemd-networkd".environment.SYSTEMD_LOG_LEVEL = "debug";
</syntaxhighlight>Log level can also be changed at runtime with<syntaxhighlight lang="bash">
</syntaxhighlight>
Log level can also be changed at runtime with
<syntaxhighlight lang="console">
$ systemctl service-log-level systemd-networkd.service debug
$ systemctl service-log-level systemd-networkd.service debug
# or
$ # or
$ systemctl service-log-level systemd-networkd.service info
$ systemctl service-log-level systemd-networkd.service info
</syntaxhighlight>
</syntaxhighlight>
Line 107: Line 109:


The current operational state of network interfaces can be learned from <code>networkctl</code>.
The current operational state of network interfaces can be learned from <code>networkctl</code>.
<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
networkctl
$ networkctl
IDX LINK          TYPE    OPERATIONAL SETUP     
IDX LINK          TYPE    OPERATIONAL SETUP     
   1 lo            loopback carrier    unmanaged
   1 lo            loopback carrier    unmanaged
Line 154: Line 156:


=== Interface Naming ===
=== Interface Naming ===
The name of an interface can be changed based on different matches. This is useful for pretty names (e.g. wan, lan), but also if you want to make sure that your interface name never changes. This might be useful because even with predictable interface naming your interface name can change, for example when you add a new PCIe card and indexing changes, or due to kernel changes the way your mainboard gets interpreted changes.<syntaxhighlight lang="nix">
 
  systemd.network.links."10-wan" = {
The name of an interface can be changed based on different matches. This is useful for pretty names (e.g. wan, lan), but also if you want to make sure that your interface name never changes. This might be useful because even with predictable interface naming your interface name can change, for example when you add a new PCIe card and indexing changes, or due to kernel changes the way your mainboard gets interpreted changes.
    # Check systemd.link(5) for other matchers
<syntaxhighlight lang="nix">
    matchConfig.Path = "pci-0000:09:00.0";
systemd.network.links."10-wan" = {
    linkConfig.Name = "wan";
  # Check systemd.link(5) for other matchers
  };
  matchConfig.Path = "pci-0000:09:00.0";
  linkConfig.Name = "wan";
};
</syntaxhighlight>
</syntaxhighlight>


Line 167: Line 171:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
  systemd.network.networks."10-wan" = {
systemd.network.networks."10-wan" = {
    matchConfig.Name = "enp1s0";
  matchConfig.Name = "enp1s0";
    networkConfig = {
  networkConfig = {
      # start a DHCP Client for IPv4 Addressing/Routing
    # start a DHCP Client for IPv4 Addressing/Routing
      DHCP = "ipv4";
    DHCP = "ipv4";
      # accept Router Advertisements for Stateless IPv6 Autoconfiguraton (SLAAC)
    # accept Router Advertisements for Stateless IPv6 Autoconfiguraton (SLAAC)
      IPv6AcceptRA = true;
    IPv6AcceptRA = true;
    };
    # make routing on this interface a dependency for network-online.target
    linkConfig.RequiredForOnline = "routable";
   };
   };
  # make routing on this interface a dependency for network-online.target
  linkConfig.RequiredForOnline = "routable";
};
</syntaxhighlight>
</syntaxhighlight>


Line 187: Line 191:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
  systemd.network.networks."10-wan" = {
systemd.network.networks."10-wan" = {
    # match the interface by name
  # match the interface by name
    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 = [
      # create default routes for both IPv6 and IPv4
    # create default routes for both IPv6 and IPv4
      { Gateway = "fe80::1"; }
    { Gateway = "fe80::1"; }
      { Gateway = "192.0.2.1"; }
    { 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
      {
    {
        Gateway = "172.31.1.1";
      Gateway = "172.31.1.1";
        GatewayOnLink = true;
      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
    linkConfig.RequiredForOnline = "routable";
  linkConfig.RequiredForOnline = "routable";
  };
};
</syntaxhighlight>
</syntaxhighlight>


Line 217: Line 221:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
  systemd.network = {
systemd.network = {
    netdevs = {
  netdevs = {
      "20-vlan10" = {
    "20-vlan10" = {
        netdevConfig = {
      netdevConfig = {
          Kind = "vlan";
        Kind = "vlan";
          Name = "vlan10";
        Name = "vlan10";
        };
        vlanConfig.Id = 10;
       };
       };
       "20-vlan20" = {
       vlanConfig.Id = 10;
        netdevConfig = {
    };
          Kind = "vlan";
    "20-vlan20" = {
          Name = "vlan20";
      netdevConfig = {
        };
        Kind = "vlan";
        vlanConfig.Id = 20;
        Name = "vlan20";
       };
       };
      vlanConfig.Id = 20;
     };
     };
  };


    networks = {
  networks = {
      "30-enp1s0" = {
    "30-enp1s0" = {
        matchConfig.Name = "enp1s0";
      matchConfig.Name = "enp1s0";
        # tag vlan on this link
      # tag vlan on this link
        vlan = [
      vlan = [
          "vlan10"
        "vlan10"
          "vlan20"
        "vlan20"
        ];
      ];
        networkConfig.LinkLocalAddressing = "no";
      networkConfig.LinkLocalAddressing = "no";
        linkConfig.RequiredForOnline = "carrier";
      linkConfig.RequiredForOnline = "carrier";
      };
    };
      "40-vlan10" = {
    "40-vlan10" = {
        matchConfig.Name = "vlan10";
      matchConfig.Name = "vlan10";
        # add relevant configuration here
      # add relevant configuration here
      };
    };
      "40-vlan20" = {
    "40-vlan20" = {
        matchConfig.Name = "vlan20";
      matchConfig.Name = "vlan20";
        # add relevant configuration here
      # add relevant configuration here
      };
     };
     };
   };
   };
};
</syntaxhighlight>
</syntaxhighlight>


Line 268: Line 272:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
  systemd.network = {
systemd.network = {
    netdevs = {
  netdevs = {
      # Create the bridge interface
    # Create the bridge interface
      "20-br0" = {
    "20-br0" = {
        netdevConfig = {
      netdevConfig = {
          Kind = "bridge";
        Kind = "bridge";
          Name = "br0";
        Name = "br0";
        };
       };
       };
    };
  };
  networks = {
    # Connect the bridge ports to the bridge
    "30-enp1s0" = {
      matchConfig.Name = "enp1s0";
      networkConfig.Bridge = "br0";
      linkConfig.RequiredForOnline = "enslaved";
    };
    "30-enp2s0" = {
      matchConfig.Name = "enp2s0";
      networkConfig.Bridge = "br0";
      linkConfig.RequiredForOnline = "enslaved";
     };
     };
     networks = {
     # Configure the bridge for its desired function
      # Connect the bridge ports to the bridge
    "40-br0" = {
      "30-enp1s0" = {
      matchConfig.Name = "br0";
        matchConfig.Name = "enp1s0";
      bridgeConfig = {};
        networkConfig.Bridge = "br0";
      # Disable address autoconfig when no IP configuration is required
        linkConfig.RequiredForOnline = "enslaved";
      #networkConfig.LinkLocalAddressing = "no";
      };
      linkConfig = {
      "30-enp2s0" = {
        # or "routable" with IP addresses configured
        matchConfig.Name = "enp2s0";
        RequiredForOnline = "carrier";
        networkConfig.Bridge = "br0";
        linkConfig.RequiredForOnline = "enslaved";
      };
      # Configure the bridge for its desired function
      "40-br0" = {
        matchConfig.Name = "br0";
        bridgeConfig = {};
        # Disable address autoconfig when no IP configuration is required
        #networkConfig.LinkLocalAddressing = "no";
        linkConfig = {
          # or "routable" with IP addresses configured
          RequiredForOnline = "carrier";
        };
       };
       };
     };
     };
   };
   };
};
</syntaxhighlight>
</syntaxhighlight>


Line 313: Line 317:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
  systemd.network = {
systemd.network = {
    netdevs = {
  netdevs = {
      "10-bond0" = {
    "10-bond0" = {
        netdevConfig = {
      netdevConfig = {
          Kind = "bond";
        Kind = "bond";
          Name = "bond0";
        Name = "bond0";
        };
      };
        bondConfig = {
      bondConfig = {
          Mode = "802.3ad";
        Mode = "802.3ad";
          TransmitHashPolicy = "layer3+4";
        TransmitHashPolicy = "layer3+4";
        };
       };
       };
     };
     };
    networks = {
  };
      "30-enp2s0" = {
  networks = {
        matchConfig.Name = "enp2s0";
    "30-enp2s0" = {
        networkConfig.Bond = "bond0";
      matchConfig.Name = "enp2s0";
      };
      networkConfig.Bond = "bond0";
      "30-enp3s0" = {
    };
        matchConfig.Name = "enp3s0";
    "30-enp3s0" = {
        networkConfig.Bond = "bond0";
      matchConfig.Name = "enp3s0";
      };
      networkConfig.Bond = "bond0";
      "40-bond0" = {
    };
        matchConfig.Name = "bond0";
    "40-bond0" = {
        linkConfig = {
      matchConfig.Name = "bond0";
          RequiredForOnline = "carrier";
      linkConfig = {
        };
        RequiredForOnline = "carrier";
        networkConfig.LinkLocalAddressing = "no";
       };
       };
      networkConfig.LinkLocalAddressing = "no";
     };
     };
   };
   };
};
</syntaxhighlight>
</syntaxhighlight>


Line 356: Line 360:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
  systemd.network = {
systemd.network = {
    networks = {
  networks = {
      "30-lan" = {
    "30-lan" = {
        matchConfig.Name = "lan";
      matchConfig.Name = "lan";
        address = [ "2001:db8:1122:3344::1/64" ];
      address = [ "2001:db8:1122:3344::1/64" ];
        networkConfig = {
      networkConfig = {
          IPv6SendRA = true;
        IPv6SendRA = true;
        };
      };
        ipv6Prefixes = [
      ipv6Prefixes = [
          {
        {
            # Announce a static prefix
          # Announce a static prefix
            ipv6PrefixConfig.Prefix = "2001:db8:1122:3344::/64";
          ipv6PrefixConfig.Prefix = "2001:db8:1122:3344::/64";
          }
        }
        ];
      ];
        ipv6SendRAConfig = {
      ipv6SendRAConfig = {
          # Provide a DNS resolver
        # Provide a DNS resolver
          EmitDNS = true;
        EmitDNS = true;
          DNS = "2001:db8:1122:3344::1";
        DNS = "2001:db8:1122:3344::1";
        };
       };
       };
     };
     };
   };
   };
};
</syntaxhighlight>
</syntaxhighlight>