How to Use SSH in Termux to Connect to a Server
How to Use SSH in Termux to Connect to a Server
Managing remote servers from your Android phone used to require clunky, ad-filled apps. With Termux, you get a authentic Linux terminal environment right in your pocket. Whether you need to manage a remote VPS, restart a web server, or push code on the go, learning how to use SSH in Termux to connect to a server gives you complete control from anywhere.
In this comprehensive guide, you will learn how to set up the OpenSSH client, establish password-backed connections, generate secure SSH keys, transfer keys to remote host machines, and troubleshoot the most annoying SSH connection errors in Termux.
Quick Summary: Connecting to a Server via Termux SSH
- Open Termux and update packages:
pkg update && pkg upgrade - Install OpenSSH:
pkg install openssh - Connect to your remote server:
ssh username@server_ip -p port_number - Enter your remote user password when prompted.
What is Termux SSH and How Does It Work?
SSH (Secure Shell) is a cryptographic network protocol used to operate network services securely over an unsecured network. The standard client-server architecture allows you to execute command-line instructions on a remote machine directly from your phone screen.
In Termux, SSH functionality is provided through the openssh package. Once installed, Termux acts as a full-featured SSH client. You can connect to remote Ubuntu, Debian, CentOS, or custom Linux servers seamlessly.
It is important to understand port usage when working with termux ssh:
- Port 22: The standard default SSH port used by most remote Linux servers and Virtual Private Servers (VPS).
- Port 8022: The default SSH port used when you run an SSH server inside Termux on Android (since non-rooted Android apps cannot bind to privileged ports below 1024).
Prerequisites Before Connecting
Before launching your first remote session, you must prepare your Termux environment. Android battery optimizations and outdated repository indexes can disrupt your connections if not handled first.
Step 1: Update Termux Packages
Open Termux and run the following command to update package lists and upgrade installed tools to their latest versions:
pkg update && pkg upgrade -y
Step 2: Install OpenSSH Client
By default, Termux does not ship with SSH tools pre-installed. You need to install the OpenSSH suite:
pkg install openssh -y
To verify that OpenSSH installed correctly, check its version:
ssh -V
You should see output indicating OpenSSL and OpenSSH version numbers, confirming the client is ready.
How to Use SSH in Termux to Connect to a Server (Password Method)
The simplest way to connect to a remote server is using standard password authentication. Follow these exact steps:
Step 1: Execute the SSH Command
Type the ssh command followed by your server's username and IP address (or domain name):
ssh root@192.168.1.100
If your remote server listens on a non-standard port (anything other than port 22), specify the port using the -p flag:
ssh -p 2222 user@example.com
Step 2: Accept the Authenticity Prompt (First-Time Connection)
When connecting to a server for the first time, SSH will display a fingerprint prompt asking if you trust the host. Type yes and press Enter.
Expected Terminal Output
Here is what your Termux screen will look like during a successful initial connection attempt:
$ ssh admin@203.0.113.15
The authenticity of host '203.0.113.15 (203.0.113.15)' can't be established.
ED25519 key fingerprint is SHA256:uN89xL1qZ3P4kRt0vW7mY6sA5bC8dE2fG0hI1jK2lM3.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '203.0.113.15' (ED25519) to the list of known hosts.
admin@203.0.113.15's password:
Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 5.15.0-88-generic x86_64)
admin@remote-server:~$
Once your password is verified, your prompt changes from your local Termux shell to the remote server shell. You are now logged in and controlling your remote server directly from Android.
Termux SSH Key Generate Tutorial (Passwordless Authentication)
Typing long, complex passwords on a mobile touch keyboard is tedious and prone to typos. More importantly, password authentication is vulnerable to brute-force login attacks. Setting up SSH keys allows you to log in instantly and securely without typing a password.
Follow this step-by-step termux ssh key generate tutorial to create modern cryptographic keys right on your device.
Step 1: Generate an SSH Key Pair in Termux
We recommend using ED25519 keys because they provide higher security and faster performance compared to legacy RSA keys. Run this command in Termux:
ssh-keygen -t ed25519 -C "termux-mobile-session"
You will see prompts asking where to save the key and whether to enter a passphrase:
Generating public/private ed25519 key pair.
Enter file in which to save the key (/data/data/com.termux/files/home/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
- Press Enter to accept the default file location (`/data/data/com.termux/files/home/.ssh/id_ed25519`).
- (Optional) Enter a passphrase to encrypt your private key locally, or press Enter twice to leave it blank for instant logins.
Your terminal will output visual randomart confirming the key pair generation:
Your identification has been saved in /data/data/com.termux/files/home/.ssh/id_ed25519
Your public key has been saved in /data/data/com.termux/files/home/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:7mX9K2pQ8vR4wL1sN0jF3bV5cG6hY7uZ8iA9kB0lM1o termux-mobile-session
The key's randomart image is:
+--[ED25519 256]--+
| .o.. |
| . o. |
| . = . |
| . = = |
| o = S |
| . * B + |
| . = X * |
| . = o O . |
| +.o===.E |
+-----------------+
Step 2: Copy Your Public Key to the Remote Server
Now, you must transfer your public key (`id_ed25519.pub`) to the target server's `~/.ssh/authorized_keys` file. OpenSSH provides a built-in command for this called `ssh-copy-id`:
ssh-copy-id -p 22 admin@203.0.113.15
Enter your remote account password one final time when prompted. `ssh-copy-id` will append your public key automatically and set correct file permissions on the target server.
ssh-copy-id is Unavailable):If the remote server blocks `ssh-copy-id`, run this single command chain in Termux to append your key manually:
cat ~/.ssh/id_ed25519.pub | ssh -p 22 admin@203.0.113.15 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
Step 3: Connect Using Your SSH Key
Now, test your SSH key connection. Run the standard SSH command:
ssh admin@203.0.113.15
You will be logged in immediately without a password prompt!
Password Authentication vs SSH Key Authentication
| Feature | Password Authentication | SSH Key Authentication |
|---|---|---|
| Security Level | Low / Moderate (Vulnerable to brute-force) | Extremely High (Cryptographically secure) |
| Convenience | Low (Requires typing password every time) | High (Instant automatic login) |
| Mobile Typing Effort | High (Prone to typos on touchscreens) | Zero (Automated handshake) |
| Server Hardening Compatibility | Poor (Best practice is disabling password login) | Excellent (Required by secured Linux servers) |
How to Create SSH Shortcuts using the Termux Config File
Memorizing IP addresses, custom ports, and username strings gets tiring quickly. You can set up short aliases in Termux using an SSH config file.
Step 1: Create or Edit the SSH Config File
Open the SSH configuration file in Termux using Nano or Vim text editor:
nano ~/.ssh/config
Step 2: Add Host Entry
Add a structured block like this for your server:
Host myvps
HostName 203.0.113.15
User admin
Port 22
IdentityFile ~/.ssh/id_ed25519
Save and exit (in Nano, press Ctrl + O, Enter, then Ctrl + X).
Step 3: Connect Using Your Alias
Now, instead of typing full IP addresses and ports, connect by typing your alias:
ssh myvps
Termux instantly connects using the saved username, port, and SSH key configuration.
Common Termux SSH Errors and Exact Fixes
Even simple SSH connections can hit obstacles due to mobile permissions, network changes, or server restrictions. Here are the common errors you might encounter and how to solve them.
Error 1: ssh: connect to host x.x.x.x port 22: Connection refused
Cause: Either the remote SSH daemon service is stopped, or a firewall (like UFW or iptables) is blocking connection attempts on port 22.
Fix:
- Verify the server is powered on and connected to the network.
- Check if SSH listens on a custom port instead of default 22. Try connecting with your custom port:
ssh -p YOUR_PORT user@host. - If you own the server, log into your server management portal or console and allow SSH traffic:
sudo ufw allow 22/tcp.
Error 2: Permission denied (publickey,password)
Cause: You entered an incorrect password, specified an incorrect username, or the server rejects public key authentication.
Fix:
- Double-check your username syntax:
ssh correct_user@ip_address. - Ensure key permissions on your local device and remote server are strictly set. On the remote server, run:
chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys - Specify your private key explicitly:
ssh -i ~/.ssh/id_ed25519 user@host.
Error 3: WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
Cause: The remote server was reinstalled, assigned a new host key, or a potential Man-in-the-Middle (MitM) security event is occurring.
Fix: If you intentionally reinstalled your server OS, clear the outdated host key fingerprint saved in your local known_hosts file in Termux:
ssh-keygen -R 203.0.113.15
After removing the old key, reconnect using ssh user@203.0.113.15 and type yes to register the new key fingerprint.
Error 4: ssh: command not found
Cause: The OpenSSH package is missing from your Termux installation.
Fix: Run pkg install openssh -y in your local Termux shell to restore the command binaries.
Best Practices for SSH Connections on Android
- Keep Connections Alive: Android OS power management often kills inactive background apps. To stop Termux from disconnecting mid-session, swipe down your Android notification shade, expand the Termux panel, and tap Acquire Wakelock.
- Use WireGuard VPN for Remote Access: Directly exposing port 22 of your home devices or local lab over public internet introduces security risks. Set up an encrypted tunnel via WireGuard or Tailscale to securely connect to private server addresses.
- Disable Root and Password Authentication: Hardened server environments should disable direct SSH root logins and force key-based access. Edit `/etc/ssh/sshd_config` on your server and set `PasswordAuthentication no` and `PermitRootLogin no`.
- Backup SSH Keys Securely: If you uninstall Termux without backing up your private key (`~/.ssh/id_ed25519`), you will lose access to servers that rely exclusively on key authentication.
Frequently Asked Questions
Does Termux require root access to use SSH?
No, Termux does not require root access to connect to remote SSH servers or run a local SSH client. Root is only required if you attempt to bind services to privileged system ports below 1024 directly on Android.
How do I close an active SSH session in Termux?
To end an active remote session and return to your local Android Termux prompt, type exit and press Enter, or press Ctrl + D.
Can I transfer files between my phone and server using SSH in Termux?
Yes. Because OpenSSH includes SCP and SFTP tools, you can transfer files directly from Termux. For example, to upload a local file to your server, run: scp -P 22 file.txt user@remote_host:/path/to/destination/.
Conclusion and Next Steps
Using SSH in Termux to connect to a server turns your Android mobile device into a flexible sysadmin pocket toolkit. By installing OpenSSH, generating ED25519 keys, and setting up clean configuration aliases, you can securely access remote Linux environments in seconds without typing passwords on a touchscreen.
Join the conversation