TMPDIR

TMPDIR is an environment variable used primarily in Unix and Unix-like operating systems to specify a directory path designated for temporary file storage by applications and system processes. When TMPDIR is set, programs that adhere to the X/Open Base Specifications (also known as POSIX) and many scripting languages consult its value to determine where to create temporary files instead of using default locations such as /tmp or /var/tmp.

Definition and Purpose

  • Environment Variable: A name‑value pair in a process’s environment that can be read by the process and its descendants.
  • Function: Provides a configurable location for transient data, enabling users or administrators to redirect temporary storage to alternative filesystems (e.g., a ramdisk, a user‑specific directory, or a location with greater space).

Standardization and Specification

  • Defined in the POSIX.1‑2001 and later specifications, which state that if the TMPDIR variable is set, its value shall be used as the directory for temporary files; otherwise, implementations shall fall back to implementation‑defined defaults.
  • The variable is referenced by functions such as tmpfile(), tmpnam(), and mkstemp() in the C standard library, as well as utilities like mktemp and scripting language libraries (e.g., Python’s tempfile, Perl’s File::Temp).

Typical Values and Defaults

  • Commonly set to paths such as /tmp, /var/tmp, $HOME/tmp, or /run/user/<uid> on modern Linux distributions.
  • In multi‑user environments, values may be user‑specific (e.g., $XDG_RUNTIME_DIR) to avoid permission conflicts.

Interaction with Related Variables

  • TMPDIR is often considered alongside TMP and TEMP, which serve similar purposes on other platforms (e.g., Windows). Some software checks these variables in order of precedence: TMPDIR, then TMP, then TEMP.
  • On systems where none of these variables are defined, the fallback is typically a hard‑coded directory such as /tmp.

Security Considerations

  • Because temporary files can be exploited for privilege escalation or information leakage, best practices recommend:
    1. Ensuring that the directory referenced by TMPDIR is owned by the invoking user and has restrictive permissions (e.g., mode 0700).
    2. Using safe APIs that create uniquely named files with appropriate permissions (e.g., mkstemp() instead of manually constructing file names).
    3. Avoiding world‑writable directories without the sticky bit set, as these permit other users to delete or modify files.

Operational Use Cases

  • User Sessions: Shell startup scripts (e.g., .profile, .bashrc) may set TMPDIR to a per‑session directory to isolate temporary data.
  • Build Systems: Tools such as make, gcc, or autoconf may rely on TMPDIR to store intermediate compilation artifacts.
  • Containerization: Container runtimes often set TMPDIR inside the container’s filesystem to prevent temporary files from affecting the host.

Configuration

  • The variable can be set in the shell environment, for example:
    export TMPDIR=/home/user/tmp
    
  • System‑wide defaults may be defined in login scripts (e.g., /etc/profile) or service unit files (systemd) using Environment=TMPDIR=….

Historical Context

  • The concept of a dedicated temporary directory predates POSIX and is observed in early UNIX systems where /tmp served as the default location. The introduction of TMPDIR allowed more flexible configuration, accommodating diverse hardware (e.g., separate partitions for temporary storage) and security models.

Relevant Standards

  • POSIX.1‑2008, Section 12.3 “Environment Variables”;
  • IEEE Std 1003.1, “Portable Operating System Interface (POSIX)”.

See Also

  • TMP, TEMP (environment variables)
  • mktemp, tmpfile, mkstemp (POSIX functions)
  • XDG_RUNTIME_DIR (freedesktop.org specification)
  • Temporary file handling in programming languages (e.g., Python’s tempfile module).
Browse

More topics to explore