How to configure Fedora / RedHat / CentOS to delete files in /tmp on boot

How can you configure Fedora / RedHat / CentOS systems to delete files in /tmp on boot like Debian and Ubuntu?


On recent versions (CentOS 7, Fedora 23), add a .conf file to "/usr/lib/tmpfiles.d", the name is irrelevant, name it boot.conf for example, but it has to be a different file than tmp.conf, there cannot be two entries for the same directory in a single file. Add the the following line:

R! /tmp 1777 root root ~0

By default /tmp is a tmpfs, and doesn't exist on boot anyway. To get into this situation, your server must already be non-standard in some way: Someone explicitly configured your system to not have /tmp emptied on boot.

So it is best to fix the problem, by undoing whatever changes were made to cause /tmp to not mount as a tmpfs on boot:

  1. Make sure the mount has not been disabled:

    systemctl unmask tmp.mount
    

    If this command fails, fix the problem before continuing.

  2. Go to single user mode. You need to be on the console for this.

    systemctl start rescue.target
    
  3. Empty the /tmp directory on the hard drive, to clear out the used space.

    rm -rf /tmp
    mkdir -m 1777 /tmp
    
  4. On CentOS 7, you need to explicitly enable the mount. On Fedora, you can skip this.

    systemctl enable tmp.mount
    
  5. Restart the system.

    systemctl start default.target