32

I am trying to move /tmp to RAM and obey good practice:

  • /var/tmp should NOT be placed in tmpfs, as it must survive reboots

  • not mount /tmp as a stand-alone tmpfs, but rely on /dev/shm or /run/shm

However, things have changed between 2.6 and 3.2 kernels :

Changes to the configuration and defaults of tmpfs filesystems

On 3.2 kernel, use RAMTMP=yes in /etc/default/tmpfs.

My question is: how can I achieve this goal with older kernels ?

My choice is nor to modify /etc/fstab neither this :

mkdir /dev/shm/tmp
chmod 1777 /dev/shm/tmp
mount --bind /dev/shm/tmp /tmp

Is there something like RAMTMP for 2.6 kernels ?

Massimo
  • 965
  • 1
  • 10
  • 23
  • 2
    [`tmpfs` considered harmful](http://rwmj.wordpress.com/2012/09/12/tmpfs-considered-harmful/). Food for thought. – sr_ Nov 15 '12 at 08:05
  • 12
    @sr_ Sorry, I strongly disagree with the linked article. `/tmp` can and should be used for temporary files, that's the whole point of it. `/tmp` is not supposed to survive a reboot (use `/var/tmp` for that) and who composes an email half the size of RAM+swap (without saving it)? – Marco Nov 15 '12 at 10:29
  • @Marco sure, I was just throwing in something I thought related. – sr_ Nov 15 '12 at 11:14
  • More context: https://fedoraproject.org/wiki/Features/tmp-on-tmpfs – Nemo Jul 19 '17 at 21:13

3 Answers3

31

You don't have to do all that, you can just mount /tmp as tmpfs by using a line like the following in /etc/fstab:

tmpfs /tmp tmpfs mode=1777,nosuid,nodev 0 0

You can also do it live (but bear in mind stuff that is currently in /tmp on your current filesystem will not be able to be accessed except through the inode and currently open file descriptors, so you should really do this by modifying /etc/fstab and rebooting):

mount -o mode=1777,nosuid,nodev -t tmpfs tmpfs /tmp

Note that a tmpfs can swap. If you want a truly RAM-only filesystem, use ramfs.

Chris Down
  • 122,090
  • 24
  • 265
  • 262
  • Chris, my point is "best practices". The developers put native support to mount /tmp over .../shm; I read it is better to re-use the same tmpfs. – Massimo Nov 15 '12 at 00:27
  • 3
    @Massimo My opinion is that the best practise is *not* to bind mount. It adds unnecessary complexity. – Chris Down Nov 15 '12 at 00:28
  • 5
    @Massimo Where are you finding this 'best practices'? The only reason I can see against mounting `/tmp` as tmpfs is because you now have 2 potential tmpfs filesystems that can fill up and use more memory (`/tmp` and `/dev/shm`). But if you're really concerned about filling up 2 tmpfs mounts, you can decrease the maximum size of the tmpfs mount. – phemmer Nov 15 '12 at 01:52
  • @ChrisDown what is bad about bind mount? Also having a single tmpfs might make things simpler right? – CMCDragonkai Jun 05 '14 at 06:42
  • 2
    @CMCDragonkai Bind mounts are anything but simple internally, they come with quite a few unintuitive caveats that you probably don't intend (a particularly publicised one being the drama surrounding requiring remount for read-only mounts). Just use two `tmpfs` mounts instead. – Chris Down Jun 05 '14 at 07:00
  • @ChrisDown if there are 2 tmpfs will they compete with each other for ram? I read that tmpfs is normally 50%. So if there are 2 tmpfs, does that mean there are 2 tmpfs each trying to use 50%? In that case, should both be 25%? – CMCDragonkai Jun 05 '14 at 07:56
  • Do the options `nosuid,nodev` really necessary? Do they exist by default? Perhaps some process would need it... – Dor Mar 18 '17 at 12:19
30

/etc/default/tmpfs is for sysvinit, for systemd (Debian default since jessie) you only need to do:

systemctl enable tmp.mount

and on Debian Stretch or later:

cp /usr/share/systemd/tmp.mount /etc/systemd/system/
systemctl enable tmp.mount

See changelog

Further details on RAMDISK/tmpfs can be found on the SSD Optimization Debian Wiki page.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
Lluís
  • 805
  • 1
  • 8
  • 13
6

I know this is rather old but in Debian-type distributions setting Set RAMTMP, RAMRUN and RAMLOCK in /etc/default/tmpfs (/etc/default/rcS or before wheezy) does the same job.

luison
  • 241
  • 2
  • 6
  • On **Ubuntu 16.04.6 LTS**: `cat /etc/default/tmpfs` results in `cat: /etc/default/tmpfs: No such file or directory`. Do you have a link for setting this up? The answer is almost short enough (and vague enough?) to be a comment. – WinEunuuchs2Unix Oct 10 '20 at 01:10