Linux – remount /tmp for the current shell

linuxmountpath

I have a command which is hard-coded to use /tmp as its working directory.

/tmp is rather small, and the command is certain to fill it up, which will cause problems for other system processes.

I can't modify the command, so I would like to remount /tmp somewhere else, only visible to the current user/shell, without affecting other users/processes.

Is this possible?

Update: Discovered the command uses the TMPDIR variable which I can override, but still interested to know if there's a Linux user-space command I can use.

Best Answer

The unshare command can do pretty much what you are asking for:

unshare -m "$SHELL"

After that you can change any mounts you like locally to that shell only.

I recommend that any mount commands you run inside the unshared shell use the -n flag, because otherwise it will change /etc/mtab which will be visible to the entire system. You probably want df and mount commands run outside that shell to still show the correct file system mounted on /tmp.