Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Suppose there's a binary application that always writes its data to /tmp.

How could I spoof/mock /tmp for the sake of this binary as some other directory (e.g. home/tmp)?

Let's assume I have no means of modifying this binary to force it to use a different directory.

share|improve this question

3 Answers 3

up vote 10 down vote accepted

You can run the application in a chroot environment i.e. the / the application sees is not the real /. You create a complete new file system hierarchy and mount (--bind) everything you need into it. The relevant point is: You can mount the real ~/tmp to the /tmp in the chroot environment.

Instead of using chroot (which requires superuser privilege) you may do more or less the same with Linux containers (lxc). I am not familiar with lxc but as it's a normal user process to the host system you do not need to be the superuser for such configurations within the container.

share|improve this answer
Great answer, I hadn't heard of either chroot or lxc being capable of doing this. It's also really nice to know there's a way of accomplishing it without being a superuser. –  Nobilis Jun 4 at 14:57
@Nobilis 98 upvotes missing for this to be recognized as a great answer... –  Hauke Laging Jun 4 at 15:00
Beware, however, that chroot requires additional setup (you are replacing the whole of /, not just /tmp, so any access to /etc, /var, etc, will also be inside the "jail") and creates security concerns of its own (the "jailed" program may be able to manipulate parts of the file system which would normally be off-limits if you aren't careful with the permissions when setting up your fake /). –  IMSoP Jun 4 at 18:03
@IMSoP Would you mind explaining "may be able to manipulate parts of the file system which would normally be off-limits" in more detail? –  Hauke Laging Jun 4 at 18:23
@HaukeLaging If the new / is not restricted to be written only by root, the "jailed" user can create or replace files which seem to be in key system locations, such as /etc/passwd; this can then be used for privilege escalation which would not be possible outside the chroot. Many Linux FTP servers, which traditionally use chroot to hide the rest of the filesystem, now refuse to do so if the directory is writeable by a non-root user. –  IMSoP Jun 4 at 18:46

Most POSIX compliant software would honor the TMPDIR environment variable e.g.

env TMPDIR=~/mytmp  /path/to/application
share|improve this answer

If all else failed, you could always run your program with a wrapper script such as this one:

# rename the current /tmp directory
mv /tmp /tmp2

# make symbolic link to your directory at /tmp
ln -s /directory /tmp

# execute your program here

# remove symbolic link
rm -f /tmp

# replace original /tmp directory
mv /tmp2 /tmp

It isn't overly elegant, but may work for your needs if no other processes use /tmp during your run.

share|improve this answer
"If no other processes use /tmp during your run": That seems very unlikely, given the number of daemons running on an average Unix system. I think this approach is extremely dangerous. –  Nate Eldredge Jun 4 at 20:10
@NateEldredge - Don't be so negative. Wouldn't it work? I'd say it is 'dangerous' but not 'extremely dangerous.' Any open file descriptors will still point to the same open files and any newly opened files will go into the linked directory. It could be a non-issue, depending on why the OP needs this feature anyway. Not knowing that, I thought I'd give an option that hadn't been offered yet, with the appropriate caveat(s) mentioned. I looked in MY /tmp dir and found it's contents fairly static. Did you do any research to arrive at your opinion of 'seems very unlikely?' –  MrWonderful Jun 4 at 22:35
Of course it will achieve what the question asks for, but it may break other things unpredictably. Other programs that open a temp file and keep it open will be okay, as you say, but some programs need to close and reopen files, or create files that other programs need to be able to see. As one example, I submit KDE, which creates a socket directory in /tmp, which programs use to talk to the launcher. With your proposal, while the wrapper is running, the sockets will not be where they belong, and newly started KDE programs will not work. –  Nate Eldredge Jun 5 at 2:09
Certainly your script could be appropriate if there is literally nothing else running on the system, or every other program and daemon has been audited to check that it uses /tmp in a compatible manner. I think that's very unlikely for a normal use case, and I didn't think your caveats made that sufficiently clear, especially for a possibly inexperienced user. –  Nate Eldredge Jun 5 at 2:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.