Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I noticed that in ubuntu, mktemp allows a user to make temporary files in a directory even if write permissions are not allowed for that user. I thus expected the permissions on mktemp to read something like this, giving the user temporary root privileges while running it:

-rwsr-xr-x 1 root root   35392 Nov 19  2012 mktemp

but instead they read something like this:

-rwxr-xr-x 1 root root   35392 Nov 19  2012 mktemp

Where I thought there was an 's', there is an 'x', meaning mktemp does not run with root privileges. How is mktemp able to create temporary files in a directory without having the permission to do so?

share|improve this question
up vote 7 down vote accepted

mktemp, by default, creates a temporary directory under /tmp, which by default is writable by anyone; if you pass the --tmpdir option to mktemp (or set the TMPDIR environment variable), it will attempt to create a temporary directory in the directory given by the option's argument, and that will fail unless you have write permission in the given directory. For example:

[me@box] $ mktemp
[me@box] $ mktemp --tmpdir=/root
mktemp: failed to create file via template `/tmp.XXXXXXXXXXX': Permission denied

If you're doing, e.g., mktemp --tmpdir=/root as a non-root user and it doesn't fail, then something very weird is going on. Are you sure that's what you're seeing?

share|improve this answer
I was just using default flags. I thought it put files into ./tmp , not /tmp because I apparently wasn't paying attention. Thanks! – MYV May 24 '13 at 20:10
Glad to be of help! – Aaron Miller May 24 '13 at 20:16
A little nit-picking: IMHO /tmp has usually the sticky bit (t) set: drwxrwxrwt root root 0 May 24 23:52 /tmp. I. e. everybody can create files/dirs in it, but cannot delete/rename files owned by someone else -- even it is formally world writable as indicated by ls. For more info see e.g. – mpy May 24 '13 at 21:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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