Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
The Media Transfer Protocol (MTP) is a protocol that enables us to transfer data between two devices. MTP is primarily used in devices running the Android operating system.
In this article, we’ll discuss how we can mount the MTP devices under Linux. We’ll cover the gvfs-mtp package, which comes with most major Linux distributions. Afterward, we’ll take a look at the jmtpfs, which allows us to mount and unmount MTP devices using the command line.
GNOME Virtual File System (GVfs) is a userspace virtual filesystem wrapper around the GNOME Input-Output (GIO) library. GVfs abstracts the inner workings of the GIO library, allowing us to access various filesystems and services without any special technical knowledge.
GVfs comes with several backends for file systems and services like Apple Mobile Devices (AFC), Google Online Accounts (GOA), and SMB. One of these modules is gvfs-mtp.
The gvfs-mtp module is designed to work solely with MTP devices. It lets us access the MTP devices through a FUSE file system.
If we’re on the GNOME desktop environment, chances are we have gvfs-mtp already installed. However, if that’s not the case, we can install it from our distro’s official package repository using a package manager:
# Ubuntu-based
$ apt install gvfs-mtp
# Fedora and RHEL
$ dnf install gvfs-mtp
# Arch-based
$ pacman -S gvfs-mtp
Before we connect our device using MTP:
Once that’s done, we’ll check whether our device is successfully connected to our machine using lsusb:
$ lsusb | grep -i smartphone
Bus 001 Device 006: ID 12d1:107e Huawei Technologies Co., Ltd. P10 smartphone
Alternatively, we can also use gio to detect our device:
$ gio mount -li | grep activation_root
activation_root=mtp://HUAWEI_HUAWEI_VNS-L21_BUC4C16523021942/
If you don’t see your device, refer to system logs to troubleshoot any issues.
With the gvfs-mtp package installed, we can now mount our MTP device either using the gio utility or a graphical file manager such as GNOME Files.
We can mount our device using the path found in the activation_root field of the gio command:
$ gio mount "mtp://HUAWEI_HUAWEI_VNS-L21_BUC4C16523021942/"
Now, we can access our mounted device using the directory that begins with the mtp:host= directory located in /run/user/$UUID/gvfs:
$ ls -l /run/user/1000/gvfs
2 dr-x------ 1 hey hey 0 Jan 1 1970 'mtp:host=HUAWEI_HUAWEI_VNS-L21_BUC4C16523021942'
Alternatively, we can also mount our device using the Bus and Device Number found in the lsusb output:
$ gio mount "mtp://[usb:001,006]/"
We can unmount the device using the -u or –unmount flag of the mount sub-command:
$ gio mount -u "mtp://HUAWEI_HUAWEI_VNS-L21_BUC4C16523021942/"
File managers that support GVfs will try to automount the MTP device — depending on how the distro is configured.
However, we can easily mount the MTP device with a click, as we can see in the following illustration:
Similarly, we can unmount the device using the eject button shown to the right of the device label.
jmtpfs is a lightweight utility based on libmtp, which we use to read and write data to MTP devices.
By default, Linux distros don’t come with jmtps, and it’s not available on most package repositories. Therefore, we need to build it from the source.
Before we install jmtpfs, we should install the libmtp dependency from the official package repository using a package manager.
Now, let’s clone the jmtpfs repository:
$ git clone https://github.com/JasonFerrara/jmtpfs
Now, let’s configure and compile jmtpfs:
$ ./configure
$ make
$ make install
Once that’s successful, let’s verify it:
$ jmtpfs
jmtpfs version: 0.5
By default, MTP devices are only readable/writable by root users. Therefore, we should first allow other users to read and write to the MTP devices.
So, uncomment the user_allow_other option in the /etc/fuse.conf file:
$ sed -i '/user_allow_other/s/^#//g' /etc/fuse.conf
Now, we’re going to mount our MTP device on a directory that has read and write permissions:
$ jmtpfs ~/mtp
Similarly, we can unmount our device using fusermount:
$ fusermount -u ~/mtp
Additionally, we can make this mount process cohere with the rest of Linux — allowing us to use the regular mount and unmount commands.
First, we’ll create a soft symbolic link of jmtpfs. The link’s name is going to be consistent with the Linux mount conventions like mount.jmtpfs:
$ ln -s /usr/local/bin/jmtpfs /usr/local/bin/mount.jmtpfs
Then, we’ll add an entry for our MTP device in the fstab file:
jmtpfs /home/user/mtp fuse nodev,allow_other,rw,user,noauto,noatime,uid=1000,gid=1000 0 0
Now, we’re ready to mount our device using mount:
$ mount /home/user/mtp
Similarly, we can now unmount our device with umount:
$ umount /home/user/mtp
In this article, we covered how we can mount and unmount MTP devices under Linux. We briefly went over what GVfs is and how we can use its gvfs-mtp module to mount and unmount MTP devices in the CLI and File managers.
Finally, we covered the alternative jmtpfs utility to mount and unmount MTP devices and how we can make this utility blend with the Linux mounting convention.