CAPTCHA
Image CAPTCHA
Enter the characters shown in the image.
This question is for testing whether or not you are human.
  • Create new account
  • Reset your password

User account menu

Home
The Hyperlogos
Read Everything

Main navigation

  • Home
  • My Resumé
  • blog
  • Howtos
  • Pages
  • Contact
  • Search

Setting temp dir location on Linux

Breadcrumb

  • Home
  • User Blogs
  • User Blog
  • Setting temp dir location on Linux
By drink | Sun May 21, 2023

I use a 1GB tmpfs on my Linux system, which causes problems with some installs which write their temporary files to /tmp. Here's a way to somewhat gracefully set TMPDIR for every user logging in:

$ cat /etc/profile.d/home.tmp.sh 
#!/bin/sh
if [ -e ${HOME}/.tmp ]
then
        if [ ! -d ${HOME}/.tmp ]
        then
                rm -f ${HOME}/.tmp
        fi
        if [ `w -hus | cut -f1 -d' ' | grep ${USER} | wc -l` == 1 ]
        then
                rm -rf ${HOME}/.tmp/*
        fi
else
        mkdir ${HOME}/.tmp
fi
export TMPDIR=${HOME}/.tmp

If .tmp exists, and it's not a directory, remove it. (You could instead rename it, but that comes with its own set of potential problems.) If it is a directory, and this is the first login, then remove its contents. We count the number of times the user appears in the utmp file by parsing it with the w command, which has been instructed to provide only usernames. Even this approach is slightly naive; what if multiple users share a home directory? Anyway, if it does not exist, create it. Finally, set the TMPDIR variable to the directory.

One possible improvement could be to check if uid is greater than 999, meaning that we would do this only for actual users who might really log in; this would avoid problems if we log in as some daemon user to do some troubleshooting or maintenance. Another would be to allow the user to specify the location of the temporary directory, perhaps by reading a file in .local.

There is a pam module pam_tmpdir available in the libpam-tmpdir package, but it doesn't appear to have any configuration, so it is worthless for this use case; it only creates subdirectories in /tmp for users.

linux
howto
  • Log in or register to post comments

Footer menu

  • Contact
Powered by Drupal

Copyright © 2025 Martin Espinoza - All rights reserved