Home / Alt manpages / tmpfs(5)

  • tmpfs(5)
  • File format
  • linux

Create and Resize a Safe tmpfs Mount

A tmpfs mount lives in RAM and vanishes the moment you unmount it, which makes it brilliant for scratch space and dangerous if you forget that detail. This guide creates an 8 MiB tmpfs, checks its permissions and accounting, resizes it without losing a test file, then unmounts it cleanly. About ten minutes.

You need a Linux shell, mount, findmnt and df. Mounting normally requires root privileges through sudo.

Checkpoint

Stop after the verification step if you only need a short-lived scratch area. The unmount step deliberately destroys everything stored in the tmpfs.

1. Check the local behaviour

The installed reference is Linux man-pages 6.7, dated 31 October 2023. This host runs Linux 6.8.0-139-generic with util-linux 2.42.4; the commands below use the interfaces from that combination.

A tmpfs keeps files in virtual memory. It can use swap under memory pressure, it consumes resources only for the data actually stored, and it has a size limit. It is not persistent storage: unmounting it loses everything in it.

The default limit is a trap. With neither size nor nr_blocks supplied, the default is size=50% of physical RAM. Always set an explicit limit for a scratch mount.

$ mount --version
mount from util-linux 2.42.4 (libmount 2.42.4: btrfs, namespaces, idmapping, fd-based-mount, statmount, statx, fanotify, assert, debug)
$ findmnt --version
findmnt from util-linux 2.42.4

Version text differs by distribution. It does not change the safety rule: do not leave an unbounded or unexpectedly large tmpfs sitting around.

2. Prepare a private mount point

Choose a directory that is not already a mount point. The example uses /tmp/example-tmpfs; replace it with a dedicated directory owned by whichever service or user needs it.

$ sudo install -d -m 0755 /tmp/example-tmpfs
$ findmnt --noheadings --target /tmp/example-tmpfs

The second command should print nothing. Existing filesystem shown? Stop and pick another directory. Do not mount over a directory that already holds files you need: they become hidden until you unmount the new filesystem.

Creating the directory is the only privileged step so far. The file operations that follow can be unprivileged if the mode, owner and group you choose allow it.

3. Mount a bounded tmpfs

Mount an 8 MiB instance with a root directory mode of 0700. The first tmpfs is the filesystem source name, a label here rather than a block device:

$ sudo mount -t tmpfs -o size=8M,mode=0700 tmpfs /tmp/example-tmpfs
$ findmnt --noheadings --output FSTYPE,OPTIONS,TARGET /tmp/example-tmpfs
tmpfs rw,relatime,size=8192k,mode=700 /tmp/example-tmpfs

M means MiB here. The limit is rounded to whole pages, so the reported value may show in KiB instead. mode=0700 gives the mount root owner-only access; owner and group otherwise come from the mount defaults unless you pass uid= and gid=.

Checkpoint

Confirm FSTYPE is tmpfs, the target is the directory you chose, and the size is near 8 MiB. Mount failed? Read the error before touching options: a missing mount point, an existing mount, or insufficient privilege is far more likely than needing a bigger limit.

4. Write data and inspect the limit

Create a small file with ordinary commands. This never touches the underlying disk while the tmpfs is mounted:

$ printf '%s\n' 'temporary test data' > /tmp/example-tmpfs/check.txt
$ cat /tmp/example-tmpfs/check.txt
temporary test data
$ df -h /tmp/example-tmpfs
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           8.0M  4.0K  8.0M   1% /tmp/example-tmpfs

Your df output may round differently; check the filesystem row, not a number you remembered. The manpage also describes aggregate tmpfs use in Shmem in /proc/meminfo and the shared field from free, but those figures include other shared-memory users too, so df stays the useful per-mount check.

5. Resize it without unmounting

tmpfs supports a remount that changes its size without discarding existing contents. Bump the limit to 16 MiB:

$ sudo mount -o remount,size=16M /tmp/example-tmpfs
$ findmnt --noheadings --output FSTYPE,OPTIONS,TARGET /tmp/example-tmpfs
tmpfs rw,relatime,size=16384k,mode=700 /tmp/example-tmpfs
$ cat /tmp/example-tmpfs/check.txt
temporary test data

A resize cannot shrink the limit below current usage. If a reduction fails, check usage with du -sh /tmp/example-tmpfs and remove only files you have confirmed are disposable. The remount itself needs privilege, and its settings must stay compatible with the original mount: a mount created with noswap, for instance, must keep that choice on remount. The local manpage records noswap as available since Linux 6.4, with swap enabled by default otherwise.

6. Unmount and recover the directory

Warning

Unmounting discards every file in this tmpfs. Copy anything you need to persistent storage first. This command is privileged:

$ sudo umount /tmp/example-tmpfs
$ findmnt --noheadings --target /tmp/example-tmpfs
$ rmdir /tmp/example-tmpfs

After unmounting, findmnt should print nothing again, and the directory is now gone. If you mounted over a directory that already had files in it, they should reappear once you unmount.

Mount busy? Find the process holding a file or working directory there with sudo fuser -vm /tmp/example-tmpfs, get that process out safely, then retry sudo umount /tmp/example-tmpfs. Do not reach for a forced or lazy unmount as a routine fix: either can leave application state harder to reason about afterwards.

Common traps

  • Expecting persistence. tmpfs suits caches, scratch data and runtime files, not the only copy of a report or database.
  • Assuming the default is small. Without an explicit size, the limit is normally half of physical RAM.
  • Confusing limit and current use. size=16M permits up to that limit; it does not allocate 16 MiB immediately.
  • Using nr_blocks with a percentage. Percentage syntax belongs to size, not nr_blocks or nr_inodes.
  • Mounting over useful files. The old contents are hidden during the mount, not copied into the tmpfs.

Done means

  • Mount point clean: it held no important data and was not already mounted.
  • Size and mode confirmed: findmnt showed tmpfs with the intended values.
  • File survived: the test file was readable before and after the remount.
  • Limit explicit: the size limit was bounded deliberately for the workload.
  • Unmounted last: the tmpfs came down only once its contents were no longer needed.