Termux, Termux-api, SSH & SCRCPY

Android  
6 min read
 
Termux, Termux-api, SSH & SCRCPY

Icon   Icon   Icon

What is Termux?

Termux is an Android terminal emulator and Linux environment app that allows you to run command-line programs and scripts on your Android device. It provides a full Linux environment with package management using APT, allowing you to install and use various tools and utilities on your Android device.

What is Termux-api?

Termux-api is a python module to access the termux-api This provides a way to get native access in Python to Android device functionality as API.

What is SSH?

SSH stands for Secure Shell. It is a cryptographic network protocol that allows secure communication between two devices over an insecure network. SSH is commonly used for remote login to a server or computer, allowing users to access a command-line interface and execute commands on a remote machine securely.


SSH TO ANDROID PHONE

f-droid

Termux

Termux-api


  • Commands from termux app on the phone:
    pkg update -y
    pkg install openssh termux-api -y
    ifconfig (note inet address: 192.168.1.228)
    whoami (note the username: u0-a564)
    passwd (to set desired password)
    sshd (to start ssh in the background)

  • Now that we have installed Termux and OpenSSH, let’s connect to the phone from our computer:

       ssh [email protected] -p 8022
       or:
       ssh 192.168.1.228 -p 8022


  • You should now see the text below on your computer if it’s the first time connecting your phone.
    Type: yes

    The authenticity of host ‘[192.168.1.101]:8022 ([192.168.1.101]:8022)’ can’t be established.
    ED25519 key fingerprint is SHA256:ypEgvEH63cav/FXJkOAFB3mmEi002M+LNGjG3TskDiU. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])?

Congrats! You have now successfully connected your phone to your computer via SSH!

Why did we use port 8022 and not 22 for ssh?

Port 8022 is not the default SSH port, which is 22. Changing the SSH port from the default 22 to a different port, like 8022, can be a security measure. Using a non-standard port can make it harder for unauthorized users or automated bots to find and potentially exploit your SSH server.


TERMUX API Command Table

additional dependencies:
apt install x11-rep root-repo unstable-repo

Let’s see what commands we can run using Termux API:

Command Description
termux-setup-storage Access shared storage
termux-battery-status Check battery status
termux-sms-send -n 01235678910 hello Send an SMS
termux-microphone-record -f ~/out.mp3 Record audio to ~/out.mp3
termux-microphone-record -q Stop recording audio
termux-clipboard-set "hello word" Set clipboard text to “hello word”
termux-clipboard-get Print clipboard text
termux-sensor -l List available sensors
termux-torch on Turn on torch (off to switch off)
termux-camera-photo photo.jpg Take a picture and save as photo.jpg
termux-tts-speak "hello how what are you doing" Text-to-speech, phone speaks the text

Additional Termux API implementations:

You can find additional information and full list of termux api commands on the official page of the Termux API:

Termux:API wiki



SFTP

What is SFTP?

SFTP, or Secure File Transfer Protocol, is a network protocol designed for secure file access and transfer. It encrypts both commands and data, ensuring a safe transfer of files between clients and servers.

  • using file manager:
    sftp://192.168.1.228:8022/

    192.168.1.228 # host
    8022 # port

See SSH section ↑
to find your host IP address: ifconfig (inet address)


  • using client:
    FileZilla is one of the most popular SFTP clients, known for its user-friendly interface and robust features. It supports secure file transfers via SFTP, making it a preferred choice for both beginners and professionals.
    filezilla



SUDO TERMUX

What is sudo?

In Termux, sudo is a command that allows a permitted user to execute a command as the superuser or another user, as specified by the security policy. It is used to perform administrative tasks that require elevated privileges on the Android device within the Termux environment.


Commands to gain superuser in Termux

  1. apt install git tsu x11-repo android-tools

  2. git clone https://gitlab.com/st42/termux-sudo

  3. cd termux-sudo

  4. cat sudo > /data/data/com.termux/files/usr/bin/sudo

  5. chmod 700 /data/data/com.termux/files/usr/bin/sudo

Commands if an error occurs:

  1. ls
  2. cd termux-sudo/
  3. chmod +x sudo
  4. pkg remove tsu -y
  5. pkg install tsu -y



SCREENSHARING via SSH

YouTube’s walkthrough video by Kris Occhipinti


Some of the commands below might not work, so feel free to experiment:
  • play video via ssh screenrecord:
ssh [email protected] -p 8022 "sudo screenrecord --output-format=h264 -"| ffplay - 2>/dev/null
  • play video via ssh ffmpeg:
ssh [email protected] -p 8022 "ffmpeg -f x11grab -i :0 -f avi - "| ffplay - &>/dev/null
  • play video via ssh:
/system/bin/screenrecord --output /data/data/com.termux/files/home/hello.mp4 --output-format=h264
  1. start sshd in termux on rooted device,
  2. then connect to the device from a Desktop machine like this:
ssh [email protected] -p8022 "sudo screenrecord --output-format=h264 -"| ffplay - 2>/dev/null

Casting Android Screen Across Network with Busybox nc (this is not encrypted)

On Desktop:

busybox nc -lp 8022|ffplay - 2>/dev/null


On Android Device:

screenrecord --output-format=h264 -|busybox nc <DESKTOP IP> 8022


Or run as on liner on desktop:

(busybox nc -lp 8022|ffplay - 2>/dev/null)&(adb shell screenrecord --output-format=h264 -|busybox nc <DESKTOP IP> 8022)



SCREENSHARING via SCRCPY

What is Scrcpy?

SCRCPY is a free and open-source application that allows you to display and control Android devices connected via USB or TCP/IP. It provides a way to mirror and control your Android device from a computer, offering features like screen mirroring, file transfer, and remote control of the Android device from the computer.


Let’s First enable usb debugging on android phone:

settings > about > software information > tap “Build number” 7 times > back to settings > developer options > enable USB debugging


you should now see allow usb debugging on your phone:

click ok


Enabling USB debugging (Security settings) allows control via input, not just screen sharing.


SCRCPY installation for Windows

Icon
  1. download SCRCPY . Choose from Assets appropriate version mine is windows 64 bit so (scrcpy-win64-v2.4.zip) extract and go inside directory.

  2. Once you’re in the scrcpy directory, open terminal and execute the following command:

       ./scrcpy


Congrats! You have now successfully connected your phone to your computer via SCRCPY!


Conclusion

Termux offers a powerful Linux environment on Android devices, enabling users to run command-line programs, SSH into their devices, and manage various tasks efficiently. With the Termux API, users can further extend the functionality by accessing shared storage, controlling device sensors, sending SMS, and more, all from the command line. Additionally, tools like SFTP, sudo for elevated privileges, and screen sharing options like SCRCPY and SSH-based methods provide comprehensive control and flexibility for Android users, bridging the gap between mobile and desktop computing. Whether you’re a tech enthusiast or a developer, Termux opens up a world of possibilities for Android device customization and remote management.

Tags:  #termux #android