maki57
maki57

Reputation: 490

Is it safe to temporarily rename /tmp and then create a tmp symlink to a different location?

The situation is that this application needs more space in /tmp. Currently my tmp folder is in root's partition. Is it safe to temporarily create a tmp symlink to a different partition just to take advantage of the larger space?

Upvotes: 10

Views: 8407

Answers (4)

BHAVIK VACHHANI
BHAVIK VACHHANI

Reputation: 1

With assumption that nothing critical running on machine.

First create backup : sudo mkdir /tmp_bak && sudo rsync -avz /tmp/ /tmp_bak

We can follow below steps to created symbolic link by following the steps below

  1. mkdir $largedrive$/tmp
  2. cd /
  3. sudo rsync -avz /tmp/ $largedrive$/tmp && sudo rm -rf /tmp/ && sudo ln -s $largedrive$/tmp

where $largedrive$ is folder path where more space is available.

Upvotes: 0

kestasx
kestasx

Reputation: 1091

It depends...

Your better option may be seting TMPDIR environment variable to point to this location before starting Your application. This variable may be taken into account by Your application (but You need to test). Also application itself may have some settings or some other variable to set temporary location (check manual).

As for making symlink, runing applications which have files open in /tmp should not sense this change (i-node number would not change; even if You delete /tmp, open files would be deallocated after they are closed by all processes who currently have them open).

It may be a problem if another application expects to find something in /tmp (will be trying to open /tmp/.X11-unix for example). Such application would get an error. You can try to overcome this by making symlinks from new tmp to files in original tmp (symlinks must be correct after /tmp is renamed) before creating symlink. It may not work well for security concious or buggy applications.

Yet some chance to brake remains (it is not attomic operation to rename and symlink, so some application still may access /tmp when it is removed, but symlink is not yet created).

So it depends on what You have running on this machine.

If You can reboot the machine and have access to it's console (physical access, LOM, virtual machine condole, or similar) You can take OS to "single user" mode (telinit 1), make symlink and reboot. Or You can edit /etc/fstab to do mount --bind.

If You have Redhat/CentOS or derivative distribution there may be issues if SElinux is enabled.

Upvotes: 5

Super-intelligent Shade
Super-intelligent Shade

Reputation: 6449

Instead of renaming and/or symlinking, you can:

mount --bind /path/to/dir/with/plenty/of/space /tmp

And umount /tmp when you are done.

If you are on a mission critical server, you can check if any program is currently using /tmp with lsof /tmp before doing the above.

NB: Run all commands as root.

Upvotes: 16

eckes
eckes

Reputation: 10423

If it is a busy or mission critical server I would not do it, as there might be an important program trying to create a file while /tmp is missing. Or it might want to rename a file. But on a moderately used server, especially when you can pause the application you can try it.

It may have some problems with open sockets/fifos in the directory. It depends a bit on the Linux distribution how much is still using /tmp. Things like X11, screen, kde/gnome are candidates. So you better check with lsof first.

If /tmp is a mountpoint you might not rename it.

The most secure way to do this is booting in single user mode or from an external boot media to do the change. Then it is quite safe (as long as you do not use SELinux).

Upvotes: 1

Related Questions