Inside a Linux Command: Processes, Files, Containers vs VMs
System DesignModel answerLast updated
Trace a Linux command from shell parsing to exit status, explain path lookup, permissions, file descriptors and /proc, then compare containers and VMs by isolation boundary.
You are asked to explain, at the operating-system level, what Linux does when people use everyday commands and when they choose an isolation technology. Name the user-space work, the system calls, the kernel objects involved, the permission checks, and the ways each step can fail. Do not treat any command as a black box.
State your assumptions first (for example, a Bash-like shell on Linux, and whether namespaces and cgroups are in scope).
Part 1: From typed line to running program
- A user types an external command. Describe everything between the Enter key and the shell showing the next prompt: parsing, expansion, executable lookup, process creation, program loading, waiting, and exit status.
- Contrast this with a builtin such as
cd. Why can't a separate program change the shell's working directory? - At a high level, how do
ls,pwd,ip, and an older tool such asifconfigget their answers?
Part 2: Files, directories, permissions, and /proc
- How does a process turn a path string into an open file? Cover directory entries, inodes, and file descriptors.
- How is a directory different from a regular file? How do read, write, and execute bits apply to each, and which credentials are checked?
- What do the main top-level directories hold?
- Why is
/procnot an ordinary directory on disk?
Part 3: Containers versus virtual machines
- Compare the two on isolation boundary, kernel sharing, startup time, resource overhead, security, and operations.
- Give example workloads where each is the better choice, and say what you would do when neither extreme fits.
Hints
Hint 1
For Part 1, think about which process attributes are inherited across fork and which are replaced by exec. Working directory belongs to a specific process.
Hint 2
For Part 2, separate three layers: the directory entry (name to inode), the inode (owner, mode, data), and the per-process file descriptor that points to an open file description with its own offset.
Hint 3
For Part 3, ask which component an attacker must compromise to escape: a shared kernel with a large syscall surface, or a hypervisor with a narrow interface. Then connect that to startup time and density.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Assumptions
I assume Linux with a Bash-like shell, executables found through PATH, and namespaces and cgroups in scope. Exact details differ across shells and kernel versions, but the contracts described here are stable.
Part 1: From typed line to running program
Shell work (user space)
- The shell reads the line and tokenizes it, respecting quotes and operators such as
|,>, and&&. - It performs expansions: variables, command substitution, tilde, and globbing (
*.logbecomes a list of names before the program ever runs). - It decides whether the command is a builtin, function, alias, or external program. If it is external and contains no
/, the shell searches eachPATHdirectory in order (often using a cached hash table).
Creating and loading the process
4. The shell calls fork (in practice clone). The child is a copy-on-write duplicate, so it is cheap.
5. In the child, the shell sets up redirections and pipes with open, pipe, and dup2, resets signal handlers, and may put the job in its own process group.
6. The child calls execve(path, argv, envp). The kernel checks the execute permission, reads the ELF header or the #! interpreter line, replaces the child's address space, and builds the initial stack with arguments and environment. The dynamic loader then maps shared libraries and jumps to main. The PID, open descriptors (unless close-on-exec), and working directory survive exec.
7. The parent calls waitpid, then reads the exit status into $?. Normal exit codes are 0–255. A death by signal is reported as 128 plus the signal number. Common shell codes are 127 (not found) and 126 (found but not executable).
Failure paths: ENOENT (not on PATH, or a bad interpreter in the shebang line), EACCES (no execute bit, or noexec mount), ENOEXEC (bad format), ENOMEM, and EAGAIN from fork when process limits are hit.
Why cd is a builtin: the working directory is an attribute of a process. A child's chdir changes only the child, which then exits. To change the shell's own directory, the shell itself must call chdir. The same reasoning applies to export, ulimit, and umask.
How common tools get answers
ls:openaton the directory, thengetdents64to read entries. With-lit also callsstatx/lstatper entry and resolves user and group names through NSS (/etc/passwd, LDAP, etc.). It sorts in user space and writes to stdout.pwd: the builtin usually prints the logical$PWD./bin/pwd -Pcallsgetcwd, where the kernel walks from the process's cwd dentry up to the root.ip: opens a netlink socket (AF_NETLINK,NETLINK_ROUTE) and sends requests such asRTM_GETLINK,RTM_GETADDR, andRTM_GETROUTE. The kernel replies with structured messages.ifconfig(net-tools): older interface, built onioctlcalls such asSIOCGIFCONFplus/proc/net/*. It shows less (no multiple-address detail, limited policy routing), which is whyipreplaced it.
Part 2: Files, directories, permissions, /proc
Path resolution: start at the root (absolute path) or the cwd (relative). For each component the kernel looks up the name in the current directory (usually from the dentry cache), checks that the caller has execute (search) permission on that directory, follows mount points and symlinks (with a loop limit of about 40), and ends at an inode. Names are just directory entries, so several hard links can point to one inode.
File descriptors: open returns a small integer indexing the process's descriptor table. Each entry points to an open file description holding the offset and flags, which in turn refers to the inode. fork and dup share a description, so parent and child share an offset. A separate open of the same file gets a new description with its own offset.
Directories vs regular files: a directory's content is a mapping from names to inodes.
r: list names.x: traverse, meaning access entries by name (cd,stat,openof a child).w(together withx): create, delete, or rename entries. Deleting depends on the directory's permissions, not the file's. The sticky bit (/tmp) restricts deletion to the file's owner.- Having
rwithoutxlets you see names but not open or stat them. Havingxwithoutrlets you open a file whose name you already know.
Permission check: the kernel uses the process's effective UID and GID plus supplementary groups. It picks one class: owner if the UID matches, otherwise group, otherwise other. The classes are not cumulative, so an owner with fewer bits than "other" is still denied. ACLs extend this. Root, or a process with CAP_DAC_OVERRIDE, bypasses most checks. Executing a binary needs x. Running a script by name needs x, and the interpreter also needs read access to it. umask shapes the mode of newly created files.
Top-level layout: /bin, /sbin, /lib (often symlinks into /usr), /usr (installed software), /etc (system configuration), /var (logs, spool, caches), /home and /root, /tmp, /dev (device nodes), /boot, /run (volatile runtime state), /mnt and /media, /opt, /proc and /sys.
/proc: a virtual filesystem (procfs). Nothing is stored on disk. File contents are generated by kernel code when you read them. /proc/<pid>/ exposes cmdline, status, maps, fd/, cwd, and exe. System-wide files include meminfo and net/dev. Tools like ps, top, and lsof are largely readers of these files. /sys plays a similar role for devices and kernel objects.
Part 3: Containers vs VMs
| Aspect | Container | Virtual machine |
|---|---|---|
| Boundary | Processes on the host kernel, isolated by namespaces (pid, mnt, net, uts, ipc, user, cgroup), limited by cgroups, restricted by capabilities, seccomp, and LSMs | Hypervisor with hardware virtualization (VT-x/AMD-V, nested paging); guest runs its own kernel |
| Kernel | Shared with the host | One per guest |
| Startup | Milliseconds to about a second | Seconds to tens of seconds; microVMs can be around 100–200 ms |
| Overhead | Little beyond the app (MBs) | Guest OS memory and disk (hundreds of MB or more), plus virtual device cost |
| Density | Very high | Lower |
| Packaging | Layered images (overlayfs), portable across hosts with a compatible kernel | Full disk image; any OS |
| Main risk | A kernel bug or misconfiguration (privileged mode, mounted docker socket) can mean host escape | Hypervisor or device-emulation bug; the attack surface is much narrower |
Choosing: containers suit trusted or semi-trusted microservices, CI jobs, and fast-scaling stateless workloads. VMs suit untrusted multi-tenant code, a different OS or kernel version, kernel modules, or strict compliance boundaries. Many production systems layer them: containers inside VMs, or hybrids such as Kata Containers, Firecracker microVMs, and gVisor, which trade some performance for a stronger boundary.
Common mistakes
- Saying a container is "a lightweight VM". It is a constrained process group with no separate kernel.
- Claiming
cdis external, or that a child can change the parent's directory. - Describing permissions as cumulative across owner, group, and other.
- Forgetting that directory
xgoverns traversal. - Treating
/procfiles as stored data. - Declaring one isolation model universally better instead of tying the choice to threat model, cost, and startup needs.