How to Transfer Files between Android and Linux

This tutorial shows how to transfer files between a Linux laptop and an Android phone using SFTP (SSH File Transfer Protocol) from the Linux terminal.

In the past, tools such as telnet and ftp were commonly used to transfer files across a network. sftp is the modern, secure equivalent of ftp, while ssh has largely replaced the insecure telnet. sftp is a sub-protocol of ssh, so it shares its authentication, encryption and host-key checking. Note that when installed through Termux, it uses port 8022 rather than the standard ssh port, 22.

The basic process

Follow these steps to get set up and start transferring files:

  1. Install Termux, a terminal emulator, on your Android phone. Check its local IP address with ifconfig (we will assume 192.168.6.140 for this example) and username with whoami (for example, u0_a999). Set a password for your user using passwd.
  2. In the Termux shell, install OpenSSH.
  3. $ pkg install openssh
  4. Ensure the sshd daemon is running. Typing sshd should start it.
  5. On your Linux laptop, ensure that an OpenSSH client is installed. This should provide sftp out of the box.
  6. Connect to your phone with sftp. For simplicity, run this from the directory where you want to save files:
$ sftp -P 8022 -o "ConnectTimeout=5" u0_a999@192.168.6.140

Edit the username and IP address in the command as required.

  1. Use standard Bash-like commands to navigate the remote filesystem. For your local filesystem, prefix standard commands with l for “local”: ls becomes lls, and cd becomes lcd.
  2. Use get to pull a file from the phone to your laptop, and put to send a file to the phone.

Connecting using a Shared Key

To connect without having to type a password, you can use a shared key pair:

  1. If you have an SSH key pair on your laptop, all well and good. If not, generate one with ssh-keygen.
  2. Copy your public key to the phone:
    ssh-copy-id -p 8022 -i ~/.ssh/id_rsa.pub u0_a999@192.168.6.140
    or ssh in and paste the public key into ~/.ssh/authorized_keys on the phone.

Then you should be able to ssh in and use sftp to transfer files without typing a password.

Example session


09:27: scratch ⧓ sftp -P 8022 u0_a999@192.168.6.140
Connected to 192.168.6.140.
sftp> pwd
Remote working directory: /data/data/com.termux/files/home
sftp> cd storage/shared/Temp/
sftp> ls
getme.txt  test.txt   
sftp> lpwd
Local working directory: /home/john/scratch
sftp> lls
putme.txt
sftp> get getme.txt renamed_getme.txt
Fetching /storage/emulated/0/Temp/getme.txt to renamed_getme.txt
getme.txt                                                                                   100%   32     6.3KB/s   00:00    
sftp> put putme.txt renamed_putme.txt
Uploading putme.txt to /storage/emulated/0/Temp/renamed_putme.txt
putme.txt                                                                                   100%   35     6.4KB/s   00:00    
sftp> ls
getme.txt            renamed_putme.txt    test.txt             
sftp> lls
putme.txt  renamed_getme.txt
sftp> ^D