- How to Easily Create RAM Disk on Debian, Ubuntu, Linux Mint, CentOS
- What is RAM Disk?
- Why use RAM disk?
- How to Create a RAM Disk in Any Linux Distro
- Test RAM Disk Speed
- Auto-mount on System Boot
- Using RAM Disk to Reduce SSD Wear Out
- How to Run VirtualBox VM on RAM Disk
- Wrapping Up
- tmpfs
- Usage
- Examples
- Disable automatic mount
- Troubleshooting
- Opening symlinks in tmpfs as root fails
- See also
How to Easily Create RAM Disk on Debian, Ubuntu, Linux Mint, CentOS
This tutorial will show you how to quickly create a RAM disk in any Linux distro (Debian, Ubuntu, Linux, Fedora, Arch Linux, CentOS, etc). Compared to commercial Windows RAM disk software that costs money, Linux can utilize this cool feature 100% free of charge.
What is RAM Disk?
RAM disk is also known as RAM drive. It’s a portion of your RAM that are formated with a file system. You can mount it to a directory on your Linux system and use it as a disk partition.
Why use RAM disk?
RAM is ultra-fast compared to even the fastest solid state drive (SSD). As you may know, the main performance bottleneck in today’s computer is the speed of hard drive, so moving programs and files to the RAM disk yields super fast computing experience.
- RAM is volatile which means all data in RAM disk will be lost when the computer shutdowns or reboots. However, this can be a pro in some situations, if you use it wisely.
- RAM is expensive so it has limited capacity. You need to make sure not allocate too much space for RAM disk, or the operating system would run out of RAM.
You can do a lot of interesting things with RAM disk.
- RAM disk is best suited for temporary data or caching directories, such as Nginx FastCGI cache, and Debian package downloads ( /var/cache/apt/archive/ ). If you use a SSD and there will be a lot of writes to a particular directory, you can mount that directory as a RAM disk to reduce wear out of SSD.
- I also use RAM disk to temporarily store screenshots when writing articles on this blog, so when my computer shut down, those screenshots will automatically be deleted on my computer.
- You might not believe it, but I use RAM disk to run virtual machines inside VirtualBox. My SSD is about 250G. I can’t run many VMs directly on the SSD and I’m not happy about the speed of my 2TB mechanical hard drive (HDD). I can move the VM from HDD to RAM disk before starting the VM, so the VM can run much faster. After shutting down the VM, I move the VM files back to HDD, which takes less than 1 minute. This of course requires your computer to have a large capacity RAM.
How to Create a RAM Disk in Any Linux Distro
First make a directory which can be anywhere in the file system such as
If you want to let every user on your Linux system use the RAM disk, then change its permission to 777.
sudo chmod 777 /tmp/ramdisk
Next, check how much free RAM are left on your system with htop command line utility because we don’t want to use too much RAM.
Then all left to do is to specify the file system type, RAM disk size, device name and mount it to the above directory. You can see from the screenshot above that I have plenty of free RAM, so I can easily allocate 1GB for my RAM disk. This can be done with the following one-liner. It will be using tmpfs file system and its size is set to 1024MB. myramdisk is the device name I gave to it.
sudo mount -t tmpfs -o size=1024m myramdisk /tmp/ramdisk
To allocate 10G for the RAM disk, run this instead.
sudo mount -t tmpfs -o size=10G myramdisk /tmp/ramdisk
If we issue the following command
We can see it’s successfully mounted.
Now if I copy my VirtualBox machines file (5.8G) into the RAM disk, my RAM usage suddenly goes up to 9.22G.
Everything in that directory will be lost and RAM usage goes down to original.
This is how you can test if your RAM disk is working.
Test RAM Disk Speed
To test write speed of RAM disk, you can use dd utility.
sudo dd if=/dev/zero of=/tmp/ramdisk/zero bs=4k count=100000
Which gave me 2.8GB/s write speed.
sudo dd if=/tmp/ramdisk/zero of=/dev/null bs=4k count=100000
Which gave me 3.1 GB/s read speed.
I also did a speed test on my SSD. The write speed is 534MB/s and read speed 1.6GB/s.
Auto-mount on System Boot
myramdisk /tmp/ramdisk tmpfs defaults,size=1G,x-gvfs-show 0 0
x-gvfs-show will let you see your RAM disk in file manager. Save and close the file. Your Linux system will automatically mount the RAM disk when your computer boots up.
To mount it immediately without reboot, run the following command.
Using RAM Disk to Reduce SSD Wear Out
To increase the lifespan of your SSD, you should avoid write and delete operations on the SSD as much as possible. The Linux operating system has two activities that make up the most write and delete operations.
If you use a Debian-based Linux distribution, the temporary download directory for software packages is /var/cache/apt/archives/ . Once the package is installed, APT package manager automatically deletes the .deb files in this directory. You can add the following line in /etc/fstab file to mount this directory in RAM.
package_archive /var/cache/apt/archives tmpfs defaults,size=6G 0 0
Fedora stores temporary package downloads under /var/cache/dnf/ directory, so you can add the following line in /etc/fstab file to mount this directory in RAM.
package_archive /var/cache/dnf tmpfs defaults,size=6G 0 0
Some folks might wonder if mounting this directory will prevent Fedora from upgrading to a new release, because Fedora requires reboot to install system upgrade packages. The answer is No. It won’t prevent system upgrades, as Fedora stores system upgrade pacakges under a different directory /var/lib/dnf/system-upgrade/ .
The logging directory on Linux is usually /var/log/ . It’s not safe to mount the entire /var/log/ directory in RAM. Change to this directory.
You will find that the /var/log/journal/ sub-directory uses the most disk space. It’s used for logging systemd services and safe to mount in RAM, so you can add the following in in the /etc/fstab file.
systemd_journal /var/log/journal tmpfs defaults,size=6G 0 0
If you run Nginx web server, you might want to mount the /var/log/nginx/ directory into RAM. On one of my servers, this directory is 2.2G in size.
nginx_logs /var/log/nginx/ tmpfs defaults,size=6G 0 0
After saving the /etc/fstab file, run the following command to mount all file systems.
How to Run VirtualBox VM on RAM Disk
Note that this requires a large-capacity RAM.
When you create a brand new virtual machine, you should set the machine folder to the RAM disk directory ( /tmp/ramdisk/ ). If you can’t find the Machine folder option, then you need to install the latest version of Virtualbox on your system.
If you have an existing VM, then select the VM in the main VirtualBox Manager window and go to the menu bar and select Machine -> Move, or right-click the VM and select Move from the context menu. You will be prompted to choose a new folder for the virtual machine. Select /tmp/ramdisk/ as the new folder.
Remember to move your VM back to the original folder before shutting down your computer, or your VM will be deleted.
Wrapping Up
And that’s the basics of creating RAM disk in Linux. If you found this post useful, then subscribe to our free newsletter or follow us Twitter or like our Facebook page. Thanks for visiting!
tmpfs
tmpfs is a temporary filesystem that resides in memory and/or swap partition(s). Mounting directories as tmpfs can be an effective way of speeding up accesses to their files, or to ensure that their contents are automatically cleared upon reboot.
Usage
Some directories where tmpfs(5) is commonly used are /tmp, /var/lock and /var/run. Do not use it on /var/tmp, because that directory is meant for temporary files that are preserved across reboots.
Arch uses a tmpfs /run directory, with /var/run and /var/lock simply existing as symlinks for compatibility. It is also used for /tmp by the default systemd setup and does not require an entry in fstab unless a specific configuration is needed.
glibc 2.2 and above expects tmpfs to be mounted at /dev/shm for POSIX shared memory. Mounting tmpfs at /dev/shm is handled automatically by systemd and manual configuration in fstab is not necessary.
Generally, tasks and programs that run frequent read/write operations can benefit from using a tmpfs directory. Some applications can even receive a substantial gain by offloading some (or all) of their data onto the shared memory. For example, relocating the Firefox profile into RAM shows a significant improvement in performance.
Examples
Note: The actual memory/swap consumption depends on how much is used, as tmpfs partitions do not consume any memory until it is actually needed.
By default, a tmpfs partition has its maximum size set to half of the available RAM, however it is possible to overrule this value. To explicitly set a maximum size, in this example to override the default /tmp mount, use the size mount option:
tmpfs /tmp tmpfs rw,nodev,nosuid,size=2G 0 0
To specify a more secure mounting, specify the following mount option:
tmpfs /www/cache tmpfs rw,size=1G,nr_inodes=5k,noexec,nodev,nosuid,uid=user,gid=group,mode=1700 0 0
See the tmpfs(5) man page and Security#File systems for more information.
Reboot for the changes to take effect. Note that although it may be tempting to simply run mount -a to make the changes effective immediately, this will make any files currently residing in these directories inaccessible (this is especially problematic for running programs with lockfiles, for example). However, if all of them are empty, it should be safe to run mount -a instead of rebooting (or mount them individually).
After applying changes, verify that they took effect by looking at /proc/mounts and using findmnt :
TARGET SOURCE FSTYPE OPTIONS /tmp tmpfs tmpfs rw,nosuid,nodev,relatime
The tmpfs can also be temporarily resized without the need to reboot, for example when a large compile job needs to run soon. In this case, run:
# mount -o remount,size=4G /tmp
Disable automatic mount
Under systemd, /tmp is automatically mounted as a tmpfs, if it is not already a dedicated mountpoint (either tmpfs or on-disk) in /etc/fstab . To disable the automatic mount, mask the tmp.mount systemd unit.
Files will no longer be stored in a tmpfs, but on the block device instead. The /tmp contents will now be preserved between reboots, which might not be the desired behavior. To regain the previous behavior and clean the /tmp directory automatically when restarting, consider using tmpfiles.d(5) :
# see tmpfiles.d(5) # always enable /tmp directory cleaning D! /tmp 1777 root root 0 # remove files in /var/tmp older than 10 days D /var/tmp 1777 root root 10d # namespace mountpoints (PrivateTmp=yes) are excluded from removal x /tmp/systemd-private-* x /var/tmp/systemd-private-* X /tmp/systemd-private-*/tmp X /var/tmp/systemd-private-*/tmp
Troubleshooting
Opening symlinks in tmpfs as root fails
Considering /tmp is using tmpfs, change the current directory to /tmp , then create a file and create a symlink to that file in the same /tmp directory. Permission denied errors are to be expected when attempting to read the symlink due to /tmp having the sticky bit set.
This behavior can be controlled via /proc/sys/fs/protected_symlinks or simply via sysctl: sysctl -w fs.protected_symlinks=0 . See Sysctl#Configuration to make this permanent.