How to Extract DMG Files in Linux
Opening image files on Linux is often a straightforward process. DMG files are, however, a bit more complicated since Linux doesn’t natively support these types of files as well as the file system they use. But if you only need to extract a DMG file to view and copy its content over to your Linux machine, there are a few workarounds.
Let’s first talk about what a DMG file is and why Linux-based systems have a harder time opening them compared to image files like ISO.
What Is a DMG File?
Unlike cross-platform image files like ISO and IMG, DMG files are only meant to be used by a Mac. This is because DMG files are proprietary disc image file formats created by Apple to share and distribute files and software for their products.
A disc image file is a type of file that acts like a physical disc drive. These types of files are cloned from actual hard drives and provide a “byte-per-byte” copy of all the data stored.
Disc image files such as DMG are often used to distribute software since developers can easily create an image file of their current environment with all its dependencies and distribute the software as a package.
This makes it easier for users to install the software since all the dependencies, configurations, and files are already present just like how the developers have it on their machines.
Although the DMG file format was made to distribute Apple software, it’s also used as a way to make backups, archive environments, and share all kinds of media in a neatly compressed package.
And since macOS already comes with a native DMG creation tool, Mac users will likely use DMG instead of other disc image file formats such as ISO.
Since not everyone uses a Mac, extracting a DMG image file outside a Mac device may prove to be problematic. So why extract such a file on Linux?
According to Statista, the Mac takes around 17% of the US PC market, which ranks it as the second most popular personal computer system. That’s a lot of people using a Mac, which also means more people using DMG to compress and share their files.
So having a friend or family sending you a DMG file is not as uncommon as people might think. Of course, there may also be other reasons why a Linux user would want to extract a DMG file on their system.
Being a proprietary image format, DMG files are harder to mount and extract on Linux since it doesn’t officially support the use of DMG.
Since most Linux distros use Extensible File Systems such as ext2, ext3, and ext4, mounting an HFS or HFS+ image on Linux will lead to incompatibilities.
Although these problems make it harder to extract DMG files outside a Mac, there are a few workarounds to successfully extract files in Linux.
The biggest problem with extracting a DMG file on Linux is that mounting one will result in a bad file system error. To successfully mount a DMG, you’ll need to install a utility known as “hfsprogs”. This command-line tool is a port from Apple’s HFS hdiutil tool that enables their system to mount HFS-type images.
By installing hfsprogs, your Linux machine will be capable of mounting HFS-type images such as DMG.
Before you install hfsprogs, make sure to update the packages on your computer.
Now, install hfsprogs on Ubuntu/Debian using:
sudo apt install hfsprogs
On Arch Linux, install hfsprogs from the AUR using yay:
yay -S hfsprogs
On Fedora, CentOS, and RHEL, use:
dnf install hfsplus-tools
To mount the DMG image file, let’s make a new mount point by creating a new directory:
sudo mkdir /mnt/mntpoint
Let’s mount the DMG file by running:
sudo mount -t hfsplus /dmg/location/Image.dmg /mount/mntpoint
That should mount the DMG image file successfully. But in this case, we’ve been prompted with a wrong file system type error.
On running the following command, you can see the utility throws a bad file system error because the DMG is compressed. Linux particularly doesn’t like mounting compressed image files.
Accessing Compressed DMG Files
To solve the problem, let’s use a utility tool known as dmg2img. This tool will convert and decompress the DMG file to an IMG file.
To install dmg2img on Ubuntu/Debian, run:
sudo apt install dmg2img
On Arch Linux:
sudo pacman -S dmg2img
On RPM-based distros, use:
sudo dnf install dmg2img
To use dmg2img use:
dmg2img will now compress and convert the DMG to an IMG file. Let’s try to mount the image file by using:
sudo mount /img/file/location/image.img /mount/point/location
Note that you no longer need to use the -t hfsplus option in the command since Linux natively supports IMG files.
Now if you view the contents of the mount point, you should see all the files within the DMG file.
Unmounting DMG Files on Linux
Mounted disc image files actively take up system resources and valuable memory space. Don’t forget to unmount disc image files after every use.
To unmount, use the command:
sudo umount /mount/point/location
Keep in mind that the command is umount and not unmount.
To check if the unmount was successful, simply ls or cd into the mount point location.
And since most Linux distributions support IMG files, you should also be able to mount it using the GUI by right-clicking the file and selecting the mount option, which for Linux Mint is Open With Disk Image Mounter.
As you can see, a mount icon has popped up; you can use this to unmount the image by right-clicking the icon and selecting Unmount.
If you look at the mount point location, you’d see that the files are now gone indicating the image file was successfully unmounted.
Should You Mount or Unzip on Linux?
If you need to extract files from a DMG image file, there are a few third-party tools such as hfsprogs that you can use to mount DMG files within Linux.
But if you only need to extract contents out of the DMG file, then unzipping tools such as dmg2img and 7-Zip should do the job, albeit with limited capabilities. If you need to edit, delete, or add any files to the DMG file, then proper mounting is the only way.
Whether you choose to mount or unzip a DMG file on Linux, both methods may still be unreliable at times due to the difference in file systems, the type of Linux distro you’re using, and the DMG file itself. As a viable last resort, extracting the DMG file on a VM installed with macOS will likely be a more reliable solution.