DEV Community

Mohammed Chami
Mohammed Chami

Posted on

Connecting to Your Android Device via SSH from Linux: A Complete Termux Guide

Ever wanted to access your Android phone's terminal from your Linux desktop? Whether you're transferring files, running commands remotely, or just exploring your device's capabilities, SSH access opens up a world of possibilities. However, getting that initial connection working can be frustrating when you're met with cryptic "Access Denied" errors.

This guide will walk you through setting up a reliable SSH connection between your Pop!_OS machine and Android device using Termux, troubleshooting common issues, and ensuring secure access.

Why SSH to Android?

SSH access to your Android device transforms how you interact with it. You can transfer files without cables, run automated scripts, access your phone's filesystem, and even use it as a development environment. It's particularly useful for developers, system administrators, and anyone who prefers command-line efficiency.

Setting Up Termux for SSH Access

Installing and Configuring Termux

Termux stands out as the most capable terminal emulator for Android. Unlike other solutions, it provides a genuine Linux environment with package management and full SSH server capabilities.

Download Termux from F-Droid rather than Google Play. The F-Droid version receives more frequent updates and has fewer restrictions.

Once installed, open Termux and update the package repositories:

pkg update && pkg upgrade
Enter fullscreen mode Exit fullscreen mode

This ensures you have the latest security patches and features.

Installing OpenSSH Server

Install the OpenSSH server package:

pkg install openssh
Enter fullscreen mode Exit fullscreen mode

Termux's OpenSSH implementation is full-featured and includes all the security features you'd expect from a modern SSH server.

Starting the SSH Service

Launch the SSH daemon:

sshd
Enter fullscreen mode Exit fullscreen mode

The SSH server will start immediately and begin listening for connections. Unlike traditional Linux systems, Termux doesn't use systemd, so the service won't automatically restart on reboot.

Finding Your Android's Network Address

Determine your Android device's IP address:

ip addr show wlan0
Enter fullscreen mode Exit fullscreen mode

Look for the inet line under the wlan0 interface. You'll see something like 192.168.1.155/24 - the IP address is the part before the slash.

Understanding Termux SSH Configuration

Port Configuration

Here's where many people get stuck: Termux uses port 8022 for SSH, not the standard port 22. This is because Android's security model prevents non-system apps from binding to privileged ports (those below 1024).

Your SSH command will look like this:

ssh -p 8022 [username]@[ip_address]
Enter fullscreen mode Exit fullscreen mode

Username Discovery

Termux uses Android's internal username system. Find your username by running:

whoami
Enter fullscreen mode Exit fullscreen mode

You'll see something like u0_a123 - this is your Android user ID that you'll need for SSH connections.

Configuring Authentication

The Password Authentication Challenge

By default, Termux disables password authentication for security reasons. Most SSH "Access Denied" errors stem from this configuration.

To enable password authentication for testing purposes, first set a password:

passwd
Enter fullscreen mode Exit fullscreen mode

Then edit the SSH configuration:

nano $PREFIX/etc/ssh/sshd_config
Enter fullscreen mode Exit fullscreen mode

Find the line containing PasswordAuthentication and change it to:

PasswordAuthentication yes
Enter fullscreen mode Exit fullscreen mode

If the line doesn't exist, add it to the file.

Restart the SSH service:

pkill sshd && sshd
Enter fullscreen mode Exit fullscreen mode

Testing the Connection

Now attempt to connect from your Pop!_OS machine:

ssh -p 8022 u0_a123@192.168.1.155
Enter fullscreen mode Exit fullscreen mode

Replace u0_a123 with your actual username and 192.168.1.155 with your Android's IP address.

Setting Up Key-Based Authentication

Why Use SSH Keys?

Password authentication works for testing, but key-based authentication is more secure and convenient. Once set up, you won't need to enter passwords for each connection.

Generating SSH Keys

On your Pop!_OS machine, generate an SSH key pair if you don't already have one:

ssh-keygen -t ed25519 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

The Ed25519 algorithm provides excellent security with smaller key sizes compared to RSA.

Copying Keys to Android

The easiest method is using ssh-copy-id:

ssh-copy-id -p 8022 u0_a123@192.168.1.155
Enter fullscreen mode Exit fullscreen mode

This command automatically copies your public key to the Android device's authorized keys file.

If ssh-copy-id fails, you can manually copy the key:

cat ~/.ssh/id_ed25519.pub | ssh -p 8022 u0_a123@192.168.1.155 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'
Enter fullscreen mode Exit fullscreen mode

Disabling Password Authentication

For better security, disable password authentication once key-based auth is working:

nano $PREFIX/etc/ssh/sshd_config
Enter fullscreen mode Exit fullscreen mode

Change or add:

PasswordAuthentication no
Enter fullscreen mode Exit fullscreen mode

Restart the SSH service:

pkill sshd && sshd
Enter fullscreen mode Exit fullscreen mode

Troubleshooting Common Issues

Connection Refused Errors

If you get "Connection refused," the SSH service isn't running or isn't listening on the expected port. Check if the service is running:

pgrep sshd
Enter fullscreen mode Exit fullscreen mode

If no process ID appears, start the service again:

sshd
Enter fullscreen mode Exit fullscreen mode

Network Connectivity Problems

Ensure both devices are on the same WiFi network. Test basic connectivity:

ping 192.168.1.155
Enter fullscreen mode Exit fullscreen mode

If ping fails, you have a network issue rather than an SSH problem.

Permission Denied with Keys

If key authentication fails, check the key permissions on both devices:

# On Pop!_OS
ls -la ~/.ssh/
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

On Android (via Termux):

chmod 700 ~/.ssh/
chmod 600 ~/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

Automating SSH Server Startup

Creating a Startup Script

Since Termux doesn't automatically start SSH on boot, create a simple script:

nano ~/start_ssh.sh
Enter fullscreen mode Exit fullscreen mode

Add:

#!/data/data/com.termux/files/usr/bin/bash
sshd
echo "SSH server started on port 8022"
Enter fullscreen mode Exit fullscreen mode

Make it executable:

chmod +x ~/start_ssh.sh
Enter fullscreen mode Exit fullscreen mode

Using Termux:Boot

For automatic startup, install the Termux:Boot add-on from F-Droid. Create a startup script in the required directory:

mkdir -p ~/.termux/boot/
echo 'sshd' > ~/.termux/boot/start_ssh
chmod +x ~/.termux/boot/start_ssh
Enter fullscreen mode Exit fullscreen mode

Advanced Configuration

Custom SSH Configuration

Create an SSH config file on your Pop!_OS machine for easier connections:

nano ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

Add:

Host android-phone
    HostName 192.168.1.155
    Port 8022
    User u0_a123
    IdentityFile ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

Now you can connect simply with:

ssh android-phone
Enter fullscreen mode Exit fullscreen mode

File Transfer Operations

Use SCP for file transfers:

scp -P 8022 file.txt u0_a123@192.168.1.155:~/
Enter fullscreen mode Exit fullscreen mode

Or use rsync for more advanced synchronization:

rsync -avz -e "ssh -p 8022" ~/Documents/ u0_a123@192.168.1.155:~/backup/
Enter fullscreen mode Exit fullscreen mode

Security Considerations

Firewall Configuration

Consider enabling UFW on your Pop!_OS machine and configuring appropriate rules:

sudo ufw allow from 192.168.1.0/24 to any port 22
Enter fullscreen mode Exit fullscreen mode

Regular Security Updates

Keep both systems updated:

# Pop!_OS
sudo apt update && sudo apt upgrade

# Termux
pkg update && pkg upgrade
Enter fullscreen mode Exit fullscreen mode

Conclusion

Setting up SSH access between Pop!_OS and Android via Termux opens up powerful possibilities for file management, remote administration, and development workflows. The key is understanding Termux's unique port configuration and authentication requirements.

Remember that the SSH server in Termux won't survive app kills or device reboots, so you'll need to restart it periodically. Consider this when planning automated workflows or remote access scenarios.

With proper configuration, you'll have a secure, efficient way to access your Android device's terminal from your Linux desktop, making cross-device development and file management significantly easier.

Top comments (1)

Collapse
 
ignacio_agull_33bb8bd0fd profile image
Ignacio Agulló

Thanks a lot for your post, Mohammed Chami.
Termux is an incredibly useful app that allows to use the power of BASH shell on Android, complete with the ability to install packages.
I was trying to transfer files automatically between GNU/Linux and Android by using rsync over SSH, but I was hitting a wall. Apps such as SshDaemon or SimpleSSHD would not suffice, for rsync needs to to be installed in both sides in order to work.
Your post shows that Termux allows to solve the whole problem by installing packages such as openssh, nmap and rsync. Now I can use rsync!

Still, Termux+OpenSSH still is not enough when it comes to copy music. I will explain why:
For a start, let's say that you want to access five directories such as DCIM, Download, Movies, Music, Pictures. You have two ways to access these directories:

  • From Android directory /storage/emulated/0/ - This is the directory you see when you connect your Android device to your computer through MTP, and here you can find these five directories, DCIM, Download, Movies, Music, Pictures.
  • From Termux directory /data/data/com.termux/files/home/ - This is the home directory for the Termux user and for SSH. Inside this directory, you find the directory 'storage', and there you have five directory links to these directories, dcim, download, movies, music, pictures. There is a sixth directory link, 'shared', that leads to /storage/emulated/0/

So you if you are using SSH and you want to use scp and rsync to copy files to the Pictures directory, you can point at it in three different ways:
u0_a123@192.168.1.155:/storage/emulated/0/Pictures
u0_a123@192.168.1.155:storage/pictures
u0_a123@192.168.1.155:storage/shared/Pictures
In all cases, files will be copied.
But no matter which one of these three addresses you choose, a problem happens when apps search for music files... and it all depends on the permission for 'Files and multimedia content':

  1. Permission set to 'allow management for all files' means that apps can find the music files. Examples: VLC.
  2. Permission set to 'access multimedia files only' means that apps cannot find the music files. Examples: Odyssey, Apollo, Mucke and others. This is the problem: music files copied through MTP are found by all aps, music files copied through SSH are only found by apps with the first permission. The cause is unknown. According to Termux, the permissions are the same for both cases, drwxrwx--- for directories, and -rw-rw---- for files. This happens even when using the -p option from rsync to preserve permissions. Maybe it could be fixed by using the command chmod to add permissions for others, r-x for directories and r-- for files, but Termux cannot add those permissions in a non-rooted Android device. This problem makes SSH unfit for copying music files to Android devices.