How to create a read-only RAM disk on macOS from the command line?

On macOS Catalina, a RAM disk with 512MB space can be created with the following command:

diskutil eraseVolume HFS+ "RAMDisk" `hdiutil attach -nomount ram://1048576`

On Ubuntu, a RAM disk and its read-only shadow can be created by:

mount -t tmpfs -o size=512m tmpfs /mnt/ramdisk
mount -t aufs -o br:/mnt/ramdisk=ro none /mnt/readonly

So how can I create a read-only RAM disk on macOS Catalina with command line like this, or create a read-only shadow of existing RAM disk?


  1. Get the device number of the RAM disk: diskutil list (e.g. disk3)

    Add some content to the RAM disk 😉.

  2. Unmount the volume: diskutil umount disk3
  3. Mount the RAM disk read-only: diskutil mount readOnly /dev/disk3.

Apply the disk number according to your environment!

  • unmount/mount works for an HFS+ disk (in case of a RAM disk)/volume or an APFS volume only
  • unmountDisk/mountDisk works for HFS+ disks and APFS container schemes.

If you format the RAMDisk as APFS container and one APFS volume

diskutil partitionDisk $(hdiutil attach -nomount ram://1048576) 1 GPTFormat APFS 'RAMDisk' '100%'

you can either mount the APFS container scheme or the APFS volume:

  1. Get the device number of the RAM disk's container scheme: diskutil list (e.g. disk4)
  2. Unmount the volume: diskutil umountDisk disk4
  3. Mount the RAM disk read-only: diskutil mountDisk readOnly /dev/disk4

or the RAM disk's APFS volume:

  1. Unmount the volume: diskutil umount disk4s1
  2. Mount the RAM disk read-only: diskutil mount readOnly /dev/disk4s1

As one-liner for an HFS+ volume with the unique name RAMDisk:

RD=$(diskutil list | awk '/RAMDisk/ { print $5 }'); diskutil umount $RD; diskutil mount readOnly $RD; exit

As one-liner for an APFS volume with the unique name RAMDisk:

RD=$(diskutil list | awk '/RAMDisk/ { print $7 }'); diskutil umount $RD; diskutil mount readOnly $RD; exit

Thanx to user3439894 for all his hints & comments...