Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8052435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:32:43+00:00 2026-06-05T07:32:43+00:00

I am using following C function to create multiple network namespaces from a single

  • 0

I am using following C function to create multiple network namespaces from a single process instance:

void create_namespace(const char *ns_name)
{
    char ns_path[100];

    snprintf(ns_path, 100, "%s/%s", "/var/run/netns", ns_name);
    close(open(ns_path, O_RDONLY|O_CREAT|O_EXCL, 0));
    unshare(CLONE_NEWNET);
    mount("/proc/self/ns/net", ns_path, "none", MS_BIND , NULL);
}

After my process creates all the namspaces and I add a tap interface to any of the one network namespace (with ip link set tap1 netns ns1 command), then I actually see this interface in all of the namespaces (presumably, this is actually a single namespace that goes under different names).

But, if I create multiple namespaces by using multiple processes, then everything is working just fine.

What could be wrong here? Do I have to pass any additional flags to the unshare() to get this working from a single process instance? Is there a limitation that a single process instance can’t create multiple network namespaces? Or is there a problem with mount() call, because /proc/self/ns/net is actually mounted multiple times?

Update:
It seems that unshare() function creates multiple network namespaces correctly, but all the mount points in /var/run/netns/ actually reference to the first network namespace that was mounted in that direcotry.

Update2:
It seems that the best approach is to fork() another process and execute create_namespace() function from there. Anyway, I would be glad to hear a better solution that does not involve fork() call or at least get a confirmation that would prove that it is impossible to create and manage multiple network namespaces from a single process.

Update3:
I am able to create multiple namespaces with unshare() by using the following code:

int  main() {
    create_namespace("a");
    system("ip tuntap add mode tap tapa");
    system("ifconfig -a");//shows lo and tapA interface
    create_namespace("b");
    system("ip tuntap add mode tap tapb");
    system("ifconfig -a");//show lo and tapB interface, but does not show tapA. So this is second namespace created.
}

But after the process terminates and I execute ip netns exec a ifconfig -a and ip netns exec b ifconfig -a it seems that both commands were suddenly executed in namespace a. So the actual problem is storing the references to the namespaces (or calling mount() the right way. But I am not sure, if this is possible).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:32:44+00:00Added an answer on June 5, 2026 at 7:32 am

    You only have to bind mount /proc/*/ns/* if you need to access these namespaces from another process, or need to get handle to be able to switch back and forth between the two. It is not needed to use multiple namespaces from a single process.

    • unshare does create new namespace.
    • clone and fork by default do not create any new namespaces.
    • there is one “current” namespace of each kind assigned to a process. It can be changed by unshare or setns. Set of namespaces (by default) is inherited by child processes.

    Whenever you do open(/proc/N/ns/net), it creates inode for this file,
    and all subsequent open()s will return file that is bound to the
    same namespace. Details are lost in the depths of kernel dentry cache.

    Also, each process has only one /proc/self/ns/net file entry, and
    bind mount does not create new instances of this proc file.
    Opening those mounted files are exactly the same as opening
    /proc/self/ns/net file directly (which will keep pointing to the
    namespace it pointed to when you first opened it).

    It seems that “/proc/*/ns” is half-baked like this.

    So, if you only need 2 namespaces, you can:

    • open /proc/1/ns/net
    • unshare
    • open /proc/self/ns/net

    and switch between the two.

    For more that 2 you might have to clone(). There seems to be no way to create more than one /proc/N/ns/net file per process.

    However, if you do not need to switch between namespaces at runtime, or to share them with other processes, you can use many namespaces like this:

    • open sockets and run processes for main namespace.
    • unshare
    • open sockets and run processes for 2nd namespace (netlink, tcp, etc)
    • unshare
    • …
    • unshare
    • open sockets and run processes for Nth namespace (netlink, tcp, etc)

    Open sockets keep reference to their network namespace, so they will not be collected until sockets are closed.

    You can also use netlink to move interfaces between namespaces, by sending netlink command on source namespace, and specifying dst namespace either by PID or namespace FD (the later you don’t have).

    You need to switch process namespace before accessing /proc entries that depend on that namespace. Once “proc” file is open, it keeps reference to the namespace.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following example is from Google to create a ColumnChart using Google Charts function
I am using the following function to create a simple accordion. The problem is
Using the following function: // remove multiple, leading or trailing spaces function trim(s) {
I am using following function to load the page. I have loads of links
I am using following function. Whenever i call it that time it will increase
I've been using the following function to make a more readable (supposedly) format for
I am using the following function to get all file sizes in a system
If I'm using the following function : clusters.prototype.shop_iqns_selected_class = function() { if(this.viewport_width < 980)
I'm using the following function that changes a calendar selection at a set time
I am using the following function to loop through a couple of open CDB

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.