I know that you can display interfaces by doing ip a show
. That only displays the interfaces that the host can see, but virtual interfaces configured by containers don't appear in this list. I've tried using ip netns
as well, and they don't show up either. Should I recompile another version of iproute2
? In /proc/net/fb_trie
, you can see the local/broadcast addresses for, I assume, as a use for the forwarding database.
Where can I find any of this information, or command to list all interfaces including containers?
To test this out, start up a container. In my case, it is a lxc container on snap. Do an ip a
or ip l
. It will show the host machine's view, but not the container configured interface. I'm grepping through procfs
, since containers are just cgrouped processes, but I don't get anything other than the fib_trie
and the arp entry. I thought it could be due to a netns namespace obfuscation, but ip netns
also shows nothing.
You can use conntrack -L
to display all incoming and outgoing connections that are established, because lxd needs to connection track the forwarding of the packets, but I'd like to list all ip addresses that are configured on the system, like how I'd be able to tell using netstat
or lsof
.
Best Answer
An interface, at a given time, belongs to one network namespace and only one. The init (initial) network namespace, except for inheriting physical interfaces of destroyed network namespaces has no special ability over other network namespaces: it can't see directly their interfaces. As long as you are still in init's pid and mount namespaces, you can still find the network namespaces by using different informations available from
/proc
and finally display their interfaces by entering those network namespaces.I'll provide examples in shell.
enumerate the network namespaces
For this you have to know how those namespaces are existing: as long as a resource keep them up. A resource here can be a process (actually a process' thread), a mount point or an open file descriptor (fd). Those resources are all referenced in
/proc/
and point to an abstract pseudo-file in thensfs
pseudo-filesystem enumerating all namespaces. This file's only meaningful information is its inode, representing the network namespace, but the inode can't be manipulated alone, it has to be the file. That's why later we can't just keep only the inode value (given bystat -c %i /proc/some/file
): we'll keep the inode to be able to remove duplicates and a filename to still have an usable reference fornsenter
later.process (actually thread)
The most common case: for usual containers. Each thread's network namespace can be known via the reference
/proc/pid/ns/net
: juststat
them and enumerate all unique namespaces. The2>/dev/null
is to hide whenstat
can't find ephemeral processes anymore.This can be done faster with the specialized
lsns
command which deals with namespaces, but appears to handle only processes (not mount points nor open fd as seen later):(which would have to be reformatted for later as
lsns -n -u -t net -o NS,PATH | while read inode path; do printf '%20u %s\n' $inode "$path"; done
)mount point
Those are mostly used by the
ip netns add
command which creates permanent network namespaces by mounting them, thus avoiding them disappearing when there is no process nor fd resource keeping them up, then also allowing for example to run a router, firewall or bridge in a network namespace without any linked process.Mounted namespaces (handling of mount and perhaps pid namespaces is probably more complex but we're only interested in network namespaces anyway) appear like any other mount point in
/proc/mounts
, with the filesystem typensfs
. There's no easy way in shell to distinguish a network namespace from an other type of namespace, but since two pseudo-files from the same filesystem (herensfs
) won't share the same inode, just elect them all and ignore errors later in the interface step when trying to use a non-network namespace reference as network namespace. Sorry, below I won't handle correctly mount points with special characters in them, including spaces, because they are already escaped in/proc/mounts
's output (it would be easier in any other language), so I won't bother either to use null terminated lines.open file descriptor
Those are probably even more rare than mount points except temporarily at namespace creation, but might be held and used by some specialized application handling multiple namespaces, including possibly some containerization technology.
I couldn't devise a better method than search all fd available in every
/proc/pid/fd/
, using stat to verify it points to ansfs
namespace and again not caring for now if it's really a network namespace. I'm sure there's a more optimized loop, but this one at least won't wander everywhere nor assume any maximum process limit.Now remove all duplicate network namespace references from previous results. Eg by using this filter on the combined output of the 3 previous results (especially from the open file descriptor part):
in each namespace enumerate the interfaces
Now we have the references to all the existing network namespaces (and also some non-network namespaces which we'll just ignore), simply enter each of them using the reference and display the interfaces.
Take the previous commands' output as input to this loop to enumerate interfaces (and as per OP's question, choose to display their addresses), while ignoring errors caused by non-network namespaces as previously explained:
The init network's inode can be printed with pid 1 as reference:
Example (real but redacted) output with a running LXC container,an empty "mounted" network namepace created with
ip netns add ...
having an unconnected bridge interface, a network namespace with an otherdummy0
interface, kept alive by a process not in this network namespace but keeping an open fd on it, created with:and a running Firefox which isolates each of its "Web Content" threads in an unconnected network namespace (all those down
lo
interfaces):