86

After seeing the comment by Anonymous on the question How is the /tmp directory cleaned up?, I found that it would be a great idea to implement on my system, since I have 16GB of RAM and I never used all of it.

My temporary files never get written to the disk. They get written to a RAM disk. I did put tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0 in /etc/fstab.

My question is:

Can I set a maximum value for RAM Usage for /tmp? And in that case, what would happen if the maximum amount got exceeded, would it write into the hard-disk drive?

I have read a solution which states:

mkdir -p /tmp/ram
sudo mount -t tmpfs -o size=512M tmpfs /tmp/ram/

But in my understanding, this won't be a permanent solution. If I need it to be permanent, it has to be added to the /etc/fstab configuration file.

If this is the correct solution, how can I transform that mount command into a line in /etc/fstab?

  • this is also a good idea if you have and SSD as you will not 'consume' it with not useful writings ... – Postadelmaga Apr 2 '14 at 10:47
  • Ha ha exactly the same question brought me here :D – Teddy Markov Apr 30 '17 at 10:53
77

You are absolutely right. The according fstab entry would look like this:

tmpfs /tmp tmpfs defaults,noatime,nosuid,nodev,noexec,mode=1777,size=512M 0 0

Please note:

As tmpfs gets filled up, it will behave as any physical hard drive by giving an "not enough space" error. While rebooting (and thus emptying the cache) will fix this, you may run into trouble when a single operation consumes more space to begin with than there's space on tmpfs. In this case your computer will start to swap from ram to disk, which will make your system crawl to a halt, given you've got a swap partition to begin with, of course.

Considering this, a size of 512MB might be far too less nowadays, since much more ram is in existence in modern machines and it has become much cheaper. Since you've already got 16GB of ram, using the default value of half your ram for tmpfs should more than suffice for almost all scenarios. To use the default value, simply leave out the size=512M entry in your /etc/fstab file.

Another note:

You can quite as easily mount other system folders into ramdisk as well, such as

/var/cache

/var/games

/var/log/apt (use only defaults,noatime without mode= or nosuid)

But beware: the same rules apply as above, running out of space might cause major trouble. E.g. imagine running out of space for /var/log/apt will render you unable to install any programs! Furthermore, loading /var/log folders into ramdisk will delete all your log files upon reboot, so you won't be able to debug your system if anything unexpected happens. So use these settings at your own risk!

Editorial note: I removed the /run in tmpfs mount option since this folder and its subfolders are already mounted in tmpfs by default.

  • 8
    If i'm not wrong, /var/tmp/ is for keeping files after reboot. Thats the main difference between this and /tmp/ , so YOU should not move /var/tmp to ram. – user285767 May 27 '14 at 13:20
  • 3
    If Ubuntu runs out of 4GB tmpfs, will it use my 20GB SWAP partition? – loostro Jun 24 '14 at 16:30
  • 3
    Yes, I'll need to add this, too. As soon as tmpfs exeeeds its limits, it'll extend to swap partition (give there is one). – FuzzyQ Jun 24 '14 at 19:10
  • 4
    Does allocating half your RAM to this mean that half your RAM is reserved for the RAMDISK, or is it only a cap to what the RAMDISK may consume, and whatever is not in use is free RAM that gets assigned to whatever program needs RAM?? – matt Jan 19 '15 at 15:01
  • 5
    @matt It's only a cap. – FuzzyQ Jan 19 '15 at 16:04
28
+100

On systems using systemd, you have the option of using a systemd unit file instead of fstab to accomplish the goal of using tmpfs to mount tmp. On my Ubuntu 16.04 system, I ran:

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

The file /usr/share/systemd/tmp.mount looks like:

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
[Unit]
Description=Temporary Directory
Documentation=man:hier(7)
Documentation=http://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
ConditionPathIsSymbolicLink=!/tmp
DefaultDependencies=no
Conflicts=umount.target
Before=local-fs.target umount.target
After=swap.target

[Mount]
What=tmpfs
Where=/tmp
Type=tmpfs
Options=mode=1777,strictatime

[Install]
WantedBy=local-fs.target

Using FuzzyQ's fstab approach, systemd translates your fstab entries into mount units dynamically. I don't think either approach is better.

  • 1
    You will need systemctl enable tmp.mount after cp else systemctl will fail with message "Failed to start tmp.mount: Unit tmp.mount not found." – vimdude Oct 8 '16 at 18:17
  • 2
    @Rick, it's even better to use systemctl edit tmp.mount that would be distro-neutral. That puts your changes into override.conf so that you don't alter default tmp.mount and you could delete override.conf easily to go back to default configuration. – Bulat M. Feb 5 '17 at 5:13

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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