Linux RAM Disk: Creating A Filesystem In RAM

Software RAM disks use the normal RAM in main memory as if it were a partition on a hard drive rather than actually accessing the data bus normally used for secondary storage such as hard disk. How do I create and store a web cache on a RAM disk to improve the speed of loading pages under Linux operating systems?

You can create the ram disk as follows (8192 = 8M, no need to format the ramdisk as a journaling file system) :
# mkfs -q /dev/ram1 8192
# mkdir -p /ramcache
# mount /dev/ram1 /ramcache
# df -H | grep ramcache

Sample outputs:

/dev/ram1              8.2M   1.1M   6.7M  15% /ramcache

Next you copy images or caching objects to /ramcache
# cp /var/www/html/images/*.jpg /ramcache
Now you can edit Apache or squid reverse proxy to use /ramcache to map to

     DocumentRoot /ramcache
     #ErrorLog /var/logs/httpd/images.example.com_error.log
     #CustomLog /var/logs/httpd/images.example.com_access.log combined

Reload httpd:
# service httpd reload

Now all hits to will be served from the ram. This can improve the speed of loading pages or images. However, if server rebooted all data will be lost. So you may want to write /etc/init.d/ script to copy back files to /ramcache. Create a script called

mkfs -t ext2 -q /dev/ram1 8192
[ ! -d /ramcache ] && mkdir -p /ramcache
mount /dev/ram1 /ramcache 
/bin/cp /var/www/html/images/*.jpg /ramcache

Call it from /etc/rc.local or create softlink in /etc/rc3.d/
# chmod +x /path/to/
# echo '/path/to/' >> /etc/rc.local

A Note About tmpfs

tmpfs is supported by the Linux kernel from version 2.4+. tmpfs (also known as shmfs) is a little different from the Linux ramdisk. It allocate memory dynamically and by allowing less-used pages to be moved onto swap space. ramfs, in contrast, does not make use of swap which can be an advantage or disadvantage in many cases. See how to use tmpfs under Linux.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 31 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
31 comments… add one
  • jaysunn Mar 5, 2010 @ 13:09

    This is very interesting. I am going to show this to one of my colleuges in regards to this benifiting us.

    Great Tutorial.


  • Dariusz Mar 5, 2010 @ 13:22

    /dev/ram1 seem not exist on Suse based systems:

    linux-jgbr:/home/dpanasiuk # ls /dev/ra*
    linux-jgbr:/home/dpanasiuk # cat /etc/issue
    Welcome to openSUSE 11.2 “Emerald” – Kernel \r (\l).

    linux-jgbr:/home/dpanasiuk #

  • 🐧 nixCraft Mar 5, 2010 @ 13:38


    mknod /dev/ram1 b 1 1
    chown root:disk /dev/ram1
    chmod 0660 /dev/ram1
    mkfs -t ext3 -q /dev/ram1 8192
    mkdir -p /ramcache
    mount /dev/ram1 /ramcache
    df -H

    Let me know…

    • Sharad Dec 7, 2011 @ 6:22

      How to unmount RAM ??

      • Marcos Jun 20, 2013 @ 16:18

        if you mounted the ram like this mount /dev/ram1 /mnt/ram1 then right click on folder /mnt/ram1 and select unmount. that simple, on puppy linux anyway. Contents of ram will still be there until remount or shutdown.

        • Marcos Jun 20, 2013 @ 16:29

          use ‘umount’ tmpfs /’dir’

          if you mount tmpfs or a ramdisk on a folder that has ‘stuff’ in it, the stuff vanishes, until the folder is unmounted and there it still is!

          • Marcos Jun 20, 2013 @ 16:42

            but if you unmount tmpfs you lose what is in there, but on ramdisk if it is unmounted it retains the data.

    • pp Apr 1, 2012 @ 17:56

      It works in SuSE 11.4

  • Swen Schroder Mar 5, 2010 @ 13:41

    Could you please compare the speed with serving the files from disk? I believe this is completely useless – as the RAM disk will only steal memory which would otherwise be used as file system cache, which will be quiet as efficient as serving the files directly from RAM!

  • tiptop Mar 5, 2010 @ 13:47

    @Swen, Why not run ab against such a setup? I guess you trust kernel too much.

    On a related note, you can use ramdisk to improve privacy too.

  • Diggity Mar 5, 2010 @ 14:00

    why use a journaled filesystem for the ramdisk?

  • 🐧 nixCraft Mar 5, 2010 @ 14:04

    @Diggity, ignore -t ext3 but you are right on spot.

    @ Swen, let face it disk i/o going to be slow as compare to your ram. Not all projects are same, so it may or may not produce results at the end of day. YMMV.

  • Dariusz Mar 5, 2010 @ 14:13

    @Vivek Gite

    Your instructions for Suse (and I suppose for any other Linux which doesn’t have /dev/ram1 created by default) worked.

    dpanasiuk@linux-jgbr:~> uname -a
    Linux linux-jgbr #1 SMP 2010-01-27 08:20:11 +0100 i686 i686 i386 GNU/Linux
    dpanasiuk@linux-jgbr:~> df -H | grep ram
    /dev/ram1 8.2M 1.1M 6.7M 15% /ramcache


  • Ravi Chandra Mar 6, 2010 @ 6:12

    Super post.

  • Boris Mar 6, 2010 @ 11:21

    On Debian, I tryed to do this :
    mkfs -q /dev/ram1 97468
    (arround 96M).
    I guess this alert :
    # mkfs -q /dev/ram1 97468
    mkfs.ext2: Filesystem larger than apparent device size.
    Proceed anyway? (y,n)

    I don’t know if it’s really dangerous.

  • 🐧 nixCraft Mar 6, 2010 @ 13:24

    Pass ramdisk_size=N option to linux kernel. You need to edit grub.conf and pass that option. Once done reboot the system. This command is added to the kernel line in grub. N must be in K size. See

  • Tapas Mallick Mar 6, 2010 @ 14:16

    Nice Post as usual :)
    if we unmount the ram disk, will it still reserve RAM or release it for system’s normal use ?

  • 🐧 nixCraft Mar 6, 2010 @ 14:29

    @ Tapas,

    Short answer – No it will not release it.

    Long answer – once memory has been allocated to the ramdisk, it can not be freed until you reboot the system. It will get flagged so that the Linux kernel will not try to reuse the memory again. Therefore, you cannot reclaim the RAM after you are done with using the ramdisk. If you remount the ramdisk, your data will still be there.

    cp /path/something.txt /ramdisk
    umount  /ramdisk
    mount /dev/ram1  /ramdisk
    ls /ramdisk/something.txt


  • Amitabh Patnaik Mar 11, 2010 @ 16:13

    This rocks! Thanks for sharing : )

  • Borys Łącki Mar 22, 2010 @ 17:04

    # mkfs -q /dev/ram1 8192

    # hdparm -tT /dev/{hda,ram1}

    Timing cached reads: 1512 MB in 2.02 seconds = 750.01 MB/sec
    Timing buffered disk reads: 154 MB in 3.03 seconds = 50.76 MB/sec

    Timing cached reads: 1350 MB in 2.00 seconds = 674.96 MB/sec
    Timing buffered disk reads: 16 MB in 0.03 seconds = 561.60 MB/sec

  • chester Apr 16, 2010 @ 4:31

    to use memory as storage and still allow the kernel to reuse the memory, you can use ramfs or the newer tmpfs

    just mount a directory
    mount -t tmpfs tmpfs /dir

    if you don’t fill up the space in the tmpfs, the kernel gets to use the free RAM for buffers and cache. If you delete a file, the kernel will reclaim the ram, and if you umount the directory, all data is lost

  • Steffen Schaumburg Jul 26, 2010 @ 1:18

    The /dev/ram method didn’t work for me, even after running mknod. I probably deselected a necessary kernel option. But the tmpfs method is working great so far, thanks :)

  • Jon Doe Sep 16, 2010 @ 21:50

    “How do I create and store a web cache on a RAM disk to improve the speed of loading pages under Linux operating systems?”

    Linux is a kernel, commonly used together with the GNU operating system instead of Hurd. In that case the OS is GNU with Linux or GNU/Linux.

  • Robert Grubba Aug 30, 2011 @ 9:43

    Thanks for Your article, it sounds really neat and I will try & test performance boost from Your solution soon.

    I’m thinking of putting my entire wordpress directory on ramdisk (and in the future also database files). For the wordpress files part it seems quite easy to do – entire directory would be on ramdisk with rsync syncing it with backup folder on harddisk (lets say in 30 minutes cycles).
    Or maybe there is a chance of setting apache to write to one location (harddisk) and serve files from another one (ramdisk which would be rsync’ed from harddrive in that case)?

    But how to manage syncing of database files form ram to local filesystem? As far as I remember I shouldn’t rsync db files when database server is running (or maybe mysqlhotcopy would do the job?)… how to cope with unexpected power-downs – I can create shutdown script syncing database after shutting down database server, but it will not manage any restarts from hardware/power failures in that way.

    Any advice would be welcome.

    • Marcos Jun 20, 2013 @ 15:31

      perhaps by creating a virtual raid 0 mirror!? with the mirror as a real folder on hd ;-)

  • Marco Jul 5, 2012 @ 20:03

    How can I erase the filesystem in /dev/ram1?

    • Marcos Jun 20, 2013 @ 15:26

      there are several ways;
      one is make a new file system; mke2fs -t ext2 /dev/ram1 or ext3, fat32 or ?
      another is to use dd if=/fs.img of=/dev/ram1
      you could also write ones or zeros to it……

  • Guest Apr 6, 2013 @ 11:10

    Ramdisk/tmpfs is kind of last resort. It’s almost never needed. Linux kernel uses all the free RAM for file cache by default, so all the frequently accessed files remain cached in RAM. Short-lived temporary files may stay in cache and not hit the disk at all. Linux kernel is smart. :)

    There’re just a few cases when ramdisk/tmpfs is much faster than real filesystem (and most of them can be reported as a bug). Usually there’re better alternatives.

    For example to reduce number of disk write attempts you can increase vm.dirty_* sysctls ( If you’re trying to reduce number of HDD spinups for a laptop, then vm.laptop_mode and laptop-mode-tools package would give you the best solution. If your software uses a lot of slow fsync’s that you need to avoid, then `eatmydata` wrapper is just for you.

    Ramdisk takes your RAM from other apps even when not used, and often it’s not easy to keep it in sync with your local data. The only way to make sure that you need it is to benchmark it, not with some general benchmarking tool, but with your particular test case.

  • Marcos Jun 20, 2013 @ 15:13

    add to kernel boot option in grub; ramdisk_size=409600 # for 400mb ramdisk;

    kernel /vmlinuz ramdisk_size=409600 #or how much you want

    this states the max amount of ram for ramdisk but doesn’t actually use up ram until you write to the ramdisk! Actually i run a 6gb ramdisk.

    after boot in terminal enter;

    mke2fs -t ext2 /dev/ram1 409600

    or ‘mkfs’ this formats a ramdisk ‘ram1’ with ext2 filesystem and fixes the max size (make smaller but not bigger, although if you have enough ram you can make several, ram2, ram3 etc)
    next in term mount the ramdisk ‘ram1′

    mount /dev/ram1 /mnt/ram1
    mount /dev/ram1 /mnt/dvd #i have no dvd so it is available. or /mnt/’your choice’

    you can edit /etc/fstab to automount and specify fs at boot up.
    I hope someone finds this useful. I use it on puppy linux fatdog64 and the totally amazing Lighthouse64 distros.

  • ScottInNH Dec 2, 2013 @ 14:49

    Folks –
    Before accepting this as a solution to performance or other problem you face, please be sure to benchmark the “with” and “without” this method.

    Borys’ hdparm benchmark _is_ insightful (if perhaps a bit too concise that some folks didn’t understand it).

    If your aim is to improve I/O performance, a RAM disk will usually NOT provide the answer you seek because the Linux kernel itself contains a “RAM disk of sorts” by implementing file caching in RAM.

    So for example: You have a webserver with images which is hosted on a slow volume.

    With a RAM disk: If you access the file once, it’s “slow” but then the file is cached by the kernel so all further reads are faster.

    With RAM disk: The first time you access the file, it _seems_ faster. but this is misleading because really this is your “second” read attempt and it’s already cached by the kernel. The first read occurred when you copied the file from disk to RAM. This explains why the RAM filesystem is almost the same speed multiple reads to (cached) disk.

    You could achieve fast disk caching by having an INIT script which simply does a “cat /some/file > /dev/null” and therefore the file ends up in cache. This trick is actually quite useful for things like proxies or file mounts hosted on slow remote NAS devices. Once you have “read” the files in some kind of preloader script, you spare your first visitor any penalty for first access.

    The invention of fast disk caching has eliminated the performance need for RAM disks (which used to be so common in the 80’s and early 90’s, and made obsolete with utilities like DOS “smartdrv” or OS like Linux and *BSD).

    That said this is a fun trick to try out. And you might very well have a need for files which “disappear” when the OS is powered off. You can also use this with ssh-keygen if you wanted to output the keys somewhere without touching the local filesystem (an obscure application to be sure, but one I was investigating).

  • Larr Apr 25, 2014 @ 15:52

    If you’re using a ramdisk… Don’t forget to add your ramdisk to /etc/fstab, to mount it automatically between boots.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum