I’ve utilised SFTP on my Android device for many years now; some methods were more complex than others and often more than they needed to be (Super user access needed) but in recent years from about Android 7 onward I stopped using rooted methods via Busybox and Dropbear SSH amongst others and settled on a method with Termux that also enables access to shared storage and external storage devices.
Table of Contents
Additionally on top of SFTP access you can also connect an SSH client and run commands available in your Termux installation.
Configuring your device
First you will want to download and install Termux from the Play Store then within Termux you need to install the OpenSSH package:
apt install openssh
Now you can either transfer an existing key or generate a new key.
Configuring the OpenSSH key pair
There is no support for using password authentication in Termux. Therefore you will need to use and put your OpenSSH public key into the ~/.ssh/authorized_keys
file. If you don’t already have a key pair you use you can generate one.
Creating a new key pair
Should the authorized_keys
file not exist. This file will need to be created and have its permissions set to 600.
touch ~/.ssh/authorized_keys
If you do not have a OpenSSH key pair yet, you can generate one with the following command:
ssh-keygen
After generating a new key the public key it needs to be added to your authorized_key
file.
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
Check the authorized_keys
file to ensure the public part has been added:
cat ~/.ssh/authorized_keys
To connect from another device you will need to take a copy of your newly created private key.
cat ~/.ssh/id_rsa
Using the cat
command will output the private key onscreen but does makes it easy to copy to a file or elsewhere.
Alternatively: copy an existing Public key
If you already have a key pair you wish to use. You want to copy the contents of your public key: example.pub
file to your authorized_keys
file. You can use the following command from your SSH client (local on device or from another device). Replace[KEY-HASH-HERE]
with your public key files contents and transfer to phone:
echo "ssh-rsa [KEY-HASH-HERE] " > ~/.ssh/authorized_keys
Tip: You could prepare the command above adding your public key contents and copy it as text to your device. Then you can copy it to your devices clipboard for local execution in Termux.
Checking file permission
Lastly you may need to fix up permission:
# Set Permissions to the file chmod 600 ~/.ssh/authorized_keys # Make sure the folder .ssh folder has the correct permissions chmod 700 ~/.ssh
Start the SSHD
service with the following command:
sshd
And that’s all there is too it; the SSH service should now be running on port 8022. Execute the sshd
command whenever you need to start the service and the command exit
will close the shell session also closing the SSHD service.
You can test connectivity on port 8022 locally within Termux using:
ssh localhost -p 8022
When you are connecting from any other devices you can specify any username you wish but you must ensure you use the correct private key.
Once connected; the path to the main internal SD root is: ftp://<ip-address>:8022//storage/emulated/0/ .
Note that the actual path may vary slightly on different devices; the path /storage/emulated/0/
comes from usage on Samsung based devices.
Termux ~/storage location details
The contents of the created $HOME/storage
folder are symlinked to different storage folders featured on the device. The table below covers several of these Termux storage locations and there intended usage:
Termux Storage Locations | Description |
~/storage/shared | The root of the shared storage between all apps. |
~/storage/downloads | Downloads from e.g. the system browser. |
~/storage/dcim | Pictures and videos taken with the device camera. |
~/storage/pictures | Other pictures. |
~/storage/music | Audio files that should be included in the music library. |
~/storage/movies | Videos and other Movie type files. |
~/storage/external | Symlinked to a Termux-private folder on external storage (only if external storage is available). |
Enabling external storage access within Termux
To use external storage path ( ~/storage/external
) you will need to grant Termux additional permission. In Termux run the following command: termux-setup-storage
and answer yes to the on screen permission prompts.
From this point forward will now be able to access external storage locations from within Termux and/or from remote SFTP or SSH sessions by accessing path: ~/storage/external
.
Note: the section Configuring the OpenSSH key pair is also relevant for configuring OpenSSH on popular Linux distributions.