Skip to content

[RFE] systemd-networkd set netns for netdev #11103

Open

Description

@mskarbek

Is your feature request related to a problem? Please describe.
I would like to be able to create network device inside a specified network namespace.

Describe the solution you'd like
Inside [NetDev] section of a netdev unit type I would like to have an additional option - NetnsName which should allow specifying the name for network namespace in which networkd will create net device. If specified namespace doesn't exist then networkd should create it before creating the device.

Describe alternatives you've considered
Shell script? :/

Activity

  1. changed the title [-][RFE]systemd-networkd set netns for netdev[/-] [+][RFE] systemd-networkd set netns for netdev[/+] on Dec 10, 2018
  2. mskarbek commented on Dec 10, 2018

    @mskarbek
    ContributorAuthor

    To complete that functionality NetnsName should probably appear also in [Match] section of a network unit to allow configuration of a device created inside the namespace.

  3. added
    RFE 🎁
    on Dec 10, 2018
  4. mbello commented on Dec 14, 2018

    @mbello

    Please consider that it may be important to support "NetnsNameAtBirth" besides "NetnsName".
    So if only NetnsName is set systemd will create the device in the NetnsName and keep it there.
    If NetnsNameAtBirth is set alongside NetnsName, the device will be created in the NetnsNameAtBirth namespace and then be moved to NetnsName.
    A special value for NetnsNameAtBirth of "create-than-move" (?), would make systemd create the device first (without setting a namespace) AND THEN move the device to NetnsName

    Below I will copy part of the explanation of why this is an important feature and you can read more (along with actual use cases for both TUN and WG interfaces here https://www.wireguard.com/netns/

    Routing & Network Namespace Integration
    Like all Linux network interfaces, WireGuard integrates into the network namespace infrastructure. This means an administrator can have several entirely different networking subsystems and choose which interfaces live in each.

    WireGuard does something quite interesting. When a WireGuard interface is created (with ip link add wg0 type wireguard), it remembers the namespace in which it was created. "I was created in namespace A." Later, WireGuard can be moved to new namespaces ("I'm moving to namespace B."), but it will still remember that it originated in namespace A.

    WireGuard uses a UDP socket for actually sending and receiving encrypted packets. This socket always lives in namespace A – the original birthplace namespace. This allows for some very cool properties. Namely, you can create the WireGuard interface in one namespace (A), move it to another (B), and have cleartext packets sent from namespace B get sent encrypted through a UDP socket in namespace A.

    (Note that this same technique is available to userspace TUN-based interfaces, by creating a socket file-descriptor in one namespace, before changing to another namespace and keeping the file-descriptor from the previous namespace open.)

    This opens up some very nice possibilities.

    Ordinary Containerization
    The most obvious usage of this is to give containers (like Docker containers, for example) a WireGuard interface as its sole interface.
    (...)

    Routing All Your Traffic
    A less obvious usage, but extremely powerful nonetheless, is to use this characteristic of WireGuard for redirecting all of your ordinary Internet traffic over WireGuard. But first, let's review the old usual solutions for doing this:
    (...)

  5. nolange commented on Feb 13, 2020

    @nolange
    Contributor

    This is already touched in the PR, but how far should the scope of namespace-support go?

    I would like to have a namespaced tun network, with atleast 2 services accessing it.

    • namespaced tun .netdev
    • namespaced tun .network
    • a namespaced Listen .socket
    • a .service being both able to get fds/stdin from multiple different netns,
      aswell as inheriting the activating socket ns.
      tftp-hpa for example would get the UDP connect request forwarded via stdin, then has to open an UDP socket for communication in the correct netns.
    • a .service that serves the namespaced tun enpdoint
    • one or more .services that are namespace-aware but should be started after the namespace and -network is operational.

    So there would be a significant amount of work to get this done via systemd, but this would allow to setup networks and services in the same fashion instead of alot of sysv-style patchwork.
    On the other hand, this goes a couple steps into container territory, but with the ability to have services accessing multiple containers at once.

  6. ngortheone commented on Mar 10, 2020

    @ngortheone

    @nolange I would like to add to the list of usecases:

    • veth .netdev with Peer namespaced
  7. yuwata commented on Mar 11, 2020

    @yuwata
    Member

    #14915 will support such a use case.

  8. OJFord commented on Apr 20, 2020

    @OJFord

    @yuwata As far as I can tell - apologies if I've missed it - #14915 does not support the differing birth and running namespace use case that @mbello mentioned above in #11103 (comment)?

  9. daiaji commented on Feb 2, 2024

    @daiaji

    For container users, this is really good, but it hasn't been completed.

  10. hurutparittya commented on Mar 10, 2025

    @hurutparittya

    Is there any ongoing effort to enable this or similar functionality?

  11. aojea commented on Jun 21, 2025

    @aojea

    For containers , with the new feature added to the OCI spec https://github.com/opencontainers/runtime-spec/blob/main/config-linux.md#network-devices , it is now possible to delegate to the runtime moving the interface to the network namespace
    So systemd can create the interface and users can indicate the runtime to move it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFE 🎁network

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions