Skip to content

Instantly share code, notes, and snippets.

@gturi
Last active November 10, 2024 06:23
Show Gist options
  • Save gturi/d88f628bc7f53cac273b51bf93b25408 to your computer and use it in GitHub Desktop.
Save gturi/d88f628bc7f53cac273b51bf93b25408 to your computer and use it in GitHub Desktop.
Mount an Android device on your pc filesystem over ssh

Mount an Android device on your pc filesystem over ssh

Install Termux and setup openssh server

  • Install Termux from Github (or F-Droid) on the phone you want to SSH into. Don't use the Play Store version, it doesn't work
  • run pkg update and pkg upgrade
  • run passwd and set up a password
  • run pkg install openssh
  • run ssh-keygen -A
  • run termux-setup-storage - this will make the app ask for storage permission
  • run sshd
  • run ifconfig to know you ip address - look at inet field that looks like 192.168.X.Y

Test ssh connection

port=8022
android_ip=192.168.X.Y
ssh $android_ip -p $port

The last command will prompt to digit the password you have previously entered. Usually ssh specifies a username (username@$server_ip), if your ssh client doesn't allow you to not enter a username, just use a blank space or asterisk.

Once connected, you can display your device internal directory tree as follows:

cd /storage/emulated/0
ls

Mount android with sshfs

android_dir=/storage/emulated/0
mount_point=/mnt/android-device

id $(whoami)

sudo mkdir $mount_point
sudo chown $(whoami):$(whoami) $mount_point

sshfs \
  -o idmap=$(whoami) \
  -p $port \
  $android_ip:$android_dir \
  $mount_point

If you need to troubleshoot sshfs connection you can add the following options:

-o debug,sshfs_debug,loglevel=debug

If you want to specify a custom ssh config:

-F /path/to/.ssh/config

Stop ssh server

pkill sshd

Unmount directory

fusermount -u $mount_point
sudo umount $mount_point
sudo rmdir $mount_point
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment