Ubuntu – Disable automount of a specific mtp device

16.04automountmountmtpnautilus

On a Ubuntu 16.04 Desktop, I have a phone that is mounted using MTP as soon as I plug it in. I don't want this to happen, I actually just want it to charge off my computer's USB power. When I plug other devices in, I do want them to automount so I only want to stop the default action for this specific device.

How can I accomplish this?

Best Answer

First, find the VendorID and ProductID of the device using lsusb. For my Samsung Galaxy S7 Edge, the appropriate line is below.

Bus 001 Device 029: ID 04e8:6860 Samsung Electronics Co., Ltd Galaxy (MTP)
---------------VendorID--^ : ^--ProductID 

You also need the directory associated with where this device is plugged in. You can go hunting around /sys/bus/usb/devices/*/idProduct manually, or you can use this quick little script by @radu-rădeanu.

Save this to ~/bin/findUSBbyID.sh and chmod +x it.

#!/bin/bash

if [ $# -ne 2 ];then
  echo "Usage: `basename $0` idVendor idProduct"
  exit 1
fi


for X in /sys/bus/usb/devices/*; do 
    if [ "$1" == "$(cat "$X/idVendor" 2>/dev/null)" -a "$2" == "$(cat "$X/idProduct" 2>/dev/null)" ]
    then
        echo "$X"
    fi
done

Run it as ~/bin/findUSBbyID.sh 04e8 6860. The output will look somthing like this:

/sys/bus/usb/devices/1-1.2

With this information you want to create a new file called /etc/udev/rules.d/90-disable-usb-device.rules. You'll need root permissions so sudo or gksudo your favorite editor as appropriate.

The file should contain one line like below. Correct the VendorID and ProductID to match your device. Also correct the /sys/bus/usb/devices/$something/authorized path to match your product as found with the findUSBbyID.sh script.

ACTION=="add", ATTR{idVendor}=="04e8", ATTR{idProduct}=="6860", RUN="/bin/sh -c 'echo 0 > /sys/bus/usb/devices/1-1.2/authorized'"

You may get another popup "Unable to mount an MTP device". That issue is being discussed here.