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
TMPDIRvariable 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(), andmkstemp()in the C standard library, as well as utilities likemktempand scripting language libraries (e.g., Python’stempfile, Perl’sFile::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
TMPDIRis often considered alongsideTMPandTEMP, which serve similar purposes on other platforms (e.g., Windows). Some software checks these variables in order of precedence:TMPDIR, thenTMP, thenTEMP.- 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:
- Ensuring that the directory referenced by
TMPDIRis owned by the invoking user and has restrictive permissions (e.g., mode0700). - Using safe APIs that create uniquely named files with appropriate permissions (e.g.,
mkstemp()instead of manually constructing file names). - Avoiding world‑writable directories without the sticky bit set, as these permit other users to delete or modify files.
- Ensuring that the directory referenced by
Operational Use Cases
- User Sessions: Shell startup scripts (e.g.,
.profile,.bashrc) may setTMPDIRto a per‑session directory to isolate temporary data. - Build Systems: Tools such as
make,gcc, orautoconfmay rely onTMPDIRto store intermediate compilation artifacts. - Containerization: Container runtimes often set
TMPDIRinside 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) usingEnvironment=TMPDIR=….
Historical Context
- The concept of a dedicated temporary directory predates POSIX and is observed in early UNIX systems where
/tmpserved as the default location. The introduction ofTMPDIRallowed 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
tempfilemodule).