How to Change Default /tmp Directory to /home/user/tmp in Debian-Based Systems Using Environment Variables


3 views

On Debian-based Linux systems, the default temporary directory (/tmp) is shared across all users and processes. This becomes problematic when:

  • Running multiple instances of Java applets/applications
  • Working with sensitive temporary files that need user isolation
  • Managing disk space quotas where /tmp is on a separate partition

Linux systems recognize several environment variables for temporary file locations:

TMPDIR
TMP
TEMPDIR
TEMP

These variables are checked in order by most applications, with TMPDIR being the most widely supported standard.

For a persistent change across all terminal sessions:

# Add to ~/.bashrc
export TMPDIR="/home/user/tmp"
export TMP="$TMPDIR"
export TEMP="$TMPDIR"
export TEMPDIR="$TMPDIR"

# Create the directory if it doesn't exist
mkdir -p "$TMPDIR"

For Java applications, you can set this in the runtime environment:

java -Djava.io.tmpdir=/home/user/tmp -jar your_applet.jar

Or set it programmatically in your Java code:

System.setProperty("java.io.tmpdir", "/home/user/tmp");

To change the default system-wide (requires root):

# Edit /etc/environment
TMPDIR=/home/user/tmp
TMP=/home/user/tmp
TEMP=/home/user/tmp
TEMPDIR=/home/user/tmp

Check if your changes took effect:

echo $TMPDIR
java -XshowSettings:properties -version 2>&1 | grep tmpdir

Permission problems:

chmod 1777 /home/user/tmp  # Sets sticky bit like /tmp

Applet-specific considerations:

Make sure your Java security policy allows accessing the custom tmp location:

grant {
    permission java.io.FilePermission "/home/user/tmp/-", "read,write,delete";
};

When running multiple instances of Java applets that rely on temporary files, using the default system-wide /tmp directory can cause conflicts. Each instance may overwrite or lock files created by others, leading to unpredictable behavior.

Debian-based systems (and most Unix-like systems) support these environment variables for temporary directories:

export TMPDIR=/home/user/tmp
export TEMP=/home/user/tmp
export TMP=/home/user/tmp

Java specifically looks for these variables in this order:

  1. TMPDIR
  2. TEMP
  3. TMP

To make this change persistent across sessions:

echo 'export TMPDIR=/home/user/tmp' >> ~/.bashrc
source ~/.bashrc

For Java applications, you can also set the temp directory via system properties:

java -Djava.io.tmpdir=/home/user/tmp -jar yourapp.jar

Or programmatically in your Java code:

System.setProperty("java.io.tmpdir", "/home/user/tmp");

To confirm the temporary directory is being used:

In bash:

echo $TMPDIR

In Java:

System.out.println(System.getProperty("java.io.tmpdir"));
  • Permission issues: Ensure the user has write access to the new directory
  • Directory existence: The directory must exist before being used
  • Applet sandbox: Some Java security policies may restrict temp directory changes