Shutterstock/ Suncheli Project

In case you are wondering, RAM Drives and tmpfs instances are not the same. This article will explain the difference and show you how to create a RAM drive in Linux using the command line. Setup a fast RAM drive in minutes!

What Is a RAM Drive?

The RAM chips in your computer can be used to hold a virtual drive. This drive is then not stored on the hard disk but in RAM. Not only will the speed of this virtual disk be significantly faster the a normal disk (and especially when compared with an older type spinning disk, as the physical movement of parts inside a spinning disk cause additional delays), RAM chips do not wear as fast as disks, and again especially and all the more so with older type physical drives.

Sounds too good to be true? Well, there is indeed a caveat; if you accidentally reboot your computer, or if it crashes, your data is all gone. RAM (Random Access Memory), the memory chips in your computer, require constant power in order to retain their information. RAM storage is considered volatile.

In other words, RAM drives lend themselves to temporary applications or to specific optimizations. For example, when we use testing servers to test software, we set up a RAM drive to let the many concurrent tests go quicker. And, even if the server were to lose power, not much would be lost; we would simply start another test run.

Another application is pre-loading often accessed data into a RAM drive. For example, if you have a server that constantly accesses a given read-only database (read-write may be more complex if the data needs to be kept), which normally resides on disk, then you could copy the read-only database to RAM automatically (with some automated scripting on sever startup for example, or with a cron job), and then let the database server use that data.

In other words, one can summarize two primary use cases, one being caching (like our R/O database example), the other being “invaluable” data storage (like our testing example). You can then take one step further (a third use case if you will) and sync data back to disk at given intervals. For example, with the testing example, which also includes writes to the RAM storage, one could write the summary and/or test data back to disk (permanent storage) at the end of every test completion.

Another caveat with RAM drives is that they are limited to the size of memory in your system and likely less than that as you need other memory to run the operating system and other software.

Sizing a RAM drive to more than – let’s say an arbitrary 80-85% of system memory – may be asking for problems. Of course, if you have 256GB of RAM in your server, then even 90% allotted for a RAM drive would still leave more than 25GB for the operating system and applications. With only 4GB, a 90% allotment to RAM would leave 0.4GB (400MB), which is quite likely going to cause issues. It thus depends, to some extent, what the total memory of the machine is and how much will be needed for other software.

A RAM drive furthermore does not work the same as tmpfs allocation/instance.

RAM Drive vs tmpfs instance

A tmpfs can also be but does not have to be, stored in your computer’s RAM chips. The common /dev/shm tmpfs mapping automatically set up with the installation of most if not all Linux operating systems is handy but does not function the same as a RAM drive.

The difference between the two is that a RAM drive (the term rigid comes to mind) is 100% store in actual RAM chips, whereas tmpfs is stored into the Linux Kernel memory pool, which may include things like swap space, which is regularly located on disk. Whilst the kernel would likely optimize all access to the pool, it still gives the possibility of data being written either to physical RAM or physical DISK. And, if it goes to disk, it will be slower.

Creating a RAM Drive

Creating a RAM drive is relatively straightforward. You can create a little script called, with the following code:

if [ "$(mount | grep -o "/mnt/ram")" != "/mnt/ram" ]; then
  sudo mkdir -p /mnt/ram
  sudo mount -t ramfs -o size=1g ramfs /mnt/ram
  sudo chown -R $(whoami):$(whoami) /mnt/ram
mount | grep ram

And another script, with the following code:

sudo umount /mnt/ram

Let’s have a look at the first script. First, we indicate we want to Bash as our command interpreter with the Shebang symbol (#!). If you would like to learn more about Shebang, have a look at Bash Automation & Scripting Basics, our 3 part article on Bash automation and scripting.

After this, we check if we already have a mount under /mnt/ram (the directory we will be using to mount our ram drive in), by using a grep-only (grep -o) of /mnt/ram in the full ‘currently mounted’ list, as displayed by mount. If the same is not found, then we proceed with three sudo commands. All three require sudo, though for various reasons.

The first command requires sudo as it makes a directory possibly of root and at least in /mnt, which are privileged/protected directories. The next command, our actual RAM disk mount, and creation, requires sudo as mounting is a privileged operation. We set the size to 1GB by using size=1g. We also indicate we want a ramfs type drive (-t ramfs) coming from the ramfs device (as indicated by the second ramfs), and we finally list the mount point as /mnt/ram.

In the third sudo-enabled command, we change the owner of the /mnt/ram directory (now our RAM drive, our ramfs mount point) to the current user and the current user’s own group by using the whoami command twice. You may like to change this to the specific and/or the specific group that will be using the ramdrive or to a wider group if more users will be using the ramdrive.

After this, we finalize our conditional if .. fi command and do a final call to mount with a grep for ram to make sure the script reports back as to what was already mounted in terms of RAM, or what was mounted just now as the script executed. This is a handy/quick verification that the script was successful when executed.

Our secondary script,, unmounts the RAM-based drive with mount point /mnt/ram, i.e. the ramfs drive we just created. WARNING: executing this immediately drop/deletes all data stored in volatile memory and remounting the RAMFS drive will not bring this back; it will simply create a brand new but empty RAM drive. Please be forewarned!

Wrapping up

In this article, we discussed RAM/ramfs (the same thing) drives and tmpfs instances/allocations. We then created a 1GB ramfs drive under /mnt/ram using a small script to mount or RAM drive.

If you like to continue reading on Linux, have a look at our Screen Recording in Linux with SimpleScreenRecorder or Bits, Bytes and Binary or From 0 to F: Hexadecimal articles!


Profile Photo for Roel Van de Paar Roel Van de Paar
Roel has 25 years of experience in IT & business, 9 years of leading teams, and 5 years in hiring & building teams. He worked for companies like Oracle, Volvo, Sun, Percona, Siemens, Karat, and now MariaDB in various senior, principal, lead, and managerial roles.
Read Full Bio »