Access Android Phone With Command Line (Linux)

Recently, I needed to wipe my phone because of slow performance. But I still had multiple data items I wanted to keep like contacts, text messages, passwords, etc. I thought the process would as simple as connecting the phone to a USB drive, and using BASH to navigate the directories. I found out this isn’t such a trivial task. After a couple hours of searching different way to utilize the command line while processing data from my phone, I found the ADB (Android Debug Bridge) package on Arch Linux.

Methods I tried that failed.

1645  sudo cp mtp:host=SAMSUNG_SAMSUNG_Android_xxxxxxxxx /home/xxxxxxxxxx
1660  gvfs-mtp
1661  gvfs-mount
1699  jmtpfs
1700  sudo jmtpfs
1701  sudo jmtpfs -o allow_other /mnt
1711  mtp-connect Device 0

These are the commands I used, and obviously I installed all these packages to use those commands. But nothing worked! But the answer was really simple. The commands laid out below take five minutes tops, are stable, and work reliably.

Install ADB (Android Debugging Tools)

This program should be stored in your distribution’s repositories. I know to install ADB on Arch it was simple with the built-in package manager, pacman. Take notice that the package name is NOT ADB, but instead ‘android-tools’

# sudo pacman -S android-tools

This package was surprisingly quick to download, but I did run into some issues with it not being installed on servers close to my network. Still, after it found the package the download was almost instantaneous.

Get Your Phone’s Vendor and Device ID

I couldn’t really find any information on how lsusb formatted it’s output in reference to these ID’s, so I tried a couple different configurations of the output and finally got the right one.

# lsusb
>> Bus 002 Device 008: ID 04e8:6860 Samsung Electronics Co., Ltd Galaxy (MTP)

Once you find your Android device in the output, you want to pay attention to the two random strings of charachters separated by a colon.  So take the following list into account.

  • Vendor Id: 04e8
  • Product Id: 6860

Copy these down, as you will need them in the next step.

Put Rules in Your udev Directory

The udev rules are like the /etc/fstab rules but for mounted devices. The programs that utilize udev (which android-tools does), look to these rules in order to interact with devices that aren’t mounted as a hard-drive. If that’s over your head or I’m not explaining well enough, just make a new file, and paste the following rules into it.

# sudo touch /etc/udev/rules.d/51-android.rules
# sudo vim /etc/udev/rules.d/51-android.rules
 SUBSYSTEM=="usb", ATTR{idVendor}=="[YOUR_VENDOR_ID_NO_BRACKETS]", MODE="0660", GROUP="adbusers"
 SUBSYSTEM=="usb",ATTR{idVendor}=="[YOUR_VENDOR_ID_NO_BRACKETS]",ATTR{idProduct}=="[YOUR_PRODUCT_ID_NO_BRACKETS]",SYMLINK+="android_adb"
 SUBSYSTEM=="usb",ATTR{idVendor}=="[YOUR_VENDOR_ID_NO_BRACKETS]",ATTR{idProduct}=="[YOUR_PRODUCT_ID_NO_BRACKETS]",SYMLINK+="android_fastboot"

Reload Rules

Now that you’ve entered your udev rules, you must reload them with the following command. This command must be run with sudo, and provides no output. So no output means it was successful in loading the rules.

# sudo udevadm control --reload-rules

Add Yourself To The “adbusers” Group

When you installed android-tools, the program should have created a user group called “adbusers”. Android-tools automatically adds the ‘root’ user to the group, but you must add yourself to the group.

# sudo usermod -a -G adbusers your_linux_username

Get Cracking!

With everything set up, you should now be able to connect to your Android device with the command line. To test, run the following command.

# adb devices

A popup will appear on your phone, and hit accept. You might get some funky output so it’s best to run the command again after you accept the connection on your phone. Here’s how my adb device command worked out.

# adb device
List of devices attached
* daemon not running; starting now at tcp:5037
* daemon started successfully
9xxxxxxxxxxxxxx51	unauthorized

# adb device
List of devices attached
9xxxxxxxxxxxxxx51	device

Once you verify your connected, you can now backup your device with the following command.

adb backup -apk -shared -all your_own_filename.ab

This process isn’t too timely, but definitely enough time to watch some old vines or something : )

I will post all the commands in order here.

# sudo pacman -S android-tools
# lsusb
>> Bus 002 Device 008: ID 04e8:6860 Samsung Electronics Co., Ltd Galaxy (MTP)
# sudo touch /etc/udev/rules.d/51-android.rules
# sudo vim /etc/udev/rules.d/51-android.rules
----------------------------------------------------------
paste into file: 
 SUBSYSTEM=="usb", ATTR{idVendor}=="[YOUR_VENDOR_ID_NO_BRACKETS]", MODE="0660", GROUP="adbusers"
 SUBSYSTEM=="usb",ATTR{idVendor}=="[YOUR_VENDOR_ID_NO_BRACKETS]",ATTR{idProduct}=="[YOUR_PRODUCT_ID_NO_BRACKETS]",SYMLINK+="android_adb"
 SUBSYSTEM=="usb",ATTR{idVendor}=="[YOUR_VENDOR_ID_NO_BRACKETS]",ATTR{idProduct}=="[YOUR_PRODUCT_ID_NO_BRACKETS]",SYMLINK+="android_fastboot"
-----------------------------------------------------------
# sudo udevadm control --reload-rules
# sudo usermod -a -G adbusers your_linux_username
# adb device
List of devices attached
* daemon not running; starting now at tcp:5037
* daemon started successfully
9xxxxxxxxxxxxxx51	unauthorized

# adb device
List of devices attached
9xxxxxxxxxxxxxx51	device
adb backup -apk -shared -all your_own_filename.ab

You Might Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *