Posted on August 21, 2025
Category: Technology
Tags: SSH, Termux, Ubuntu, proot-distro, Android, cybersecurity, remote access, SSH keys
Views: 541
Hey there! I recently set out to connect my PC to an Ubuntu environment running inside Termux on my Android device using SSH. It’s a fantastic way to access a full Linux system on the go, but it took some troubleshooting to get it right, especially when my initial SSH attempts with a custom key failed. Let me walk you through how I set it up, tackled the connection issue, and addressed security concerns about passwords, all while keeping the connection secure within my local Wi-Fi network.
My goal was to securely SSH from my PC to the Ubuntu system I’d installed via proot-distro in Termux, ensuring only my PC could connect over my home Wi-Fi. Since proot-distro lacks true root access to bind to low ports like 22, I used Termux’s built-in SSH server, which runs on port 8022 by default.
Here’s what I did in Termux to get started:
- Updated packages to keep everything fresh:
pkg update && pkg upgrade
- Installed OpenSSH:
pkg install openssh
- Started the SSH server:
sshd
To ensure every SSH connection lands in Ubuntu as my user, myuser, I edited Termux’s ~/.bash_profile:
if [ -z "$PROOT_DISTRO_NAME" ]; then
proot-distro login ubuntu --user myuser
fi
This made sure that when I connected via SSH, I’d be dropped directly into Ubuntu’s environment as myuser, no extra steps required.
To keep things secure, I opted for public key authentication over passwords to restrict access to my PC. On my PC, I generated a custom SSH key pair using the ed25519 algorithm, which is modern and secure:
ssh-keygen -t ed25519 -f ~/.ssh/termux_key -C "[email protected]"
This created a private key (termux_key) and a public key (termux_key.pub) in my PC’s ~/.ssh/ directory. I manually copied the public key to Termux by displaying it on my PC:
cat ~/.ssh/termux_key.pub
Then, in Termux, I pasted it into ~/.ssh/authorized_keys:
mkdir -p ~/.ssh && chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
I pasted the key (starting with ssh-ed25519), saved the file, and set the correct permissions. Termux’s SSH configuration ($PREFIX/etc/ssh/sshd_config):
Port 8022
PubkeyAuthentication yes
PasswordAuthentication no
AuthorizedKeysFile .ssh/authorized_keys
After restarting the SSH server (pkill sshd && sshd), I connected from my PC:
ssh -i ~/.ssh/termux_key -p 8022 your_termux_username@android_ip
This ensured a secure, one-to-one connection within my Wi-Fi network.
My first attempt to connect using the custom key didn’t go smoothly—I couldn’t establish the SSH connection, and it kept failing. The issue was tied to the custom key setup, so I rolled up my sleeves and dove into troubleshooting.
I started by verifying the key pair on my PC, checking that both termux_key and termux_key.pub existed in ~/.ssh/ with:
ls -l ~/.ssh/termux_key*
If they were missing or corrupted, I’d regenerate them with ssh-keygen. Next, I confirmed the public key was correctly pasted into Termux’s ~/.ssh/authorized_keys by checking:
cat ~/.ssh/authorized_keys
It needed to match the PC’s public key (starting with ssh-ed25519). Permissions were a common issue, so I ensured they were correct in Termux:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 755 ~
If permissions were too permissive, SSH would reject the key. I also checked the SSH server configuration in $PREFIX/etc/ssh/sshd_config, ensuring PubkeyAuthentication yes and AuthorizedKeysFile .ssh/authorized_keys were set. To confirm sshd was running, I ran:
ps aux | grep sshd
netstat -tuln | grep 8022
If it wasn’t running, I restarted it with sshd. For more clues, I used verbose mode on my PC:
ssh -v -i ~/.ssh/termux_key -p 8022 your_termux_username@android_ip
This showed details like “Offering public key” or “Permission denied (publickey),” which helped identify issues like a mismatched key or server rejection. If the key wasn’t working, I recopied it to Termux:
scp -P 8022 ~/.ssh/termux_key.pub your_termux_username@android_ip:~/
In Termux, I appended it to authorized_keys:
cat ~/termux_key.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
rm ~/termux_key.pub
I also verified network connectivity by pinging the Android IP from my PC (ping android_ip) and testing port 8022:
nc -zv android_ip 8022
Android’s firewall or battery optimizations could interfere, so I checked that Termux had network permissions in Android Settings and disabled battery optimization (Settings > Apps > Termux > Battery > Allow background activity). If all else failed, I considered reinstalling OpenSSH:
pkg uninstall openssh && pkg install openssh
These steps got my SSH connection up and running, landing me in Ubuntu as myuser.
I was initially confused about why I needed to set a password for the Termux user with passwd. It turned out to be useful for initial SSH testing before setting up keys or for other Termux tools that might require authentication. Since I disabled password authentication in sshd_config (PasswordAuthentication no) and used my custom key, the Termux password wasn’t used for SSH, so it didn’t pose a security risk. If I had kept password authentication enabled, I’d need a strong, unique password to prevent brute-force attacks.
myuser PasswordI also set a password for myuser in Ubuntu (via adduser myuser) and wondered about its security implications. Since SSH authentication happens at the Termux level with my custom key, the myuser password is only relevant within Ubuntu—for example, for sudo commands. Proot-distro’s sandboxed environment limits the risk, as a compromised myuser password wouldn’t give access to my Android device’s root filesystem. To stay safe, I used a strong, unique password for myuser, limited its privileges (only granting sudo when needed), and ensured file permissions were secure:
chmod 700 /home/myuser
chmod 600 /home/myuser/*
This kept the Ubuntu environment locked down without impacting the SSH connection.
Setting up SSH from my PC to Ubuntu on Termux was a bit of a journey, but it’s now a secure, seamless setup. Using a custom SSH key, restricting access to my PC’s IP, and automatically logging into Ubuntu as myuser made it both secure and convenient. Troubleshooting the initial connection failure required carefully checking keys, permissions, and network settings, but it paid off with a reliable connection. The Termux and Ubuntu passwords are low-risk in my key-based setup, as long as I keep them strong and unique. This setup is perfect for anyone wanting to run a Linux environment on their Android device and access it securely from their PC—just keep those keys safe and double-check your configurations!
Disclaimer: This blog post was created with assistance from Grok 3, an AI developed by xAI, under my direct supervision and guidance to ensure accuracy and alignment with my vision for the content.