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:
- Install Termux, a terminal emulator, on your Android phone. Check its local IP address with
ifconfig(we will assume192.168.6.140for this example) and username withwhoami(for example,u0_a999). Set a password for your user usingpasswd. - In the Termux shell, install OpenSSH.
- Ensure the
sshddaemon is running. Typingsshdshould start it. - On your Linux laptop, ensure that an OpenSSH client is installed. This should provide
sftpout of the box. - Connect to your phone with
sftp. For simplicity, run this from the directory where you want to save files:
$ pkg install openssh
$ sftp -P 8022 -o "ConnectTimeout=5" u0_a999@192.168.6.140
Edit the username and IP address in the command as required.
- Use standard Bash-like commands to navigate the remote filesystem. For your local filesystem, prefix standard commands with
lfor “local”:lsbecomeslls, andcdbecomeslcd. - Use
getto pull a file from the phone to your laptop, andputto 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:
- If you have an SSH key pair on your laptop, all well and good. If not, generate one with
ssh-keygen. - Copy your public key to the phone:
orssh-copy-id -p 8022 -i ~/.ssh/id_rsa.pub u0_a999@192.168.6.140sshin and paste the public key into~/.ssh/authorized_keyson the phone.
Then you should be able to ssh in and use sftp to transfer files without typing a password.
- If you have any problems connecting, check that you have
600permissions (rw) on your~/.ssh/authorized_keysfile on the phone, and700permissions (rwx) on the parent directory~/.ssh. - Also check that the ssh daemon is running:
Kill it if necessary withps aux | grep sshdpkill sshdand restart it withsshd.
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