Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The distribution is an Ubuntu server running the 2.6.35-30 Linux kernel.

I would like to have a directory that sits completely in memory. Is this possible without root privileges?

share|improve this question

3 Answers 3

up vote 16 down vote accepted

Linux provides a tmpfs device which any user can use, /dev/shm. It is not mounted to a specific directory by default, but you can still use it as one.

Simply create a directory in /dev/shm and then symlink it to wherever you want. You can give the created directory any permissions you choose, so that other users can't access it.

This is a RAM backed device, so what's there is in memory by default. You can create any directories you need inside /dev/shm

Naturally, files placed here will not survive a reboot, and if your machine starts swapping, /dev/shm won't help you.

The Solaris parallel to /dev/shm is /tmp which is a "swap" type partition, and also memory based. As with /dev/shm, arbitrary users may create files in /tmp on Solaris.

OpenBSD has the capability to use a memory based mount as well, but does not have one available by default. The mount_mfs command is availabe to the super user.

I'm not sure about other *BSDs.

share|improve this answer
1  
A symlink could solve the where problem. –  enzotib Dec 8 '11 at 22:37
    
D'oh... forgot about those pesky little symlinks. –  gabe. Dec 8 '11 at 22:39
    
@enzotib fixed it! –  gabe. Dec 8 '11 at 22:41
    
There doesn't seem to be a /dev/tmpfs on the system (neither does my own system which has a 3.0.0 kernel). Are you sure that's not something created by your distribution? –  bitmask Dec 9 '11 at 0:46
    
Ah, on my system, /dev/tmpfs is called /dev/shm as noted by Gilles. –  bitmask Dec 9 '11 at 0:55

Your system may have one already available; recent Linux systems based on Glibc always have a tmpfs mounted on /dev/shm.

If your system doesn't have one or it's too small, then a filesystem not mounted by root pretty much means FUSE. On Ubuntu, you need to be in the fuse group to use FUSE. Looking through available FUSE filesystems, I see only Ramfuse, which unfortunately is abandoned upstream.

share|improve this answer

In general, no, filesystems can only be mounted by root. If you allow a user to place filesystems arbitrarily, that's essentially giving them root. (Easy way: mount one over /etc, put your own passwd and shadow there, su with the new root password you just created, unmount)

If you want a tmpfs at a particular location, you could add it to /etc/fstab, with the flags of noauto,user and then a user would be able to mount it (but its not particularly clear why you wouldn't just auto-mount it)

If users need arbitrary tmpfs's, then you have a couple options:

  • use subdirectories of one tmpfs. Probably the best approach. You really probably don't need more than one tmpfs.
  • create a script that will create a directory and mount a tmpfs over it, and then print out where it put it. Use sudo to allow users to run it. Make sure that you do not allow the user to pick arbitrary paths.
share|improve this answer

Your Answer

 
discard

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.