Termux step by step (Part 1)

termux step by step

When I first met Termux, and I'm far from being a Linux user, it caused two thoughts in my head: “Cool utter!” and "How to use it?". Having rummaged through the Internet, I did not find a single article that fully allows you to start using Termux so that it brings more pleasure than crap. We'll fix this.

For what, in fact, did I get to Termux? Firstly, hacking, or rather the desire to understand it a little. Secondly, the inability to use Kali Linux.
Here I will try to put together all the useful things I found on the topic. This article is unlikely to surprise anyone who understands, but for those who only know the delights of Termux, I hope it will be useful.

For a better understanding of the material, I recommend repeating what I described not as a simple copy-paste, but to enter commands on my own. For convenience, we need either an Android device with a keyboard connected, or, as in my case, an Android device and a PC / Laptop (Windows) connected to the same network. Android is preferably rooted, but not required. Sometimes I indicate something in brackets, usually this will allow you to better understand the material (if what is written in brackets is not entirely clear, feel free to skip it, then everything will be explained in the process and as necessary).

Step 1

I will be banal and damn logical at the same time

Install Termux from Google Play Market:

Termux step by step (Part 1)

We open the installed application and see:

Termux step by step (Part 1)

The next step is to update the pre-installed packages. To do this, we enter two commands in order, in the course of which we agree with everything by entering Y:

apt update
apt upgrade
With the first command, we check the list of installed packages and look for those that can be updated, and with the second we update them. For this reason, the commands must be written in this sequence.

We now have the most recent version of Termux.

A few more commands

ls - displays a list of files and directories in the current directory
cd – moves to the specified directory, for example:
It is important to understand: if the path is not specified directly (~/storage/downloads/1.txt) it will be from the current directory
cd dir1 – will move to dir1 if it exists in the current directory
cd ~/dir1 – will move to dir1 at the specified path from the root folder
cd  or cd ~ - move to root folder
clear - clear the console
ifconfig - you can see the IP, or you can configure the network
cat – allows you to work with files/devices (within the same thread) for example:
cat 1.txt – view the contents of the 1.txt file
cat 1.txt>>2.txt – copy file 1.txt to file 2.txt (file 1.txt will remain)
rm - used to remove files from the file system. Options used with rm:
-r – process all nested directories. This key is required if the file being deleted is a directory. If the file being deleted is not a directory, then the -r option has no effect on the rm command.
-i – display a confirmation prompt for each deletion operation.
-f – do not return an erroneous exit code if the errors were caused by non-existent files; do not ask for confirmation of transactions.
For example:
rm -rf mydir – delete the file (or directory) mydir without confirmation and error code.
mkdir <путь> - creates a directory at the specified path
echo – can be used to write a line to a file, if '>' is used, the file will be overwritten, if '>>' the line will be appended to the end of the file:
echo "string" > filename
We look for more details on UNIX commands on the Internet (no one canceled self-development).
The keyboard shortcut Ctrl + C and Ctrl + Z interrupts and stops the execution of commands, respectively.

Step 2

Make your life easier

In order not to torture yourself unnecessarily by entering commands from the on-screen keyboard (in the “field” conditions, of course, you can’t get away from this) there are two ways:

  1. Connect a full keyboard to your Android device in any convenient way.
  2. Use ssh. Simply put, the console of Termux running on your Android device will be opened on your computer.

I went for the second way, although it is a bit complicated to set up, it all pays off in ease of use.

You need to install the ssh client program on the computer, I use Bitvise SSH Client, incl. all further actions are performed in this program.

Termux step by step (Part 1)

Because at the moment Termux only supports connecting using the Publickey method using a key file, we need to create this file. To do this, in the Bitvise SSH Client program, on the Login tab, click on client key manager in the window that opens, generate a new public key and export it in OpenSSH format to a file called termux.pub (in fact, any name can be used). The created file is placed in the internal memory of your Android device in the Downloads folder (this folder, and several others, Termux has simplified access without root).

In the Login tab, in the Host field, enter the IP of your Android device (you can find out by entering the ifconfig command in Termux) in the Port field should be 8022.

Now let's move on to installing OpenSSH in Termux, for this we enter the following commands:

apt install openssh (in the process, if necessary, enter 'y')
pkill sshd (with this command we stop OpenSSH)
termux-setup-storage (connect internal memory)
cat ~/storage/downloads/termux.pub>>~/.ssh/authorized_keys (copy key file)
sshd (start ssh host)

We return to Bitvise SSH Client and click the Log in button. During the connection process, a window will appear in which we select Method - publickey, Client key is Passphrase (if you specified it when generating the key file).

In case of a successful connection (if everything is done as written, it should connect without problems), a window will open.

Termux step by step (Part 1)

Now we can enter commands from the PC and they will be executed on your Android device. It is not difficult to guess what benefits this provides.

Step 3

Set up Termux, install additional utilities

First of all, let's install bash-completion (shortcut, magic-Tab, whoever calls it). The essence of the utility is that, by entering commands, you can use autocomplete by pressing Tab. To install, write:

apt install bash-completion (Works automatically on pressing Tab)

Well, what is life without a text editor with code highlighting (if you suddenly want to code, but you want to). To install, write:

apt install vim

Here you can already use autocomplete - we write 'apt i' now press Tab and our command is appended to 'apt install'.

Using vim is not difficult, to open the 1.txt file (if it does not exist, it will be created) we write:

vim 1.txt

Press 'i' to start typing
Press ESC to finish typing
The command must be preceded by a colon ':'
':q' - exit without saving
':w' - save
':wq' - save and exit

Since we can now create and edit files, let's improve the look and feel of Termux's command line a bit. To do this, we need to set the PS1 environment variable to "[ 33[1;33;1;32m]:[ 33[1;31m]w$ [ 33[0m][ 33[0m]" (if you are wondering what it is and with what eat it please here). To do this, we need to add the line to the '.bashrc' file (it is located at the root and is executed every time the shell is started):

PS1 = "[ 33[1;33;1;32m]:[ 33[1;31m]w$ [ 33[0m][ 33[0m]"

For simplicity and clarity, we will use vim:

cd
vim .bashrc

We enter the line, save and exit.

Another way to add a line to a file is to use the 'echo' command:

echo PS1='"[ 33[1;33;1;32m]:[ 33[1;31m]w$ [ 33[0m][ 33[0m]"' >>  .bashrc

Note that to display double quotes, the entire string with double quotes must be enclosed in single quotes. This command has '>>' because the file will be padded to overwrite '>'.

In the .bashrc file, you can also enter alias's - abbreviations. For example, we want to carry out update and upgrade with one command at once. To do this, add the following line to .bashrc:

alias updg = "apt update && apt upgrade"

To insert a line, you can use vim or the echo command (if it doesn’t work on your own - see below)

The alias syntax is:

alias <сокращение> = "<перечень команд>"

So let's add an abbreviation:

echo alias updg='"apt update && apt upgrade"' >> .bashrc

Here are some more useful utilities

Install via apt install

man - Built-in help for most commands.
man %commandname

imagemagick - Utility for working with images (converting, compressing, cropping). Supports many formats including pdf. Example: Convert all pictures in the current folder into one pdf and reduce their size.
convert *.jpg -scale 50% img.pdf

ffmpeg - One of the best audio/video converters. Google instructions for use.

mc - A two-pane file manager like Far.

There are still many steps ahead, the main thing is that the movement has begun!

Source: habr.com

Add a comment