The Linux temporary directory follows the convention of UNIX in general and resides at /tmp, which is the standard temporary directory path in POSIX. While we can change its underlying partition, how we enforce a new temporary path depends on the context.
In this tutorial, we explore ways to change the Linux temporary directory path. First, we discuss a standard method to tell an application it should store its temporary data elsewhere. After that, we turn to two more direct methods for changing what and where /tmp is.
2. Temporary Directory Variables and Location
The default Linux temporary directory path is /tmp. Still, we might be able to influence that in certain environments by setting variables in the shell.
Let’s see which variables affect our environment by setting each with export and testing:
$ export TMP=/home/baeldung/temp
$ export TEMP=/home/baeldung/temp
$ export TMPDIR=/home/baeldung/temp
Notably, only the export of $TMPDIR changes the behavior of mktemp.
So, similar to the POSIX mktemp, the interpreter prioritizes $TMPDIR over others, including the default. In fact, this is expected as $TMPDIR is in the POSIX standard as well:
This variable shall represent a pathname of a directory
made available for programs that need a place to create
In other words, although Python is just an example, some applications may employ $TEMP and $TMP, but most should check and adhere to $TMPDIR as the standard way to change the path of the temporary directory.
Now, let’s see what we can do about the applications that don’t use any of the above.
3. Change the Temporary Directory Location
Like other Linux filesystem objects, directories can be pointed to by links. Although with potential side effects, we can even replace /tmp with a link, switching up the actual directory or the reference path.
3.1. Link to /tmp
Of course, we can always link back to /tmp.
For example, an application may use /var/app/tmp instead of the system’s default temporary directory. In this case, we can avoid having to change environment variables by using ln:
$ mv /var/app/tmp/* /tmp
$ rm --recursive --force /var/app/tmp
$ ln --symbolic /tmp /var/app/tmp
There are several steps to this process:
- move all data from /var/app/tmp to /tmp, being careful when replacing
- remove /var/app/tmp
- recreate /var/app/tmp as a symbolic link (symlink) to /tmp
This way, our application should be able to directly employ /tmp without further modifications. One main drawback of this method is the possibility of the application to delete the /var/app/tmp directory and recreate it.
3.2. /tmp Link
Alternatively, we can carefully delete /tmp and recreate it as a link, which points elsewhere:
$ mkdir /xtmp
$ chmod 1777 /xtmp
$ chown root:root /xtmp
$ cp /xtmp/* /tmp
$ rm --recursive --force /tmp
$ ln --symbolic /xtmp /tmp
This more complex process involves multiple commands:
- create a new directory /xtmp
- make the permissions of /xtmp like the permissions as a temporary directory
- remove the original /tmp (dangerous)
- recreate /tmp as a symbolic link to /xtmp
Still, there are several pitfalls to this approach:
- /tmp is often a mount point for a separate partition, requiring further steps involving /etc/fstab
- /tmp must have the correct permissions to avoid issues
- depending on the time the partition behind /xtmp is mounted, earlier boot services may fail
- using a link instead of /tmp may cause problems for some applications
If we consider all of the above, this is still a viable solution.
In this article, we looked at changing the Linux temporary directory path.
In conclusion, as long as they check them, we can tell applications to use another path for storing temporary files via environment variables. Still, we always have the alternative of changing what points to /tmp and where /tmp itself points.