Basic Termux command

What is termux?

Termux is an Android terminal emulator and Linux environment app that works directly with no rooting or setup required. A minimal base system is installed automatically – additional packages are available using the APT package manager.

We will be seeing some of the basic commands to use in termux app.

TO UPDATE

apt update

Check whether termux is properly installed

pkg install sl

sl

Find factors of a number

pkg install coreutils

factor 1337

To open any site in termux

apt install w3m

First, install curl

pkg install curl

after that:

curl -LO https://raw.githubusercontent.com/Hax4us/Metasploit_termux/master/metasploit.sh

Get access permission

chmod 777 metasploit.sh

./metasploit.sh

How do I get help for a specific package?

Usually packagename -h will display basic help for an application.

Advanced information about program usage can be viewed through man tool. It can be installed with pkg install man. Typing commands man man or man info can help beginning and advanced users alike. For example, man busybox will output the manual page for the package ‘busybox’.

Tips, when reading a man page:

  • Use q to quit
  • Use space for next page
  • /search for search
  • n for repeat search

Why do I keep getting a ‘/bin/sh bad interpreter’ error?

This error is thrown due to access script interpreter at nonexistent location.

Termux does not have common directories like /bin, /sbin, /usr/bin at their standard place. There is an exception for certain devices where /bin is a symbolic link to /system/bin, but that does not make a difference.

Interpreters should be accessed at this directory only:

/data/data/com.termux/files/usr/bin

There are three ways to fix this:

  • Install termux-exec by using pkg install termux-exec. It won’t affect the current session, but after a restart should work without any setup. Not needed if your Termux is up to date. If still not working, try the next workaround.
  • Use command termux-fix-shebang to fix the shebang line of specified file.
  • Use termux-chroot from package proot to setup a chroot environment mimicking a normal Linux file system in Termux.

Why does a compiled program show warnings about DT_FLAGS_1=0x8?

This warning is completely safe and just notifies developer that linker detected unused extra information inside executable file. In case of DT_FLAGS_1=0x8, it warns about RTLD_NODELETE ELF section. Besides DT_FLAGS_1=0x8, there more types of ELF sections which are not handled by Android linker.

To make this warning disappear you need to use utility “termux-elf-cleaner” binary file and probably on all its dependencies.

pkg install termux-elf-cleaner
termux-elf-cleaner ./myprogram ./libmysharedlibrary.so

Number of supported ELF sections increases with Android OS version. On Android 7.0 and higher you are unlikely to get linker warnings.

Why does an executable throw an ‘Exec format error’?

You are trying to execute binary compiled for the different CPU architecture. For example, it is not possible to run x86 binary on ARM and vice versa.

If you have the source code for your binary, try to recompile it with Android NDK to CPU architecture of your device which can be checked with uname -m.

If binary is closed source or you cannot get it successfully compiled, try the QEMU in user mode. In order to run x86 binary, install the package qemu-user-i386 (if 32 bit) or qemu-user-x86_64 (if 64 bit). Then try to execute your binary under QEMU in way like qemu-i386 ./myprogram.

How do I fix a broken environment?

export PREFIX=/data/data/com.termux/files/usr
export PATH=${PATH}:${PREFIX}/bin

How can I keep my data when reinstalling Termux?

Usually there is no need to reinstall Termux. Just rm -rf $PREFIX and restart application – this will not touch the content of $HOME.

If you really want to reinstall application, you can archive contents of $HOME and store it on shared storage (/sdcard).

Backing up Termux

Backing up

In this example, a backup of both home and sysroot will be shown. The resulting archive will be stored on your shared storage (/sdcard) and compressed with gzip.

1. Ensure that storage permission is granted:

termux-setup-storage

2. Go to Termux base directory:

cd /data/data/com.termux/files

3. Backing up files:

tar -zcvf /sdcard/termux-backup.tar.gz home usr

Backup should be finished without any error. There shouldn’t be any permission denials unless the user abused root permissions. Warnings about sockets are okay.

Restoring

Instructions for home directory and usr (sysroot or prefix) are separate, though if you did backup in the way shown above, these directories are stored in the same archive.

There also will be assumed you have granted access to shared storage and your archive is stored at /sdcard/termux-backup.tar.gz.

By following these instructions all your Termux files will be overwritten with ones from back up.

Home directory

Just follow the steps listed here:

1. Go to Termux base directory:

cd /data/data/com.termux/files

2. Replace home directory with one from your backup:

rm -rf home
tar -zxvf /sdcard/termux-backup.tar.gz home

The home directory isn’t runtime-critical, no additional steps like closing/re-opening Termux required.

Sysroot (prefix)

The restoring of sysroot is quite complicated. You will have to delete $PREFIX where all package data is stored.

1. Go to Termux base directory:

cd /data/data/com.termux/files

2. Copy busybox binary in the way shown here. You can’t use any other archiver binary here as only busybox doesn’t have dependencies which will gone in next step.

pkg install busybox
cp ./usr/bin/busybox ./tar

3. Erase sysroot. At this point, all packages will be deleted.

rm -rf usr

4. Restore sysroot from backup:

unset LD_PRELOAD
./tar -zxvf /sdcard/termux-backup.tar.gz usr

Now close Termux with the “exit” button from notification and open it again.

With restic

Restic is a utility for doing incremental backups. It can work with both local and remote backups. Backed up data is encrypted and de-duplicated.

This package is not available by default. Install it with the following command:

pkg install restic

Notice: restic is for advanced users only. While it provides more features than tar, it is harder in use. Most people will use it in scripts rather than manually in the command line. Instructions here provided only as an example.

Backing up

These steps will backup only sysroot (prefix) and not your home. The repository will be stored on shared storage.

1. Initialize local restic repository. You will be prompted for a password because encryption is enabled (no way to turn off). If the password is lost – your data will be locked forever.

restic init -r /sdcard/termux-backups

2. Backing up sysroot ($PREFIX):

restic backup -r /sdcard/termux-backups --tag termux $PREFIX

A tag is not necessary but will be useful in case if you decide to backup multiple directories into the same repository. For example, you can backup home directory in this way:

restic backup -r /sdcard/termux-backups --tag termux-home $HOME

Restoring

Same as with tar-based backups, restoring of sysroot is tricky. Following steps are applicable only for local backups:

1. Make sure that restic is available:

pkg install restic

2. Copy restic binary to $HOME. We are going to erase sysroot.

cp $PREFIX/bin/restic $HOME/restic

3. Erase sysroot. All packages will be deleted.

rm -rf $PREFIX

4. Restore sysroot from latest snapshot:

unset LD_PRELOAD
$HOME/restic restore -r /sdcard/termux-backups --tag termux --target / latest

If everything is done right, you should now have a Termux restored from the backup. Close your Termux application and open it again.

Tip: if you do not want to enter a restic password or specify repository each time, you can set environment variables “RESTIC_PASSWORD” and “RESTIC_REPOSITORY”.

 

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *