Permanent access to Termux via ssh tunnel

Mutt

Professional
Messages
1,057
Reputation
7
Reaction score
597
Points
113
6fd1e179-3a56-4d45-94d5-2b33e1479ba1.png

In case you need constant access to Termux via ssh, but there is no direct access to your smartphone or tablet over the network, you can use a home server or service to create ssh tunnels.

Preparing Termux
If Termux is just installed, do pkg updateto update the utilities and package list.

To deploy you need:
  • openssh - to access Termux;
  • autossh - to maintain the tunnel;
  • termux-services - for autossh startup.

Install packages at once:
Code:
pkg install openssh autossh termux-services

Creating an ssh server in Termux
First, set a password to access the Termux ssh server:
Code:
passwd

Let's write the port for the server in the file ~/../usr/etc/ssh/sshd_config :
Code:
Port 2222
PrintMotd no
PasswordAuthentication yes
PubkeyAcceptedKeyTypes + ssh-dss
Subsystem sftp /data/data/com.termux/files/usr/libexec/sftp-server

Restart the ssh server:
Code:
sv down sshd
sv up sshd

To start the server at device startup, do:
Code:
sv-enable sshd

Let's move on to setting up the connection.

Creating an ssh tunnel
Let's say there is an ssh server for example myserver. This is either a home server or a service that provides access to the ssh server. As an example, Free Linux shell server.

In order not to prescribe the hostname, port, etc. each time when connecting to the ssh server to create a tunnel, we will write the config ~/.ssh/config.
Example:

Code:
Host srv_rev
User username
Port 22
HostName myserver.com
RemoteForward 3022 localhost: 2222
ServerAliveInterval 30
ServerAliveCountMax 1
ExitOnForwardFailure yes
How to create a key for passwordless login via ssh is read in a separate article.

Let's check if we have configured it correctly: the shell prompt should be displayed, otherwise you will have to enter the password every time you create the tunnel. In the case of the service, it will be impossible to enter the password.
Code:
ssh srv_rev

If it asks for a password, create a key to enter.

Tunnel connection service
Since we indicated in the config in the previous step RemoteForward, it remains only to connect to the server using the configuration parameters.

We'll use termux-servicesit so that we don't have to enter the connection command every time,. The utility launches applications immediately when creating a Termux session.

It is very simple to add services: create a folder with the name of the service, inside a file runwith launch rights and a shebang that points to the shell /bin/sh.

So, step by step:
  • create a foldermkdir ~/../usr/var/service/autossh/
  • create a fileecho '#!/bin/sh' > ~/../usr/var/service/autossh/run echo 'exec autossh -M 20022 -NT -f srv_rev' >> ~/../usr/var/service/autossh/run
  • fix the shebang for Termuxtermux-fix-shebang ~/../usr/var/service/autossh/run
  • we give the rights to performancechmod +x ~/../usr/var/service/autossh/run
  • start the servicesv up autossh
After that, an ssh tunnel will be established to access Termux from a remote server, if configured correctly.

Connecting to Termux
We connect to the remote server via ssh.
Then we connect to ssh in Termux, which is tunneled to port 3022:
Code:
ssh 127.0.0.1 -p 3022

What did you get in the end
Now, after launching Termux on the remote server, access through the tunnel to the terminal on the smartphone will appear. Since the connection on the smartphone can be interrupted from time to time for various reasons, autossh will help to re-establish the connection to the remote ssh server and restore the tunnel. It is desirable that port 20022 on the remote machine be open to allow autossh to react quickly when the connection is broken.
 
Top