Create a RAM disk in Linux
Category : How-to
There are many reasons for creating a memory based file system in Linux, not least of which is to provide a near zero latency and extremely fast area to story files. A prime use of a RAM disk is for application caching directories or work areas.
There are two main types of RAM disk which can be used in Linux and each have their own benefits and weaknesses:
- ramfs
- tmpfs
See my other post for the differences between ramfs and tmpfs.
Check the amount of free RAM you have left on your machine before creating a RAM disk. Use the Linux command free to see the unused RAM. The below is an example of a 31GB of ram in a production server.
free -g total used free shared buffers cached Mem: 31 29 2 0 0 8 -/+ buffers/cache: 20 11 Swap: 13 6 7
The free command shows the amount of RAM availale on your system in addition to the amount of memory used, free and used for caching. SWAP space is also displayed and shows if your system is writing memory to disk.
Create a folder to use as a mount point for your RAM disk.
mkdir /mnt/ramdisk
Then use the mount command to create a RAM disk.
mount -t [TYPE] -o size=[SIZE] [FSTYPE] [MOUNTPOINT]
Substitute the following attirbutes for your own values:
- [TYPE] is the type of RAM disk to use; either tmpfs or ramfs.
- [SIZE] is the size to use for the file system. Remember that ramfs does not have a physical limit and is specified as a starting size.
- [FSTYPE] is the type of RAM disk to use; either tmpfs, ramfs, ext4, etc.
Example:
mount -t tmpfs -o size=512m tmpfs /mnt/ramdisk
You can add the mount entry into /etc/fstab to make the RAM disk persist over reboots. Remember however, that the data will disappear each time the machine is restarted.
vi /etc/fstab
tmpfs /mnt/ramdisk tmpfs nodev,nosuid,noexec,nodiratime,size=1024M 0 0
See my other post for the differences between ramfs and tmpfs.
31 Comments
that_anon
7-Aug-2014 at 5:54 pmThanks for this post and the one about rams vs tmpfs.
I’m making great use of it!
# mount | grep tmpfs | wc -l
201
Rochelle
31-Dec-2014 at 8:11 pmVery informative!
Cy O’Hara
15-Jan-2015 at 1:58 pmHi, Newbie here, silly question time. Just done the free -g and the mkdir /mnt/ramdisk test, which revealed a field full of 0’s and “permission denied”, is there any advice on this?
Cy
Fabio
26-Oct-2015 at 4:59 pmYou need to type in sudo mkidir /mnt/ramdisk
Because this is what allows you to do that (sudo)
james.coyle
15-Jan-2015 at 2:47 pmI’m not quite following what happened in your scenario but if you are getting permission denied it’s because you don’t have access to write to the /mnt/ folder. You could try running this as root.
robert
16-May-2015 at 1:50 amThanks James. My two used computers are running like a rocket with Linux Mint and a Ramdisk.
Ram Sambamurthy
14-Feb-2017 at 1:47 amCan you explain how it’s running like a rocket? What did you do exactly with Linux Mint? What’s using the ramdisk?
Hardened Criminal
24-May-2015 at 3:28 amThank you!
You made my whole day better.
daveb666
3-Jun-2015 at 10:47 amGreat article – thank you!
Darpan Agarwal .
10-Oct-2015 at 1:03 pmmount -t -tmpfs -o size=21m tmpfs /mnt/ramdisk
showing error i.e. Unknown fs type ‘tmpfs’.
Metal3d
20-Oct-2015 at 5:35 pmMaybe you shuould remove the “-” before “-tmpfs”:
mount -t tmpfs …
sun
10-Oct-2015 at 2:46 pmWhat’s the difference between `[TYPE]` and `[FSTYPE]` in the command:
`mount -t [TYPE] -o size=[SIZE] [FSTYPE] [MOUNTPOINT]`? Aren’t they always the same representing the file system type?
lukasz
22-Nov-2015 at 1:34 amThank you :)
Cosmin
18-Dec-2015 at 9:16 pmGreat article,but I have one question. In windows I have a program (Primo Ramdisk) that can create RAM disk dinamically, so when use some space from it it will take from sistem RAM and when I delete the information from it, it will return to the system RAM. In this situation I will have back to my System RAM all memory that its availlable if I dont have anything in DISK RAM. So my question is this function tmpfs can do that or the memory alocated to DISK RAM is lost for the System RAM even if its not used? Or if you know a similar program that exist for linux like mine used in windows that can do that?
Heidi
26-Mar-2016 at 9:39 amThank you, this was very helpful to me.
Could some one clarify when using a ramfs ramdisk and I delete a file from it (using Gnome) is the file file still kept on the ramdisk?
Say I have a sensitive document that I decrypted to the ramdisk and then I want to delete the file without removing the ramdisk is it safe to delete it using Gnome?
howard
23-Feb-2021 at 2:29 pmI realize that this is several years late …
Using the rm command will just remove the directory entry for the file; the actual file will not be purged from memory until that memory is required for something else or a power-off (even with a reboot, it is possible that the RAM will not be sufficiently purged.)
Instead of using the rm command, look into using the shred command.
Nerus
9-Oct-2016 at 12:13 pmAnyone have any idea why htop didn’t print used memory by ramdisk ?
Rajesh
1-Dec-2016 at 6:59 pmIt was very informative. thanks
Timo
14-Mar-2017 at 7:53 pmThis article and the one on the differences between ramfs and tempfs are very helpful.
There is an obvious mistake however. It must of course be
emacs /etc/fstab
;) Thank you and best regards!
james.coyle
14-Mar-2017 at 8:03 pmNooo – the post is 100% correct ;)
Otherwise I get an error.
Pravallika KG
3-Jul-2017 at 1:17 pmHi James,
When i am running ‘mount -t tmpfs -o size=1m tmpfs /mnt/ramdisk’, i am getting following error:
mount: mounting tmpfs on /mnt/ramdisk/ failed: Invalid argument
Do i need any special permissions like should i be root user or anything? Please help me where i am missing?
James Larrowe
10-Jun-2018 at 8:17 pmRemove the tmpfs before /mnt/ramdisk and run it as root.
ZuluPro
23-Sep-2017 at 3:44 amHello James,
This isn’t a RAM disk but a RAM filesystem
kosher
23-Apr-2018 at 6:56 pmAs ZuluPro mentioned, this is not RAM disk but a RAM filesystem. Creating RAM disk would create a entry in /dev/ dir I believe.
Sean Brennan
26-May-2018 at 8:19 pm40 minutes to mere seconds to populate a mysql database with 50 megabytes of data blobs numbering 64 thousand entries. Seriously mysql seems to be all about transactions and in-memory processing just crushed traditional disk based approaches. Thanks, man!
Kristof
17-Feb-2019 at 5:29 pmHi, James,
Thanks for taking the trouble to teach us about Linux’s in-memory file systems. Just what I need.
Whilst your posts on this subject are clear and — I’m happy to believe till otherwised proven — error-free, I think an important piece of info is missing: How can I get rid of the file system and reclaim the RAM it uses when I’ve finished with it— apart from by rebooting of course?
Lothar Scholz
12-Jul-2019 at 11:05 pmI can’t specify options or file system type without becoming root. Is there any way i can create the ram filesystem in my home directory and mount it from my .profile init script ?
Raja
4-Jan-2020 at 4:37 pmThe explanation above is for a tmpfs and not ramfsI believe.
In fedora /tmp is already tmpfs type!
I am wonderingif there any benefits of creating tempfs and not using the /tmp
Sebastian
13-Nov-2020 at 9:14 amgreat article, but it’s lacking one thing to be perfect: section about unmounting:
sudo umount -f /mnt/ramdisk
howard
2-Mar-2021 at 9:49 amHere we are, over 7 years later, and your post is the #1 or #2 result when Googling “linux mount ramdisk”. It’s an easy read, clear, concise, and to the point — everything a good post should be. Congratulations, and thank you!
alex
2-Mar-2022 at 1:00 pmGreat writeup.
One thing:
“Remember however, that the data will disappear each time the machine is restarted.”
This isn’t true! I’ve added an entry to fstab and the files exist after a restart.
This is on ubuntu 21.10.