Store Files on your Android Phone with Linux
Take your files on the go with Android and Linux. In this howto, I’ll show you how to easily attach your phone or tablet to your Linux PC and transfer files.
Older versions of Android supported USB Storage, but newer versions use Media Transfer Protocol (MTP) and Picture Transfer Protocol (PTP). If USB Storage is supported on your device, then there is little to do but attach it with a USB cable, and most modern Linux distros will pick it up and display the files. Transferring files is then just carried out as a regular copy operation.
Picture Transfer Protocol (PTP)
Picture transfer protocol was intended to be used by digital cameras to enable the transfer of image files from cameras to your PC. If you select this option from your phone, then it’s likely that photo applications will detect your phone and display it as a camera and allow you to transfer images from it.
Media Transfer Protocol (MTP)
Media transfer protocol is built on PTP and offers extensions to transfer any type of file. This can be used to make your device act like a USB storage device, but you’ll need to make some system configuration changes to allow this. The following steps can be used to enable this.
The software needed is a tool called mtpfs, and can be added from the software repositories by:
sudo apt-get jmtpfs
Now we need somewhere on the directory tree where your phone’s files will live. I chose to put them under the top level /media/ directory, as this is usually where storage devices are hooked in, but you can mount your phone to any existing directory. I created mine with:
sudo mkdir /media/phone
To allow non-root users, such as yourself to mount the phone, you’ll need to edit the fuse configuration. Edit /etc/fuse.conf and enable the line user_allow_other. Your fuse.conf file should now look something like:
# /etc/fuse.conf - Configuration file for Filesystem in Userspace (FUSE) # Set the maximum number of FUSE mounts allowed to non-root users. # The default is 1000. #mount_max = 1000 # Allow non-root users to specify the allow_other or allow_root mount options. user_allow_other
In order to interact with fuse, you’re best to add yourself to the fuse group. This can be done with:
sudo usermod <username> -aG fuse
Replace <username> with your user name, or the name of any user you want to add to the fuse group. Once you log out and log in again you’ll then be a member of the group.
Finally, allow members of the fuse group write access to the file location, by issuing:
sudo chgrp fuse /media/phone sudo chmod g+w /media/phone
Connect your phone to your PC with a USB cable, and select MTP from your phone. Now we can mount the phone to the file system using:
jmtpfs -o allow_other /media/phone
All going well, this will display an output something similar to:
Unable to open ~/.mtpz-data for reading, MTPZ disabled.Listing raw device(s) Device 0 (VID=0bb4 and PID=0f25) is UNKNOWN. Please report this VID/PID and the device model to the libmtp development team Found 1 device(s): 0bb4:0f25 @ bus 1, dev 6 Attempting to connect device Android device detected, assigning default bug flags Listing File Information on Device with name: (NULL)
You should now be able to see the phone’s files under /media/phone/Internal storage/ and have all the access rights needed to interact with them.
Finally, when you’re done, you’ll need to remove the mount. This can be done with:
fusermount -u /media/phone
It can also be useful to add aliases for these commands in your shell start up scripts. In an appropriate location you can add:
alias android-connect="jmtpfs -o allow_other /media/phone" alias android-disconnect="fusermount -u /media/phone"
You can then uses these aliases to connect and disconnect the phone.