Skip to main content

Linux namespace relationships library

Project description

Linux Kernel Namespace Relations

NOTE: Python 3.6+ supported only

This Python 3 package allows discovering the following Linux Kernel namespace relationships and properties, without having to delve into ioctl() hell:

  • the owning user namespace of another Linux kernel namespace.
  • the parent namespace of either a user or a PID namespace.
  • type of a Linux kernel namespace: user, PID, network, ...
  • owner user ID of a user namespace.

See also ioctl() operations for Linux namespaces for more background information of the namespace operations exposed by this Python library.

Installation

$ pip3 install linuxns-rel

API Documentation

Please head over to our linuxns_rel API documentation on GitHub Pages.

CLI Examples

List User Namespaces

You can either use a simple lsuserns or sudo lsuserns -- the latter ensures that you'll see all user namespaces instead of only those you have user access to.

$ sudo lsuserns 

may yield something like this, a pretty hierarchy of Linux kernel user namespaces:

user:[4026531837] process "init" owner root (0)
 ├── user:[4026532465] process "firefox" owner foobar (1000)
 ├── user:[4026532523] process owner foobar (1000)
 │   └── user:[4026532524] process owner foobar (1000)
 │       └── user:[4026532525] process owner foobar (1000)
 │           └── user:[4026532526] process "bash" owner foobar (1000)
 ├── user:[4026532699] process "firefox" owner foobar (1000)
 ├── user:[4026532868] process "firefox" owner foobar (1000)
 └── user:[4026532467] process owner foobar (1000)

If you have either Chromium or/and Firefox running, then these will add some user namespaces in order to sandbox their inner workings. And to add in some more hierarchical user namespaces, in another terminal session simply issue the following command:

$ unshare -Ur unshare -Ur unshare -Ur unshare -Ur

Debian users may need to sudo because their distro's default configuration prohibits ordinary users to create new user namespaces.

List PID Namespaces

$ sudo lspidns 

shows the PID namespace hierarchy, such as:

pid:[4026531836] process "init" owner user:[4026531837] root (0)
 ├── pid:[4026532532] process "sh" owner user:[4026531837] root (0)
 └── pid:[4026532468] process "chromium-browser --type=zygote" owner user:[4026532467] foobar (1000)
     ├── pid:[4026532464] process "chromium-browser" owner user:[4026532589] foobar (1000)
     ├── pid:[4026532466] process "chromium-browser" owner user:[4026532589] foobar (1000)
     └── pid:[4026532590] process "chromium-browser" owner user:[4026532589] foobar (1000)

Don't worry that the PID namespace hierarchy doesn't match the user namespace hierarchy. That's perfectly fine, depending on which programs run. In our example, we didn't create new PID namespaces when using unshare, so we see only additional PID namespaces created by Chromium (Firefox doesn't create them though).

Namespace Graph

Okay, with the famous graphviz installed (apt-get install graphviz) now simply do:

$ sudo -E graphns

Note: -E ensures that the graph viewer will correctly use the your desktop environment theme.

...and you get something fancy with arrows, et cetera, in a cute (erm, "qute") little viewer window:

hierarchical namespace graph

The view window supports these actions:

  • mouse drag: move the visible area around inside the view window (usefull when either the graph is really large or when zoomed in).
  • mouse wheel: zoom in/out.
  • +/- keys: zoom in/out.
  • 1 key: reset zoom to 1x.
  • s key: save image to an SVG file.
  • q key: close and exit the viewer.
  • h key: show help on mouse and keys.

Potentially FAQs

  1. Q: Why do get_userns() and get_parentns() return file objects (TextIO) instead of filesystem paths?

    A: Because that's what the Linux namespace-related ioctl() functions are giving us: open file descriptors referencing namespaces in the special nsfs namespace filesystem. There are no paths associated with them.

  2. Q: What argument types do get_nstype(), get_userns(), get_parentns(), and get_owner_uid() expect?

    A: Choose your weapon:

    • a filesystem path (name), such as /proc/self/ns/user,
    • an open file object (TextIO), such as returned by open(),
    • an open file descriptor, such as returned by fileno() methods.
  3. Q: Why does get_parentns() throw an PermissionError?

    A: There are multiple causes:

    • you didn't specify a PID or user namespace,
    • the parent namespace either doesn't exist,
    • or the parent namespace is inaccessible to you,
    • oh, you really have no access to the namespace reference.
  4. Q: Why does get_userns() throw an PermissionError?

    A: You don't have access to the owning user namespace.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

linuxns-rel-1.0.3.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

linuxns_rel-1.0.3-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file linuxns-rel-1.0.3.tar.gz.

File metadata

  • Download URL: linuxns-rel-1.0.3.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.3

File hashes

Hashes for linuxns-rel-1.0.3.tar.gz
Algorithm Hash digest
SHA256 479f9aa8dc96c68d60788fac27e6f971a1076399fc1d98ffee8399011aba4080
MD5 f6166c678c2656ee085434217a1e3133
BLAKE2b-256 d880f8355f6d0c9f7619e9c13999c98cbd4de968075f4e17c9afba39a127066c

See more details on using hashes here.

File details

Details for the file linuxns_rel-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: linuxns_rel-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.3

File hashes

Hashes for linuxns_rel-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3ba02f0abdc4884eff7de4fd2512623dd97f846df953859de9a54ddc43ba97d6
MD5 1b1de9fc7a27b80f2046ea696c97d35e
BLAKE2b-256 a1e4fc98bae0042b826320ed3f6c16506c53181daea137d9860f7c8aeb6e828d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page