154

What is this folder: /run/user/1000 on my Fedora system and what does it do?

~ $ df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           1.2G   20K  1.2G   1% /run/user/1000

EDIT: 7 june 2019.

My two answers don't agree on what directory or where the files stored in this place were:

Patrick:

Prior to systemd, these applications typically stored their files in /tmp.

And again here:

/tmp was the only location specified by the FHS which is local, and writable by all users.

Braiam:

The purposes of this directory were once served by /var/run. In general, programs may continue to use /var/run to fulfill the requirements set out for /run for the purposes of backwards compatibility.

And again here:

Programs which have migrated to use /run should cease their usage of /var/run, except as noted in the section on /var/run.

So which one is it that is the father of /run/user/1000, why is there no mention in either answer of what the other says about the directory used before /run/user.

  • There's no mention in either answer of what the other says about the directory used before /run/user because only one actually discusses this. Braiam's answer discusses the directory used before /run rather than /run/user. – David Yockey Jun 08 '19 at 14:18
  • 1
    An answer to the question of "the father of /run/user/1000" is presented as a reasoned but unsubstantiated statement by Patrick. Braiam doesn't address this question. It's likely that the storage location of files used by running processes, i.e. temporary files, in the past was just assumed to be the standard /tmp directory. If so, it'd be tough to substantiate because few people provide written record of the conventional or obvious. To provide a substantiated answer would probably require some concerted digging into computer history. – David Yockey Jun 08 '19 at 14:22
  • Thank you for your input – somethingSomething Jun 08 '19 at 14:24
  • 1
    @somethingSomething Stumbled across this again after your update. To answer your request for clarification, no /var/run was never used for the same purpose as /run/user/$id. /var/run is writable by root only, and thus not usable by users. /run/user/$id is for normal users. /var/run is also not deprecated by the existence of /run/user/$id either. They serve different purposes. /var/run is for system wide services. /run however does deprecate /var/run but this is a separate matter entirely, and unrelated to /run/user/$id. – phemmer Jun 07 '20 at 03:32

2 Answers2

167

/run/user/$uid is created by pam_systemd and used for storing files used by running processes for that user. These might be things such as your keyring daemon, pulseaudio, etc.

Prior to systemd, these applications typically stored their files in /tmp. They couldn't use a location in /home/$user as home directories are often mounted over network filesystems, and these files should not be shared among hosts. /tmp was the only location specified by the FHS which is local, and writable by all users.

However storing all these files in /tmp is problematic as /tmp is writable by everyone, and while you can change the ownership & mode on the files being created, it's more difficult to work with.

So systemd came along and created /run/user/$uid. This directory is local to the system and only accessible by the target user. So applications looking to store their files locally no longer have to worry about access control.
It also keeps things nice and organized. When a user logs out, and no active sessions remain, pam_systemd will wipe the /run/user/$uid directory out. With various files scattered around /tmp, you couldn't do this.

phemmer
  • 71,831
  • 42
    Should mention that it is called $XDG_RUNTIME_DIR, documented at http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html – o11c Oct 19 '14 at 06:32
  • What if: I have started a "background" computation process with nohup, and it saves its intermediate results/data in a temp file. Can I count on it not being wiped while the process is running, or it will be wiped, and the process started with nohup will loose its data? – imz -- Ivan Zakharyaschev May 19 '15 at 09:39
  • It's unlikely to be wiped, but /run/user is a tmpfs filesystem in fedora, so it'll be limited to the amount of space allocated to it. See the df output above. – jsbillings Jul 04 '15 at 23:51
  • 1
    What if the pidfile is a service running under root. Should it's PID go under /var/run or /var/run/user/0 ? If since there is no active sessions will it be removed? – TSG Mar 25 '16 at 14:20
  • @TSG I think since there will be no active login session for root, there won't be a /var/run/user/0 folder either. Those files should go under /run instead, I believe (and not the now-deprecated /var/run). – Peter Hansen Jan 19 '20 at 16:22
  • Just to say, $uid doesn't generally store the userid, but according to https://askubuntu.com/a/468239/37574, $UID does (possibly depending on the shell). So it might be worth using that in the answer instead. – mwfearnley Jun 19 '23 at 12:12
  • $uid in the example isn't meant to be literal but a placeholder for whatever UID is in question. Though it can be literal if you do uid=123 (again, 123 is a placeholder). – phemmer Jun 19 '23 at 12:46
18

According to the latest draft of FHS (File Hierarchy Standard), /run:

This directory contains system information data describing the system since it was booted. Files under this directory must be cleared (removed or truncated as appropriate) at the beginning of the boot process.

The purposes of this directory were once served by /var/run. In general, programs may continue to use /var/run to fulfill the requirements set out for /run for the purposes of backwards compatibility. Programs which have migrated to use /run should cease their usage of /var/run, except as noted in the section on /var/run.

Programs may have a subdirectory of /run; this is encouraged for programs that use more than one run-time file. Users may also have a subdirectory of /run, although care must be taken to appropriately limit access rights to prevent unauthorized use of /run itself and other subdirectories.

In the case of the /run/user directory, is used by the different user services, like dconf, pulse, systemd, etc. that needs a place for their lock files and sockets. There are as many directories as different users UID's are logged in the system.

Braiam
  • 35,991