The Magic of DMG Files
If you download a new software application on a Mac (like Google Chrome or Adobe Photoshop), it usually arrives as a single file with a .dmg extension. When you double-click that file, macOS magically mounts a brand new “virtual hard drive” on your desktop. This virtual drive acts exactly like a physical USB flash drive, allowing you to drag the application out of it and into your Applications folder.
These Disk Images (DMGs) are the cornerstone of software distribution on macOS. They are essentially highly compressed, self-contained file systems that protect their internal data from corruption during internet transfers.
If you are a software developer trying to package your new app for distribution, or a systems administrator attempting to extract data from a corrupted backup image on a remote server, you cannot rely on double-clicking a graphical icon. You must have absolute control over the virtual disk mounting system. To create, convert, encrypt, and mount Disk Images directly from the command line, you must use the incredibly powerful hdiutil (Hard Disk Image Utility) command.
Step 1: Open the Terminal
Because you are manipulating virtual file systems, some advanced hdiutil operations require administrator privileges, but basic mounting and unmounting can be done as a standard user.
- Press Command + Space to open Spotlight Search.
- Type
Terminaland press Enter.
Step 2: Mounting a Disk Image
If you SSH into a remote Mac and need to install software from a DMG file you just downloaded via curl, you cannot double-click it. You must forcefully mount it via the terminal.
To attach the virtual drive to the operating system, use the attach argument followed by the exact path to the file.
hdiutil attach ~/Downloads/installer.dmg
The terminal will pause for a second as it verifies the cryptographic checksums of the file (ensuring it was not corrupted during download). Once verified, it will output a table of data. Look at the far right column of the output. It will show you the exact folder path where the virtual drive is now accessible, which is almost always located in the /Volumes/ directory (e.g., /Volumes/Application_Installer).
You can now use standard cd and cp commands to navigate into that volume and copy the software out.
Step 3: Unmounting (Ejecting) the Image
Once you are finished extracting the software, you cannot just delete the .dmg file. The operating system is still actively running a virtual hard drive from that file. You must safely eject the virtual drive first.
You use the detach argument, followed by the specific /Volumes/ path that was generated in Step 2.
hdiutil detach /Volumes/Application_Installer
The virtual drive is instantly severed, freeing up system memory and allowing you to safely delete the original .dmg file.
Step 4: Creating a Brand New Disk Image
The true power of hdiutil is creation. If you have a folder full of highly sensitive corporate documents (e.g., ~/Desktop/SecretDocs) and you want to package them into a secure, encrypted file to email to a coworker, you can generate a DMG entirely from scratch.
To create a brand new Disk Image from an existing folder, use the create argument. You specify the name of the new file, use the -srcfolder flag to tell it what to put inside, and you can add military-grade encryption using the -encryption flag.
hdiutil create -encryption -srcfolder ~/Desktop/SecretDocs ~/Desktop/SecureArchive.dmg
The terminal will immediately halt and demand you create a secure password. Once you enter the password, macOS will algorithmically compress the folder, encrypt it with AES-128 cryptography, and output a pristine SecureArchive.dmg file onto your desktop. Without the password, the data inside that file is mathematically impossible to read.
Step 5: Converting Image Formats
If you download a massive Linux operating system installer, it will likely be in the .iso format (the global standard for CD/DVD images), rather than the Apple-specific .dmg format.
While macOS can usually read .iso files, sometimes you need to convert them to a native format to manipulate them further.
hdiutil convert ~/Downloads/linux_installer.iso -format UDRW -o ~/Desktop/converted_installer.dmg
The -format UDRW flag stands for “UDIF Read/Write,” transforming the static ISO into a dynamic macOS Disk Image that you can actively edit and modify. By mastering hdiutil, you command the entire architecture of Apple’s software distribution ecosystem.