Using terminals in Android

Image credit: John Moeses Bauan

Terminals on the smartphone

Install terminal applications from the application store (e.g., Termux). However, on some Android phones there might be an unable to install issue. Under these cases, one could configure an easy-to-use terminal based on the original Android terminal.

Step 1: Install busybox

Busybox source binary could be found in its official website: https://busybox.net/downloads/binaries/1.21.1/. Get the source binary and install as follows:

curl ${busybox-url}
mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
cp ${busybox-binary-file} /system/xbin
cd /system/xbin
chmod 777 busybox
busybox --install .

ps | grep sh # test whether busybox is successfully installed, grep doesn't exist before
# vi is also available
# busybox ifconfig to check interface

Step 2: Install bash shell

mv ${bash-file} /system/xbin
cd /system/xbin
chmod 755 bash

https://blog.csdn.net/lindev/article/details/9720179

https://forum.xda-developers.com/t/better-terminal-emulator-magic.518959/#post-3839562

# bash-config (put it in /data/user)
HOME=/data/user
HISTFILE=${HOME}/history
HISTCONTROL=ignoredups:ignorespace
PS1='\e[1;34m[chenj]\$\e[0m'
export HISTTIMEFORMAT='%F %T '
export HISTFILESIZE=500000
export HISTSIZE=500000
shopt -s histappend
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'
cd ${HOME}

start bash with: bash --rcfile /data/user/bash-config

Root the Android phone

Step 1: Try rooting programs

Rooting programs might be able to root a phone with one click of buttom. For example, Kingo Root would work on ZTE NX508J. However, on many Android phones with higher Android versions, the rooting programs probably would not work. Then go to Step 2.

Step 2: Unlocking the bootloader (BL)

Mi bootloader unlock: https://www.bilibili.com/video/BV1SQ4y1k7my/?p=1&share_medium=android&share_plat=android&share_session_id=e22fef2b-1df9-413f-946b-d232f9f659b3&share_source=COPY&share_tag=s_i×tamp=1635362899&unique_k=YrG7dy

https://en.miui.com/unlock/download_en.html

Xiaomi Mi 8SE bootloader unlock and TWRP & Rom Install: https://www.youtube.com/watch?v=OLJUU_5fXoI&ab_channel=RicksTechRepairs

How to FIX Mi flash unlock tool not detecting Xiaomi phone and unlock: https://www.youtube.com/watch?v=etjs-47zdUY&ab_channel=VelianSpeaksTech

Step 3: Install Magisk and patch the image

Install MagiskManager.apk. Magisk releases could be found in: https://github.com/topjohnwu/Magisk/releases, and a detailed introduction to Magisk: https://sspai.com/post/53043.

Image could be found in the official websites, for example, for Google phones: https://developers.google.cn/android/images. Subtract boot.img from the downloaded image and then patch the image based on this guidance: https://blog.csdn.net/qq_36628150/article/details/113865782

Step 4: flash in the new image

adb reboot bootloader
fastboot getvar current-slot ${slot}
fastboot flash boot_${slot} ${patched_img}
fastboot reboot

If you meet the fastboot waiting for any device (on win10) issue, the solution is:

  1. use USB 2.0 interface
  2. download driver https://developer.android.com/studio/run/win-usb or https://www.ytechb.com/install-google-usb-driver-windows/
  3. go to Device Manager, find your phone in Other Devices (not Portable Devices), and Update driver for it by selecting your downloaded driver to initiate driver installation. https://blog.csdn.net/tq501501/article/details/117471016

Check if rooting is successful

root succeeds if su appears in /system/xbin

Android adb debug

adb devices # check whether android phone is connected to laptop
adb -d shell su # open android shell with root

# File transmission
adb push ${laptop-local-file} ${android-dir} # transmit file to android phone
adb pull ${android-file} ${laptop-local-dir} # transmit file from android phone to the laptop

# install package
adb install [-r] [-s] <apk-file> # -r reinstall the apk, -s install the apk package to the sdcard (it is installed to the internal storage by default)

# uninstall the package
adb uninstall [-k] <package> # -k:delete the app but keep user data and cached menu

# reboot
adb reboot

# check phone model
adb shell getprop ro.product.model

# check Android system version
adb shell getprop ro.build.version.release

# check CPU information
adb shell cat /proc/cpuinfo

# check CPU architecture
adb shell getprop ro.product.cpu.abi

# check memory information
adb shell cat /proc/meminfo

Fuller version of abd command https://juejin.cn/post/7036000323990716423

Android adb commandline debug (am, pm, input, dumpsys) https://blog.csdn.net/weixin_44021334/article/details/118354101

Did you find this page helpful? Consider sharing it 🙌

Jing Chen
Jing Chen
Ph.D. of Computer Networking

My research interests include low-latency network transport, interactive video streaming and wireless networks.