Mount a DMG from the Command Line in Mac OS X

Dec 17, 2011 - 4 Comments

Mount a DMG from the Command Line in Mac OS X

A helpful command line tool called hdiutil is included in Mac OS X that allows disk image files (.dmg extension) to be mounted directly from the Terminal, without the need of using the GUI. Using hdiutil for such a task is helpful for scripting or remote connections through SSH.

Mounting a Disk Image File (DMG) from the Terminal

From the command line use the following syntax to mount the image:

hdiutil attach /path/to/diskimage.dmg

When the .dmg is mounted, a message will display something like this:

/dev/disk1 Apple_partition_scheme
/dev/disk1s1 Apple_partition_map
/dev/disk1s2 Apple_HFS /Volumes/Mounted Disk Image

Take note of the last entry with the name of the just mounted disk image, you will use the first part of this line to unmount the disk (/dev/disk1s2).

Unmounting a DMG from the Terminal

Recalling that the device name of the mounted image, use the following syntax to unmount the dmg:

hdiutil detach /dev/disk1s2

If you missed the device location earlier, you can retrieve information on all mounted drives and images by using:

hdiutil info

Locate the name of the mounted image and note the /dev/disk entry alongside of it.

The hdiutil tool works to mount .iso and other image files as well as being able to convert them to other disk image formats.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: William Pearson in Command Line, Mac OS X, Tips & Tricks

4 Comments

» Comments RSS Feed

  1. bernuli says:

    Re unmounting. “hdiutil info” under 10.6.8 does not give me the /dev/diskname/. However, the “df” command does

  2. Iekei says:

    good tip

  3. scott says:

    …or instead of bothering with hdiutil, you could also just type the easier-to-remember “open /path/to/diskimage.dmg”.

    There’s no such shortcut for unmounting though.

  4. MJF says:

    Apparently, as of OS X 10.9.5, you can unmount using the path to the volume. E.g.:

    hdiutil detach /Volumes/Mounted\ Disk\ Image

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates