Understanding Linux Namespaces with lsns (with examples)

Understanding Linux Namespaces with lsns (with examples)

Linux namespaces are an important feature of the Linux kernel that provide process isolation and resource management. The lsns command is used to list information about namespaces in Linux. In this article, we will explore 8 different use cases of the lsns command with detailed code examples, motivations, explanations, and example outputs for each use case.

Use Case 1: List all namespaces

To list all namespaces on the system, simply run the lsns command without any arguments:

lsns

Motivation: This use case helps us understand the namespaces present in the system and provides an overview of the level of isolation and resource management.

Example Output:

NS TYPE  NPROCS   PID USER     COMMAND
0  mnt   2       1   root     /sbin/init
1  uts   1       1   root     /sbin/init
2  ipc   1       1   root     /sbin/init
...

Use Case 2: List namespaces in JSON format

To list namespaces in JSON format, use the --json option:

lsns --json

Motivation: Listing namespaces in JSON format allows for easy parsing and integration with other tools or scripts.

Example Output:

[
    {
        "NS": 0,
        "TYPE": "mnt",
        "NPROCS": 2,
        "PID": 1,
        "USER": "root",
        "COMMAND": "/sbin/init"
    },
    {
        "NS": 1,
        "TYPE": "uts",
        "NPROCS": 1,
        "PID": 1,
        "USER": "root",
        "COMMAND": "/sbin/init"
    },
    ...
]

Use Case 3: List namespaces associated with a specific PID

To list namespaces associated with a specific PID, use the --task option followed by the PID:

lsns --task <PID>

Motivation: This use case allows us to view the specific namespaces that a process is associated with, providing insights into its isolation and resource management.

Example Output:

NS TYPE  NPROCS   PID USER     COMMAND
0  mnt   2       1   root     /sbin/init
1  uts   1       1   root     /sbin/init
2  ipc   1       1   root     /sbin/init
...

Use Case 4: List namespaces of a specific type only

To list namespaces of a specific type only, use the --type option followed by the desired type (mnt, net, ipc, user, pid, uts, cgroup, time):

lsns --type <type>

Motivation: This use case allows us to focus on a particular type of namespace, enabling us to understand the isolation and resource management for that specific aspect.

Example Output (listing only mnt namespaces):

NS TYPE  NPROCS   PID USER     COMMAND
0  mnt   2       1   root     /sbin/init
...

Use Case 5: List namespaces with specific output format

To list namespaces with a specific output format, use the --output option followed by the desired format (available formats: NS, TYPE, NPROCS, PID, USER, COMMAND):

lsns --output <format>

Motivation: This use case allows us to customize the output of the lsns command, showing only the relevant information for our needs.

Example Output (showing only namespace ID, type, PID, and command):

NS TYPE  PID COMMAND
0  mnt   1   /sbin/init
1  uts   1   /sbin/init
...

Conclusion

In this article, we explored 8 different use cases of the lsns command, covering a wide range of scenarios for listing information about namespaces in Linux. These examples demonstrated the versatility and usefulness of the lsns command in understanding process isolation and resource management in a Linux system.

Tags :

Related Posts

How to use the command 'join' (with examples)

How to use the command 'join' (with examples)

The join command is a powerful tool that allows you to join lines of two sorted files on a common field.

Read More
How to use the command redis-server (with examples)

How to use the command redis-server (with examples)

Redis is a popular open-source in-memory data structure store used as a database, cache, and message broker.

Read More
How to list all files inside a compressed archive using the command 'lz' (with examples)

How to list all files inside a compressed archive using the command 'lz' (with examples)

The ’lz’ command is used to list all files inside a compressed archive, specifically in a ‘.

Read More