All articles

Inspect a Linux process environment safely

5 minutes read


Linux publication

Share this article

𝕏✉

Illustration for Inspect a Linux process environment safely

Environment variables often explain why a service finds the wrong executable, uses an unexpected locale, or connects through a proxy. They can also contain database passwords, API tokens, and credentials. Inspect the smallest possible slice and keep it out of shared logs.

Understand the procfs format

/proc/PID/environ contains the environment present when the process started, with entries separated by null bytes rather than newlines. A process can alter its own memory later, so this file is not guaranteed to reflect every runtime change.1

Access is also subject to a ptrace-based permission check. Root in one context does not guarantee visibility across every namespace or security policy.1

To read selected low-risk keys:

pid=12345
tr '\0' '\n' < "/proc/$pid/environ" \
  | grep -E '^(LANG|LC_ALL|TZ)='

Do not begin with an unfiltered dump. Even shell history, terminal scrollback, and support tickets can become unintended secret stores.

Try it with a temporary child

lab=$(mktemp -d)
child_pid=
cleanup() {
  if [ -n "$child_pid" ] && kill -0 "$child_pid" 2>/dev/null; then
    kill -TERM "$child_pid"
    wait "$child_pid"
  fi
  rm -f -- "$lab/ready"
  rmdir -- "$lab" 2>/dev/null || true
}
trap cleanup 0 1 2 15

DEMO_COLOR=green sh -c ': > "$1/ready"; exec sleep 30' sh "$lab" &
child_pid=$!

attempt=0
while [ ! -e "$lab/ready" ]; do
  if ! kill -0 "$child_pid" 2>/dev/null || [ "$attempt" -ge 50 ]; then
    printf '%s\n' 'child did not become ready' >&2
    exit 1
  fi
  attempt=$((attempt + 1))
  sleep 0.1
done

tr '\0' '\n' < "/proc/$child_pid/environ" \
  | grep '^DEMO_COLOR='

kill -TERM "$child_pid"
wait "$child_pid"
child_pid=
cleanup
trap - 0 1 2 15

$! is valid as soon as the shell forks, but until the child calls execve, its procfs environment is still a copy of the parent shell’s. The child creates the ready file only after sh has started with DEMO_COLOR in its environment, so the loop waits for that point before reading.1

The filter names a variable whose value you already know is safe. That is a better operational habit than dumping the environment and redacting afterward.

Compare environment and arguments

The process command line lives in /proc/PID/cmdline and also uses null separators:

tr '\0' ' ' < "/proc/$pid/cmdline"
printf '\n'

Command-line arguments and environment variables are separate inputs. Both may expose secrets to users who can inspect the process, so applications should prefer protected files, credential stores, or dedicated secret-passing mechanisms where available.23

For a systemd service, configuration may come from Environment=, EnvironmentFile=, a manager environment, or a wrapper script. Compare the live process with systemctl cat example.service and any referenced drop-ins.

[!CAUTION] Never paste a complete production environment into chat, issue trackers, or monitoring output. Query only the key you need and redact its value when proving presence is enough.

Sources and further reading
  1. proc_pid_environ(5)
  2. environ(7)
  3. proc_pid_cmdline(5)