Create and Resize a Safe tmpfs Mount
You will create a temporary filesystem with an 8 MiB limit, check its permissions and accounting, resize it without losing a test file, then unmount it cleanly. Allow about ten minutes. You need a Linux shell, mount, findmnt and df; mounting normally requires root privileges through sudo.
The route
Jump straight to the step you need, or tick off Done means at the end.
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 is running Linux 6.8.0-139-generic with util-linux 2.42.4. The commands below use the interfaces provided by that combination.
A tmpfs keeps files in virtual memory. It can use swap when memory pressure requires it, consumes resources for the data actually stored, and has a size limit. It is not persistent storage: unmounting it loses its contents. The default limit is size=50% of physical RAM when neither size nor nr_blocks is supplied, so 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
The version text can differ on another distribution. That does not change the important safety rule: do not leave an unbounded or unexpectedly large tmpfs in place.
2. Prepare a private mount point
Choose a directory that is not already being used as a mount point. The example uses /tmp/dixon-tmpfs; replace it with a dedicated directory owned by the service or user that needs it.
$ sudo install -d -m 0755 /tmp/dixon-tmpfs
$ findmnt --noheadings --target /tmp/dixon-tmpfs
The second command should produce no output. If it shows an existing filesystem, stop and choose another directory. Do not mount over a directory containing files you need: those files become hidden until the new filesystem is unmounted.
The directory creation is the first privileged action. The later file operations can be unprivileged if the selected mode, owner and group permit them.
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; it is a label here, not a block device.
$ sudo mount -t tmpfs -o size=8M,mode=0700 tmpfs /tmp/dixon-tmpfs
$ findmnt --noheadings --output FSTYPE,OPTIONS,TARGET /tmp/dixon-tmpfs
tmpfs rw,relatime,size=8192k,mode=700 /tmp/dixon-tmpfs
The M suffix means MiB for this option. The limit is rounded to whole pages, so the displayed value may be expressed in KiB. mode=0700 gives the mount root owner-only access; the initial owner and group are otherwise set from the mount defaults unless you pass uid= and gid=.
Checkpoint
Confirm that FSTYPE is tmpfs, the target is the directory you chose, and the reported size is near 8 MiB. If the mount fails, read the error before changing options. A missing mount point, an existing mount, or insufficient privilege is more likely than a need for a larger limit.
4. Write data and inspect the limit
Use ordinary commands to create a small file. This does not write to the underlying disk location while the tmpfs is mounted.
$ printf '%s\n' 'temporary test data' > /tmp/dixon-tmpfs/check.txt
$ cat /tmp/dixon-tmpfs/check.txt
temporary test data
$ df -h /tmp/dixon-tmpfs
Filesystem Size Used Avail Use% Mounted on
tmpfs 8.0M 4.0K 8.0M 1% /tmp/dixon-tmpfs
Your df output may use different rounding. Check the filesystem row, not a remembered byte count. The manpage also describes aggregate tmpfs use in Shmem in /proc/meminfo and the shared field from free; those figures include other shared-memory users, so df is the useful per-mount check.
5. Resize it without unmounting
tmpfs supports a remount that changes its size without discarding existing contents. Increase the limit to 16 MiB:
$ sudo mount -o remount,size=16M /tmp/dixon-tmpfs
$ findmnt --noheadings --output FSTYPE,OPTIONS,TARGET /tmp/dixon-tmpfs
tmpfs rw,relatime,size=16384k,mode=700 /tmp/dixon-tmpfs
$ cat /tmp/dixon-tmpfs/check.txt
temporary test data
A resize cannot make the limit smaller than the current usage. If a reduction fails, inspect usage with du -sh /tmp/dixon-tmpfs and remove only files you have confirmed are disposable. The remount itself needs privilege, and its settings must remain compatible with the original mount. For example, a mount created with noswap must keep that choice on remount; the local manpage records that option as available since Linux 6.4 and says swap is enabled by default otherwise.
6. Unmount and recover the directory
Warning
Unmounting discards every file in this tmpfs. Copy anything needed to persistent storage before this step. The command is privileged:
$ sudo umount /tmp/dixon-tmpfs
$ findmnt --noheadings --target /tmp/dixon-tmpfs
$ rmdir /tmp/dixon-tmpfs
After the unmount, findmnt should again print nothing. The directory is now gone. If you mounted over a directory that already contained files, they should reappear after unmounting.
If the mount is busy, find the process holding a file or working directory with sudo fuser -vm /tmp/dixon-tmpfs, leave that process safely, then retry sudo umount /tmp/dixon-tmpfs. Do not reach for a forced or lazy unmount in a routine cleanup: those choices can leave application state harder to reason about.
Common traps
- Expecting persistence: tmpfs is suitable for 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=16Mpermits up to that limit; it does not allocate 16 MiB immediately. - Using
nr_blockswith a percentage: percentage syntax belongs tosize, notnr_blocksornr_inodes. - Mounting over useful files: the old directory contents are hidden during the mount, not copied into tmpfs.
Done means
- The mount point was empty of important data and was not already mounted.
findmntshowedtmpfswith the intended size and mode.- The test file was readable before and after the remount.
- The size limit was explicit and bounded for the workload.
- The tmpfs was unmounted only after its contents were no longer needed.