Termux: Linux Administration On-The-Go from Android

Termux: Linux Administration On-The-Go from Android
Termux

Termux is an amazing open-source Android app that primarily offers terminal emulation. However, it is something much more than a terminal. Using Termux is like you have a Linux distribution inside your Android device. Without rooting the device. Without any special setup.

A basic set of Linux commands is available and you can install more packages using the apt package manager.

You can use Termux with the Android virtual keyboard, but using an external keyboard is much more convenient.

Great documentation is available in their wiki. The packages list is available here.

Although you can set up a lot of packages in Termux, the most important thing (according to my opinion) is the ability to securely connect with a remote server using SSH. In this server, you may have any software you want 🙂

Installation

Install Termux at Google Play Store here.

Start a session

Starting Termux, you will get the following welcome screen:

Welcome to Termux!

Wiki:            https://wiki.termux.com
Community forum: https://termux.com/community
Gitter chat:     https://gitter.im/termux/termux
IRC channel:     #termux on freenode

Working with packages:

 * Search packages:   pkg search <query>
 * Install a package: pkg install <package>
 * Upgrade packages:  pkg upgrade

Subscribing to additional repositories:

 * Root:     pkg install root-repo
 * Unstable: pkg install unstable-repo
 * X11:      pkg install x11-repo

Report issues at https://termux.com/issues

$

Here is a screenshot of welcome screen

Termux welcome screen
Termux welcome screen

Left panel

You can manage sessions from Termux left panel (swipe from the left to right to display it).

From this panel you can also show/hide Android keyboard.

Termux left panel
Termux left panel

Copy – Paste inside Termux

In most devices (with external keyboard), Ctrl+Shift+C (for copy) and Ctrl+Shift+V (for paste).

What is always working is the “long pressing anywhere on the terminal”:

Termux: copy paste share
Termux: copy paste share

Font size

There are 3 ways to change font size:

  • Use the “two-finger pinch gesture” as usual to magnify the screen (or the opposite)
  • Use CTRL ALT + from the command line to increase the font size and CTRL ALT – for the opposite
  • Use Termux Styling for a small cost

Common commands

$ whoami
u0_a255
$ pwd
/data/data/com.termux/files/home
$ ls -la                                total 11
drwx------ 2 u0_a255 u0_a255 3488 Aug 12 22:16 .
drwx------ 4 u0_a255 u0_a255 3488 Aug 12 21:37 ..
-rw------- 1 u0_a255 u0_a255   95 Aug 15 12:43 .bash_history
$ date
Sat Aug 15 12:44:24 EEST 2020           
$

Text editors

nano is available by default.

For other popular editors see https://wiki.termux.com/wiki/Text_Editors

Color Bash prompt

If you want to colorize bash prompt

cd ~
nano .bashrc

Add the following

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

Then

. .bashrc

I maintain a Github repo for Termux dotfiles at https://github.com/pontikis/dotfiles-termux

The relevant file is https://github.com/pontikis/dotfiles-termux/blob/master/.bashrc

The result

Termux color bash prompt
Termux color bash prompt

Storage

By default all files created by Termux are only available through Termux session.

See https://wiki.termux.com/wiki/Internal_and_external_storage

To access files outside Termux

termux-setup-storage

Now, you can list external files using

ls -la ~/storage
total 7
drwx------ 2 u0_a255 u0_a255 3488 Aug 15 13:27 .
drwx------ 6 u0_a255 u0_a255 3488 Aug 15 13:27 ..
lrwxrwxrwx 1 u0_a255 u0_a255   26 Aug 15 13:27 dcim -> /storage/emulated/0/DCIM
lrwxrwxrwx 1 u0_a255 u0_a255   30 Aug 15 13:27 downloads -> /storage/emulated/0/Download
lrwxrwxrwx 1 u0_a255 u0_a255   30 Aug 15 13:27 movies -> /storage/emulated/0/Movies
lrwxrwxrwx 1 u0_a255 u0_a255   30 Aug 15 13:27 music -> /storage/emulated/0/Music
lrwxrwxrwx 1 u0_a255 u0_a255   30 Aug 15 13:27 pictures -> /storage/emulated/0/Pictures
lrwxrwxrwx 1 u0_a255 u0_a255   22 Aug 15 13:27 shared -> /storage/emulated/0
$

In case that external storage exists (eg SD card), the result looks like:

u0_a255@localhost:~$ ls -la storage/total 7
drwx------ 2 u0_a255 u0_a255 3488 Aug 18 16:58 .
drwx------ 7 u0_a255 u0_a255 3488 Aug 18 16:58 ..
lrwxrwxrwx 1 u0_a255 u0_a255   26 Aug 18 16:58 dcim -> /storage/emulated/0/DCIM
lrwxrwxrwx 1 u0_a255 u0_a255   30 Aug 18 16:58 downloads -> /storage/emulated/0/Download
lrwxrwxrwx 1 u0_a255 u0_a255   50 Aug 18 16:58 external-1 -> /storage/47BB-17F4/Android/data/com.termux/files
lrwxrwxrwx 1 u0_a255 u0_a255   30 Aug 18 16:58 movies -> /storage/emulated/0/Movies
lrwxrwxrwx 1 u0_a255 u0_a255   30 Aug 18 16:58 music -> /storage/emulated/0/Music
lrwxrwxrwx 1 u0_a255 u0_a255   30 Aug 18 16:58 pictures -> /storage/emulated/0/Pictures
lrwxrwxrwx 1 u0_a255 u0_a255   22 Aug 18 16:58 shared -> /storage/emulated/0
u0_a255@localhost:~$

Access Termux $HOME outside Termux

You can’t!

Termux $HOME is located at

/data/data/com.termux/files/home

and you don’t have access to /data/data folder (in a non-rooted device).

See also https://wiki.termux.com/wiki/Sharing_Data

System update

Use

pkg update && pkg upgrade

or (if you prefer)

apt-get update && apt-get -V upgrade

Result

Testing the available mirrors:
[*] https://dl.bintray.com/termux/termux-packages-24: ok
[*] https://grimler.se/termux-packages-24: ok
[*] https://main.termux-mirror.ml: ok
[*] https://termux.mentality.rip/termux-packages-24: ok
Picking mirror: https://dl.bintray.com/termux/termux-packages-24
Ign:1 https://dl.bintray.com/termux/termux-packages-24 stable InRelease
Ign:2 https://dl.bintray.com/grimler/game-packages-24 games InRelease
Ign:3 https://dl.bintray.com/grimler/science-packages-24 science InRelease
Get:4 https://dl.bintray.com/termux/termux-packages-24 stable Release [8255 B]
Get:5 https://dl.bintray.com/grimler/game-packages-24 games Release [5344 B]
Get:6 https://dl.bintray.com/grimler/science-packages-24 science Release [6191 B]
Get:7 https://dl.bintray.com/termux/termux-packages-24 stable Release.gpg [821 B]
Get:8 https://dl.bintray.com/grimler/game-packages-24 games Release.gpg [475 B]
Get:9 https://dl.bintray.com/grimler/science-packages-24 science Release.gpg [475 B]
Get:10 https://dl.bintray.com/termux/termux-packages-24 stable/main aarch64 Packages [117 kB]
Get:11 https://dl.bintray.com/grimler/game-packages-24 games/stable aarch64 Packages [3983 B]
Get:12 https://dl.bintray.com/grimler/science-packages-24 science/stable aarch64 Packages [8722 B]
Fetched 151 kB in 4s (35.8 kB/s)
Reading package lists... Done
Building dependency tree... Done
2 packages can be upgraded. Run 'apt list --upgradable' to see them.
Reading package lists... Done
Building dependency tree... Done
Calculating upgrade... Done
The following packages will be upgraded:
  command-not-found termux-tools
2 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 148 kB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 https://dl.bintray.com/termux/termux-packages-24 stable/main aarch64 termux-tools all 0.94 [7766 B]
Get:2 https://dl.bintray.com/termux/termux-packages-24 stable/main aarch64 command-not-found aarch64 1.56 [141 kB]
Fetched 148 kB in 0s (171 kB/s)
(Reading database ... 3450 files and directories currently installed.)
Preparing to unpack .../termux-tools_0.94_all.deb ...
Unpacking termux-tools (0.94) over (0.93) ...
Setting up termux-tools (0.94) ...
(Reading database ... 3450 files and directories currently installed.)
Preparing to unpack .../command-not-found_1.56_aarch64.deb ...
Unpacking command-not-found (1.56) over (1.55-1) ...
Setting up command-not-found (1.56) ...
Checking availability of current mirror: bad
Testing the available mirrors:
[*] https://dl.bintray.com/termux/termux-packages-24: bad
[*] https://grimler.se/termux-packages-24: ok
[*] https://main.termux-mirror.ml: ok
[*] https://termux.mentality.rip/termux-packages-24: ok
Picking mirror: https://main.termux-mirror.ml
Ign:1 https://dl.bintray.com/grimler/game-packages-24 games InRelease
Ign:2 https://dl.bintray.com/grimler/science-packages-24 science InRelease
Ign:3 https://main.termux-mirror.ml stable InRelease
Get:4 https://dl.bintray.com/grimler/game-packages-24 games Release [5344 B]
Hit:4 https://dl.bintray.com/grimler/game-packages-24 games Release
Get:5 https://main.termux-mirror.ml stable Release [8255 B]
Get:6 https://main.termux-mirror.ml stable Release.gpg [821 B]
Get:7 https://dl.bintray.com/grimler/science-packages-24 science Release [6191 B]
Hit:7 https://dl.bintray.com/grimler/science-packages-24 science Release
Get:9 https://main.termux-mirror.ml stable/main aarch64 Packages [117 kB]
Fetched 126 kB in 3s (33.2 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
$

More info https://wiki.termux.com/wiki/Package_Management

Setup basic tools

Easily find files

pkg install mlocate
Checking availability of current mirror: bad
Testing the available mirrors:
[*] https://dl.bintray.com/termux/termux-packages-24: ok
[*] https://grimler.se/termux-packages-24: ok
[*] https://main.termux-mirror.ml: ok
[*] https://termux.mentality.rip/termux-packages-24: ok
Picking mirror: https://main.termux-mirror.ml
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
mlocate
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 36.1 kB of archives.
After this operation, 156 kB of additional disk space will be used.
Get:1 https://main.termux-mirror.ml stable/main aarch64 mlocate aarch64 0.26-4 [36.1 kB]
Fetched 36.1 kB in 7s (4643 B/s)
Selecting previously unselected package mlocate.
(Reading database ... 3450 files and directories currently installed.)
Preparing to unpack .../mlocate_0.26-4_aarch64.deb ...
Unpacking mlocate (0.26-4) ...
Setting up mlocate (0.26-4) ...
$

then run

updatedb

To find any file

locate filename

Advanced auto-completion

pkg install bash-completion

Install Midnight Commander file manager

pkg install mc

Remote access with SSH

OpenSSH

pkg install openssh

This will also create the directory ~/.ssh

More info https://wiki.termux.com/wiki/Remote_Access

SSH key

Create SSH key with strong passphrase:

ssh-keygen -b 4096 -t rsa

Add public key (~/.ssh/id_rsa.pub) to Github or Bitbucket or whatever

Config ssh

You can config each ssh connection you use adding a file ~/.ssh/config

If you don’t use the default port 22 (strongly recommended) you may define the port in ~/.ssh/config so you do not have to set the port in the ssh command.

If your remote server has to connect with Github (for example) in order to deploy a project, you have to create SSH keys (as mentioned above). But, having SSH keys in a machine working on the internet may be a security risk. You may use the local agent for this purpose (strongly recommended). This is called agent forwarding. Read more here and here.

You may also config the User.

nano ~/.ssh/config

example:

Host server1.com
  Port 5001
  User username

Host server2.com.com
  Port 5000
  ForwardAgent yes
  User username

Host server3.com
  Port 22
  ForwardAgent yes
  User username

Copy public key to remote server

ssh-copy-id -i ~/.ssh/id_rsa.pub username@remote_server

REMARK: ssh-copy-id needs to connect using a password. If you have disabled password authentication with PasswordAuthentication no (strongly recommended), you have to enable it for a moment to execute ssh-copy-id. Then, disable it again.

Connect

Start SSH agent:

eval "$(ssh-agent -s)"
ssh-add

Connect:

ssh username@remote_server

or more simple (in case User is defined in ~/.ssh/config)

ssh remote_server

Appendix

Your comments are welcome!


Never Miss A Post!

Subscribe to our free NewsLetter