KBOX Linux emulator for Android

KBOX2 is a BusyBox based Linux emulator for Android running inside Jack Palevich's Terminal Emulator for Android or ConnectBot (Android terminal emulators).

With this solution you have a (basic) Linux system in the pocket. You can even compile programs.

Installation of KBOX2
KBOX2 is used on Adroid 4 systems. Run another Android App OneBox Package Manager installer once to create a kbox2 subdirectory under the terminal emulator application containing all necessary emulator software, providing simplified versions of a lot of Linux commands.


 * 1) There also exists a manual installation script, see KBOX2 installation; you can use this script to install KBOX in the ConnectBot application directory /data/data/org.connectbot
 * 2) Why is the Linux emulator installed below the subdirectory of a terminal emulator? Because the Android security model dictates that every app can only access its own executables and data files...

Installation of KBOX3
KBOX3 is used on Adroid 5 systems. Here we document using the ConnectBot terminal emulator. cd /data/data/org.connectbot Now you can follow the installation instructions on KBOX3 installation.

Create a symbolic link: ln -s kbox3/bin/kbox_shell kbox

Now start the emulator manually: /data/data/org.connectbot/kbox

To install more packages, see KBOX3 downloads.

Restrictions
This should work for any off-the-shelf Android device. Your device needs not to be (and should not be) rooted.

As a consequence only user mode programs are allowed. TCP/IP privileged ports lower than 1024 cannot be used. For the same reason SUID programs are technically impossible (e.g. ping, route).

Hidden files remain invisible, even when using ls -lad *

Since Android does not have real usernames, any program in the emulation needs to use fake usernames like "kbox" etc. Also remember when using e.g. ssh to use real usernames.

The external SD card is vfat mounted, so specific Linux features can't be used. It can however be used as a storage device, e.g. /storage/extSdCard/Download can be used to store installation kits.

Run the emulator
By default the Jack Palevich's terminal emulator uses the standard Android shell /system/bin/sh.

To automatically run the emulator shell, execute: /data/data/jackpal.androidterm/kbox2/bin/kbox_shell


 * 1. You can automate the execution of this shell by replacing the default /system/bin/sh - shell via:

Preferences &rarr; SHELL &rarr; Command line
 * 2. An alternative shell is not available from the ConnectBot terminal emulator. In this case you can use a symbolic link.

Libraries
The emulator has a primitive Android Bionic C-library.

The emulator is a BusyBox implementation, so it only has basic Linux functionality.

If you need the full Linux commands, then you better install

Processor
uname -a Linux localhost 3.0.31-963581 #2 SMP PREEMPT Mon Mar 4 18:06:43 KST 2013 armv7l GNU/Linux

Remark that my dual-core mobile has more power than my Raspberry Pi...

Storage
df 2>/dev/null Filesystem            Size   Used   Free   Blksize /                    1.18G   996M   211M   4096 /dev                  402M   112K   401M   4096 /dev/shm              402M  0.00K   402M   4096 /system              1.15G   972M   208M   4096 /storage/sdcard0     1.08G   996M   111M   4096 /storage/extSdCard   14.8G  11.0G  3.78G   32768

Top
top Mem: 802212K used, 21148K free, 0K shrd, 28696K buff, 158356K cached CPU0: 0.0% usr 50.0% sys  0.0% nic 50.0% idle  0.0% io  0.0% irq  0.0% sirq CPU1: 0.0% usr  0.0% sys  0.0% nic  100% idle  0.0% io  0.0% irq  0.0% sirq Load average: 24.42 12.60 6.26 1/1301 21656

PID PPID USER     STAT   VSZ %VSZ CPU %CPU COMMAND 18828    2 root     SW       0  0.0   0  9.3 [kworker/u:3] 2020 1667 system   S     640m 79.5   0  1.8 system_server 2219 1667 radio    S     506m 62.9   0  0.9 {m.android.phone} com.android.phon 20475    2 root     SW       0  0.0   0  0.9 [kworker/u:0] 1687 1680 root     S    45604  5.5   0  0.4 /system/bin/admsrv 1659    1 radio    S    12124  1.4   0  0.4 /system/bin/rild 14849 1667 u0_a126  S     486m 60.4   0  0.3 {LocationService} com.google.andro 20537    2 root     SW       0  0.0   0  0.3 [kworker/0:0] 3737 1667 u0_a144  S     524m 65.1   0  0.2 {.mobilesecurity} com.avast.androi 21460 15736 u0_a146 R     1308  0.1   0  0.2 top

Cleanup after installation
The KBOX2 Linux emulator is installed as an option into the Terminal Emulator for Android.

Especially when you are short of RAM you can easily gain 12,5 MB of (temporary) installer files once the emulator has been installed and is working fine.

If needed later, you can perform a fresh installation. Remember to save your user & data files on an external SD card.


 * Deinstall OneBox Package Manager


 * Remove directory /storage/sdcard0/com.monami_ya.onebox.manager (and kbox2-base-installer) via My Files

cd /data/data/jackpal.androidterm ls -l drwxrwx--x   2 u0_a146  u0_a146       4096 Apr 10 19:43 app_HOME drwxrwx--x   3 u0_a146  u0_a146       4096 Apr 11 21:00 cache drwxrwxr-x   2 u0_a146  u0_a146       4096 May  1  2013 installer-image drwxrwxr-x  10 u0_a146  u0_a146       4096 Apr 10 23:39 kbox2 -rwxr-xr-x   1 u0_a146  u0_a146    2714309 Apr 10 19:54 kbox2-base-installer drwxr-xr-x   2 system   system        4096 Apr 10 19:40 lib -rwxr-xr-x   1 u0_a146  u0_a146        142 Apr 26  2013 setup drwxrwx--x   2 u0_a146  u0_a146       4096 Apr 10 20:22 shared_prefs
 * Remove remaining installer files:

df 2>/dev/null Filesystem            Size   Used   Free   Blksize /storage/sdcard0     1.08G  1.04G  45.8M   4096

rm -rf installer-image/ setup kbox2-base-installer

df 2>/dev/null Filesystem            Size   Used   Free   Blksize /storage/sdcard0     1.08G  1.03G  56.0M   4096

Set the execution path
If you want to add user executables, you might need to change the exeuction path. Remember that the SD card cannot be used for bin files.

PATH='/bin:/sbin:/usr/bin:/usr/local/bin:/android_root/system/bin' vi /etc/profile ... PATH&#61;"${PATH}:/home/kbox/bin" cd /storage/extSdCard/kbox


 * Add the double quotes, or you would not be able to login any more!
 * /sbin and /usr/bin are symbolic links to /bin

Access the Android file system
ls /android_root acct                      init.samsung.rc            res cache                     init.samsung.usb.rc        root config                    init.samsungskomer.rc      sbin d                         init.samsungskomer.usb.rc  sdcard data                      init.trace.rc              storage default.prop              init.usb.rc                sys dev                       lib                        system efs                       lpm.rc                     tmp etc                       mnt                        ueventd.goldfish.rc fstab.samsungskomer        modemfs                    ueventd.rc init                       preload                    ueventd.samsung.rc init.bt.rc                 prerecovery.rc             ueventd.samsungskomer.rc init.goldfish.rc           proc                       var init.rc                   recovery.rc                vendor

Install additional packages
Additional packages can be installed using dpkg. You can download installation packages from KBOX2 or KBOX3 downloads using wget.

Download install and run
An example: cd /storage/extSdCard/Download1 wget http://kevinboone.net/file-5.22_kbox3.deb dpkg -i file-5.22_kbox3.deb

file file-5.22_kbox3.deb file-5.22_kbox3.deb: Debian binary package (format 2.0)


 * 1) Or /sdcard/Download

Telnet daemon

 * Install kbox-login and utelnetd from KBOX2 download using dpkg.


 * Manually run the utelnetd daemon from the console.
 * Telnet via e.g. PuTTY

Remember to use port 10023 (no root access): telnet 192.168.1.41 Trying 192.168.1.41... telnet: Unable to connect to remote host: Connection refused

telnet 192.168.1.41 10023

Compile native ARM programs
Use gcc compiler; see download.


 * Requires 100 MB of RAM storage.
 * Currenlty only version 4.7 is supported

SSH client and server
Prerequite: kbox-login wget http://kevinboone.net/dropbear_0.52_kbox.deb dpkg -i dropbear_0.52_kbox.deb

Provides the client commands ssh and scp, and the /bin/ssh_daemon.sh ssh daemon.

To login to a remote system:

ssh ruser@...

To start the ssh server: /bin/ssh_daemon.sh &

To login: ssh -p 10022 ruser@192.168.1.41

Coreutils package
The coreutils package provides Linux commands with more options than the standard BusyBox versions.

Other packages
Other packages you can download from KBOX2 or KBOX3 downloads.

More packages exist... see http://kevinboone.net

Security
When running daemon programs, you can better stop the emulator before leaving your intranet...

KBOX and Android versions

 * 1. The KBOX2 manual installation script works with other terminal emulators like ConnectBot; you need to install in directory /data/data/org.connectbot


 * 2. Android Lollipop (5.0.2) fails to run the KBOX3 emulator:

/data/data/jackpal.androidterm/kbox3/bin/kbox_shell fakechroot: dlopen: undefined symbol: dlopen

After the standard installation you need to manually overwrite libfakechroot.so. Copy http://www.kevinboone.net/libfakechroot_fix_dlopen_1.so to /sdcard/Download

cd /sdcard/Download cat libfakechroot_fix_dlopen_1.so > /data/data/jackpal.androidterm/kbox3/lib/libfakechroot.so


 * 3. The KBOX2 manual installation does not work on Android 5 since only position independent (PIE) executables are allowed...


 * 4. Android 5 does not allow terminal emulators to write to external SD card storage /storage/extSdCard. Reading is not a problem. (you could use the Android or Windows explorer program instead to transfer files from /sdcard)

Uninstalling the terminal emulator
Uninstalling the Terminal Emulator for Android will also remove the KBOX2 and any of its data files without any warning. Therefore it is adviced to use an (external) SD card for storage. Do not use /home/kbox to store data or program sources.

No privileged commands
No root access, no SUID, no system ports.

Consequence: any linux or network tool requiring root privileges does not work.

ping www.pi.be ping: permission denied (are you root?) Use the built-in Android ping command: which ping result: /bin/ping
 * ping, tcpdump, route, etc. do not work

rm /bin/ping which ping result: /android_root/system/bin/ping route add -net 192.168.0.0/16 dev wlan0 route: ioctl 0x890b failed: Operation not permitted (no solution here) Use higher (non privileged) port numbers.
 * route does not work
 * TCP/IP ports below 1024 cannot be used by user (root required)

SD card
The external SD card is a vfat file system. Therefore exec rights are not available; so you can only store executable programs in RAM.

Slow response
Sometimes slow response: Might be caused by too few free RAM.
 * Radio streaming interrupted
 * Console commands slowly responding (command entry echo; execution delay)

Telnet session fails
Timeout?
 * Login again...

Network commands fail
telnet 192.168.1.47 telnet: can't connect to remote host (192.168.1.47): No route to host

route -n Kernel IP routing table Destination    Gateway         Genmask         Flags Metric Ref    Use Iface 0.0.0.0        192.168.1.1     0.0.0.0         UG    0      0        0 wlan0 0.0.0.0        192.168.1.1     0.0.0.0         UG    383    0        0 wlan0 192.168.1.0    0.0.0.0         255.255.255.0   U     383    0        0 wlan0 192.168.1.1    0.0.0.0         255.255.255.255 UH    0      0        0 wlan0 192.168.1.30   192.168.1.1     255.255.255.255 UGH   0      0        0 wlan0


 * Why is 192.168.1.0/24 not working?
 * Why is there a 192.168.1.30/32?

Routing problems which cannot be manually solved, because route requires root access.

Symbolic links not supported
ln -s a b ln: b: Function not implemented Symbolic links not allowed on the vfat file system: mount |grep extSdCard /dev/block/vold/179:97 /storage/extSdCard vfat rw,dirsync,nosuid,nodev,noexec,noatime,nodiratime,uid=1000,gid=1023,fmask=0002,dmask=0002,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0