Termux-app: Feature Request: Add the ability to run Termux commands via. ADB without root.

Created on 18 Nov 2018  路  14Comments  路  Source: termux/termux-app

Currently, in order to run a command, you have to do something like this:

adb shell am start -n com.termux/.app.TermuxActivity
adb shell input text "[command here]"
adb shell input keyevent 113 && adb shell input keyevent 66

which, in my opinion, is a little hackey... There are a lot of things that could stop this script, like the screen being locked or a Termux session already being open with text typed in.

I propose that a new Activity/Service is added (or an extension of com.termux/.app.TermuxService to allow use by ADB). You could possibly even allow another app (with user granted permission) to run a command!

This would be BEYOND useful (and a little cool, too), like being able to start up sshd and setup port forwarding completely from the computer! You wouldn't even have to unlock the screen, assuming that you have already accepted the computer's RSA Fingerprint on your phone.


If something to allow this already exists, just let me know and I can change the title and close the "Issue".

Most helpful comment

I second this, it would be very convenient if we could pass commands to Termux via a system broadcast.

All 14 comments

I second this, it would be very convenient if we could pass commands to Termux via a system broadcast.

ADB port forwarding can be used to access termux remotely, via sshd or telnetd.

  1. Using sshd

Prepare termux for sshd using public key or password. Detailed steps for setting up sshd can be found on Termux Wiki.

By default, termux sshd listens on port 8022. Create port forwarding to port 8022

$ adb forward tcp:8022 tcp:8022

After those steps, termux can be accessed via ssh into 127.0.0.1 on port 8022

$ ssh -p 8022 [email protected]
  1. Using telnetd

Termux comes with busybox, which includes a telnet daemon. The daemon can be set up to allow remote access.

Inside termux terminal, run the following command to start telnetd on port 8023

$ telnetd -b 127.0.0.1:8023

On the computer, set up adb port forwarding

$ adb forward tcp:8023 tcp:8023

After port forwarding has been created, telnet client can be used to access termux.

$ telnet 127.0.0.1 8023

Duplicate of issue 77

@alive4ever I am aware you can use sshd, however, that is not what I mentioned. What if you want to start sshd from the PC? I said this in the first comment, please read the entire thing (not to be rude... I realize it may have sounded that way...).

I agree with JacobTDC, the ADB port-forwarding solution assumes SSHD to already be running. What I am looking for is to start SSHD by sending an intent (in my case, my screen broke, SSHD was not running, and I need to start it to retrieve some data/execute some scripts. Sure some of it can be done via pure adb, but termux offers better tools for my needs).

And imagine the possibilities of 3rd Party Plugins! :O

What I am looking for is to start SSHD by sending an intent

Variants of what could be done for this:

  1. Termux:Intents plugin. It may implement APIs for opening Termux session with executed arbitrary command and/or executing arbitrary command by third party application without opening Termux session and somehow returning output of this command.
  1. A functionality mentioned above will be integrated into Termux and it will be possible to enable/disable it via checkbox in settings.

These arbitrary intents/broadcasts should be disabled by default.

And imagine the possibilities of 3rd Party Plugins! :O

And how these plugins will interact with Termux ? They won't be able to read/write files under $PREFIX or $HOME anyway. To share user id between applications, the same signature is required.

@xeffyr I'm having difficulty knowing whether you are in support of or against this idea. XD
And, other apps already can view and edit Termux files. Try termux-open. It uses an intent to tell Termux to allow an app temporary access.
The Termux:Intents plugin sounds like a good idea, as well.

I'm having difficulty knowing whether you are in support of or against this idea.

I support this idea. But it should be properly implemented as not all users want to have $HOME or $PREFIX freely accessible by third party apps.

And, other apps already can view and edit Termux files. Try termux-open.

I know about termux-open. But it seems doesn't support opening file for editing, only viewing/sending:

$ termux-open -h
Usage: termux-open [options] path-or-url
Open a file or URL in an external app.
  --send               if the file should be shared for sending
  --view               if the file should be shared for viewing (default)
  --chooser            if an app chooser should always be shown
  --content-type type  specify the content type to use

For example, when trying to save text file in QuickEdit, it shows error "write failed: EBADF (Bad file descriptor)". So file can be saved only on /sdcard but not in $HOME.

Also, the termux-open provides access to the single file (not directory). So, what if a third-party plugin should unpack multiple files to directory in $HOME ? Or what if someone will create a Termux-based "IDE" that should have free access to Termux private directories like $HOME or $TMPDIR for working with executables and other files ?

The main problem in implementing third-party plugins for Termux is that Android applications having different user id are sandboxed and can't interact with each other on file system level. This was possible on old Android versions by just doing chmod 777 on specific private app directories, but after SELinux was introduced, this became impossible.

Hmm.. I seem to remember seeing an intent somewhere that allowed for file editing... I might have imagined it...

For user convenience an option could be added "to get into termux" from app's preference view ?

I suppose if android:debuggable="true" was compiled into termux, you could do: adb run-as com.termux and export PATH and LD_LIBRARY_PATH accordingly.

And how these plugins will interact with Termux ? They won't be able to read/write files under $PREFIX or $HOME anyway. To share user id between applications, the same signature is required.

@xeffyr: an AIDL interface paired with a custom permission would do the job.

Apps willing to invoke termux will need to include the relevant permission in their manifest and call the termux service via a binder instance. Since the service will be part of the termux apk it will have all permissions to rw in internal files dir et al.

I guess the harder part is to think about a good - reliable api to export to other applications. In principle AIDL is able to pass all kind of objects between proccess boundaries but, due to google restrictions, some objects cannot be consumed by the clients.

All you need to do is what @AbdullahM0hamed said. I repackaged a version of it with that tweak to its manifest a while back and have been running it like that ever since. No service or plugin necessary.

On my Pixel 4 XL, the following variables are set in a termux session normally (found by running env):
SHELL=/data/data/com.termux/files/usr/bin/bash PREFIX=/data/data/com.termux/files/usr PWD=/data/data/com.termux/files/home EXTERNAL_STORAGE=/sdcard LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so HOME=/data/data/com.termux/files/home LANG=en_US.UTF-8 TMPDIR=/data/data/com.termux/files/usr/tmp ANDROID_DATA=/data TERM=xterm-256color SHLVL=1 ANDROID_ROOT=/system LD_LIBRARY_PATH=/data/data/com.termux/files/usr/lib PS1=\$ PATH=/data/data/com.termux/files/home/bin:/data/data/com.termux/files/usr/bin:/data/data/com.termux/files/usr/bin/applets _=/data/data/com.termux/files/usr/bin/env

So by running env > termux-source and then from the adb session running . /path/to/termux-source I get a proper session. The PS1 variable is missing its trailing space, though, so the following sed statement can fix that:

sed "s/^PS1=/PS1='/;s/ $/ '/" < termux-source_orig > termux-source_new

Before running that make sure your only variable with a trailing space is indeed PS1.

You can also make all those variables get exported by doing:

sed 's/^/export //'
Was this page helpful?
0 / 5 - 0 ratings

Related issues

flipflop97 picture flipflop97  路  3Comments

AnkurJagani picture AnkurJagani  路  3Comments

joakim-noah picture joakim-noah  路  5Comments

bitnetwork picture bitnetwork  路  3Comments

TheNickVendetta picture TheNickVendetta  路  4Comments