Stop letting orphaned namespaces hang around like ghosts
Container Basics On Linux (Namespaces/Cgroups)
Stop letting orphaned namespaces hang around like ghosts
🧩 The Challenge
Sometimes you stop a container but the kernel keeps holding onto the network or mount namespace, leaving you with phantom interfaces you can’t get rid of. It drives me up the wall when I’m trying to clean up a test rig and nothing will unmount because of a “device or resource busy” error.
💡 The Fix
Use the lsns command to spot the stragglers and then follow that up with the unshare utility to force a cleanup of those stale environments. It saves you from having to reboot just to clear out a stuck virtual interface.
lsns -t net
# Identify the PID keeping the namespace alive and kill it
# If it's a persistent mount namespace, use nsenter to jump in and unmount
nsenter -t <PID> -m umount /some/stuck/path
⚙️ Why It Works
Namespaces are just kernel structures that stick around as long as a process is attached to them or a file is mounted within them. Getting inside that namespace with nsenter lets you perform the housecleaning from the correct perspective rather than shouting at your host OS.
🚀 Pro-Tip: Keep an eye on /proc/<PID>/ns to see exactly which namespace types a process is pinning.
Linux Tips & Tricks | © ngelinux.com | 10/10/2026
