Home / Alt manpages / proc_self(5)

  • proc_self(5)
  • File format
  • linux

Use /proc/self Without Inspecting the Wrong Process

You will finish able to inspect the process you actually mean, explain what /proc/self/ does, and avoid a subtle shell mistake where a helper command reads its own process information instead. The examples use the installed Linux man-pages 6.7 documentation, provided by the Debian package manpages version 6.7-2.

Allow about ten minutes. You need a shell and a mounted /proc filesystem. The commands are read-only and need no elevated privileges for your own process. Reading another process can be restricted by its credentials, dumpable state or the host's procfs policy.

1. Understand the name

/proc is a kernel-provided pseudo-filesystem. Its numeric directories represent process IDs, so /proc/1234/ describes process 1234 while it exists. The special directory /proc/self/ is an alias for the numeric directory belonging to the process that is accessing procfs.

That last phrase is the detail to keep in view. It does not mean the process you happen to call "your process", and it does not mean the parent shell. It means the process performing the access.

Checkpoint: confirm that procfs is present before using the examples.

$ test -r /proc/self/status && printf '%s\n' 'procfs is readable'
procfs is readable

If this test fails, stop and investigate how procfs is mounted or restricted. Do not create a replacement directory: an ordinary directory named /proc would not provide process information.

2. Compare the shell PID with /proc/self

Run this as one shell command:

$ sh -c 'printf "shell PID: %s\n" "$$"; readlink /proc/self; readlink /proc/$$'
shell PID: 241411
241421
self

Your numbers will differ. The first number is the shell's PID. readlink /proc/self is performed by the separately executed readlink process, so it reports that command's PID. The next command asks for /proc/ followed by the shell's PID; its result is self because that numeric directory is the one represented by the alias from the shell's point of view.

The exact output is timing-dependent because short-lived helper processes can exit immediately. What matters is that the first and second numbers need not match. This is the most common distraction trap when debugging a shell command: replacing a direct file access with a helper can change which process accesses the path.

3. Inspect a shell by its numeric PID

Use $$ when the shell itself is the process you want. This example reads the shell's status file through its numeric directory:

$ sh -c 'grep -E "^(Name|Pid|PPid|Uid|Gid):" /proc/$$/status'
Name:   sh
Pid:    242253
PPid:   236558
Uid:    1004    1004    1004    1004
Gid:    1004    1004    1004    1004

The spacing and numbers vary, but the Pid: value should match the shell PID when the shell reads the file directly. The status file contains many more fields, including credentials and parent-process information. Select only fields you need rather than treating the file as a stable, human-facing report.

A similar-looking command can inspect the wrong process:

$ sh -c 'cat /proc/self/comm'
cat

cat opens /proc/self/comm, so the result names cat. To read the shell's command name, use its numeric PID instead:

$ sh -c 'cat /proc/$$/comm'
sh

For a compiled program, record its PID and open /proc/<pid>/ from that program or from a trusted observer. For a shell script, remember that $$ identifies the current shell, while an external command launched by the script has its own PID. A subshell and some shell constructs can also change which process is doing the work, so verify with the Pid: field when the distinction matters.

4. Use /proc/self in a program

Inside a program, /proc/self/ is useful when the program wants a path to its own procfs directory without first formatting its PID. For example, a process can open /proc/self/status to read its own status, or /proc/self/fd/ to examine its file descriptors. The kernel resolves the alias for the process making each access.

Do not pass a path containing /proc/self/ to another program and assume it will still refer to the original process. The receiving program becomes the accessor. If the identity must survive a hand-off, pass a numeric PID, an already opened file descriptor, or another explicit identifier that matches the operation's security model.

For a quick check from a shell, compare an explicit PID with the status it names:

$ target_pid=$$
$ grep -E '^(Name|Pid|PPid):' "/proc/$target_pid/status"
Name:   sh
Pid:    242253
PPid:   236558

The quoted path prevents accidental word splitting if you later build a more complex path. The PID itself should come from a trusted source. Never accept an unvalidated PID from an untrusted user and combine it with a path for a privileged operation.

5. Check ownership and access before escalating

Process directories are normally owned according to the target process's effective user and group. Linux can instead expose the files as owned by root when the target process is not dumpable, as a security measure. In a non-initial user namespace, the relevant namespace root mapping can be used. A container's root user therefore does not automatically have the same view as root on the host.

Inspect ownership without changing anything:

$ stat -Lc 'path=%n owner=%U:%G mode=%A' /proc/self /proc/1
path=/proc/self owner=andy:dixon mode=dr-xr-xr-x
path=/proc/1 owner=root:root mode=dr-xr-xr-x

The output depends on the user, namespaces, kernel and target process. A failed read is not proof that the file is absent. It can indicate that the target is protected or that your procfs configuration hides information.

Do not solve a permission error by making procfs broadly readable or by changing a process's dumpable setting without understanding the exposure. Those changes can reveal credentials, command arguments, memory mappings or other sensitive process details. If you need to inspect a service, use the smallest privilege your host policy permits, and keep the access read-only.

6. Recover from a misleading result

If a command reports a name or PID you did not expect, first print the PID of the process you intended to inspect. Then replace /proc/self/ with /proc/$pid/ before adding more diagnostic commands. This removes the ambiguity at the path level.

If the numeric directory has disappeared, the process probably exited and its PID may later be reused. Capture the information you need while the process is alive, and do not treat a PID alone as a permanent identity. If access is denied, stop at the error and review the target's credentials, namespace and procfs restrictions rather than retrying as root by habit.

Done means

  • You can state that /proc/self/ means the process accessing procfs.
  • You know that cat /proc/self/... normally reports on cat, not its parent shell.
  • You use /proc/$$/ for the current shell and verify the Pid: field when it matters.
  • You pass an explicit, trusted numeric PID when another process must inspect a target.
  • You treat ownership and access failures as security boundaries, not invitations to weaken procfs.