Ubuntu Trusty 14.04 on your Android

Linux have a set of many nice applications that could fit and run on your android device.

You can install ubuntu trusty on your android by creating an special partition on your external sdcard and installing the base system there. ** Your phone must be rooted ** preferable with cyanogenmod 11.

1. Formating the card

I recommend to use an sdcard with at least 2Gb free (4 recommended) to install ubuntu. You need to be aware that you will be formatting and repartitioning your card, DON’T FORGET TO BACKUP ANYTHING THERE. ALL THE DATA ON THAT DISK WILL BE LOST.

All this procedure is executed as root.

The first step is to connect the card and make an dmesg to identify which /dev entry is the card…

dmesg will output something like:

[73331.858224] sd 110:0:0:0: [sdd] 62676992 512-byte logical blocks: (32.0 GB/29.8 GiB)
[73331.859476] sd 110:0:0:0: [sdd] Write Protect is off
[73331.859487] sd 110:0:0:0: [sdd] Mode Sense: 03 00 00 00
[73331.860709] sd 110:0:0:0: [sdd] No Caching mode page found
[73331.860721] sd 110:0:0:0: [sdd] Assuming drive cache: write through
[73331.864708] sd 110:0:0:0: [sdd] No Caching mode page found
[73331.864719] sd 110:0:0:0: [sdd] Assuming drive cache: write through
[73331.866241] sdd: sdd1
[73331.869340] sd 110:0:0:0: [sdd] No Caching mode page found
[73331.869348] sd 110:0:0:0: [sdd] Assuming drive cache: write through
[73331.869355] sd 110:0:0:0: [sdd] Attached SCSI removable disk

All you need to do is to identify the device (in this case sdd) and wipe all partitions there:


[root@ghost ~]# fdisk /dev/sdd


Welcome to fdisk (util-linux 2.24.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p
Disk /dev/sdd: 29.9 GiB, 32090619904 bytes, 62676992 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000


Device Boot Start End Blocks Id System
/dev/sdd1 2048 62676991 22948864 83 Linux

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

And now, you are enabled to create two partitions, one of them for ubuntu, the other one for your /storage/sdcard1 (external sdcard)

open fdisk:

[root@ghost ~]# fdisk /dev/sdd
Welcome to fdisk (util-linux 2.24.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Create a new partition for ubuntu (in this case, 2G):

Command (m for help): n


Partition type:
p   primary (0 primary, 0 extended, 4 free)
e   extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-62676991, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-62676991, default 62676991): +2G

Created a new partition 1 of type 'Linux' and of size 2 GiB.

Create a new partition for data:

Command (m for help): n
Partition type:
p   primary (1 primary, 0 extended, 3 free)
e   extended
Select (default p): p
Partition number (2-4, default 2):
First sector (4196352-62676991, default 4196352):
Last sector, +sectors or +size{K,M,G,T,P} (4196352-62676991, default 62676991):


Created a new partition 2 of type 'Linux' and of size 27.9 GiB.

Now, toggle this partition as FAT32… (today is not required)

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): c

If you have created or modified any DOS 6.x partitions, please see the fdisk documentation for additional information.
Changed type of partition 'Linux' to 'W95 FAT32 (LBA)'.

And then, you have the two partitions:

Command (m for help): p
Disk /dev/sdd: 29.9 GiB, 32090619904 bytes, 62676992 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device    Boot     Start       End   Blocks  Id System
/dev/sdd1           2048   4196351  2097152  83 Linux
/dev/sdd2        4196352  62676991 29240320   c W95 FAT32 (LBA)

Now, you should save the changes:

Command (m for help): w

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Now it’s done. The next step is to format the card for ubuntu:

[root@ghost ~]# mkfs.ext4 -L ubuntu /dev/sdd1
mke2fs 1.42.8 (20-Jun-2013)
Filesystem label=ubuntu
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912


Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

And then, format the other card for your data:

[root@ghost ~]# mkfs.vfat /dev/sdd2
mkfs.fat 3.0.26 (2014-03-07)

2. Mounting and preparing the device for the first stage.

You need to install debootstrap (apt-get install debootstrap  / yum install debootstrap), and then, you are able to introduce the armhf platform into your device

First step is to mount the partition:

[root@ghost ~]# mkdir /mnt/tmp
[root@ghost ~]# mount /dev/sdd1 /mnt/tmp

Then you are able to start the debootstrap process (first stage):
[root@ghost ~]# debootstrap --arch=armhf --variant=minbase --foreign trusty /mnt/tmp http://ports.ubuntu.com/ubuntu-ports
W: Cannot check Release signature; keyring file not available /usr/share/keyrings/ubuntu-archive-keyring.gpg
I: Retrieving Release
I: Retrieving Packages
I: Validating Packages
I: Resolving dependencies of required packages...
I: Resolving dependencies of base packages...
I: Found additional base dependencies: gcc-4.8-base gnupg gpgv libapt-pkg4.12 libreadline6 libstdc++6 libusb-0.1-4 readline-common ubuntu-keyring
I: Checking component main on http://ports.ubuntu.com/ubuntu-ports...
I: Retrieving adduser 3.113+nmu3ubuntu3
I: Validating adduser 3.113+nmu3ubuntu3
.
.
I: Extracting libsemanage1...
I: Extracting libsepol1...
I: Extracting libslang2...
.
.

Now you have the base system installed (stage 1), Then sync and umount :), it will take a while…

[root@ghost ~]# sync
[root@ghost ~]# umount /mnt/tmp

Now we are ready to grab your sdcard and put it on your phone.

3. Preparing ubuntu from the phone…

You need to enable the debugging mode on your phone, and then, you need to give root privileges to adb (Settings->Developer Options->Android Debugging->Selected) (Settings->Developer Options->Root Access->Apps and ADB)

Now, connect your phone using your microusb cable to your computer and use the adb tool (you must install it before).

[root@ghost ~]# adb shell
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
shell@ja3g:/ $ su
root@ja3g:/ #

Now we have to find where is ubuntu… All you need to do is:

root@ja3g:/ # mount | grep sdcard1
/dev/block/vold/179:10 /mnt/media_rw/sdcard1 vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1023,gid=1023,fmask=0007,dmask=0007,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
/dev/fuse /storage/sdcard1 fuse rw,nosuid,nodev,relatime,user_id=1023,group_id=1023,default_permissions,allow_other 0 0

And then, you know that scard1 was mounted from /dev/block/vold/179:10… Given that ubuntu is on the next lower partition, it should be on /dev/block/vold/179:9

And then execute the following commands:

cd /
mount -o rw,remount /
mkdir /ubuntu
mount -o ro,remount /
export MYROOT=/ubuntu
export SDCARD=/ubuntu
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH
export HOME=/root
mount -o exec,dev,suid -t ext4 /dev/block/vold/179:9 $SDCARD
for f in dev dev/pts proc sys ; do mount -o bind /$f $MYROOT/$f ; done
chroot $MYROOT /bin/bash -l

Now you are running ubuntu 14.04!

bash-4.3#

4. Second stage…

Now, from ubuntu running there, you need to start the second stage. That will install several applications.

bash-4.3# debootstrap/debootstrap --second-stage
I: Keyring file not available at /usr/share/keyrings/ubuntu-archive-keyring.gpg; switching to https mirror https://mirrors.kernel.org/debian
I: Installing core packages...
I: Unpacking required packages...
I: Unpacking adduser...
I: Unpacking base-files...
I: Unpacking base-passwd...
.
.
.

Now you have to logout from the chroot…

bash-4.3# exit

And then, remount some partitions:

cd /; for f in dev dev/pts proc sys ; do mount -o bind /$f $MYROOT/$f ; done

Then, you can log into ubuntu again:

root@ja3g:/ # chroot /ubuntu /bin/bash -l
root@localhost:/#

5. Preparing the system…

Let’s configure the ubuntu repositories, this step is very important because is mandatory if you want to install anything else trough apt-get

To create sources.list, execute:

echo 'deb http://ports.ubuntu.com/ubuntu-ports/ trusty main restricted universe multiverse' > /etc/apt/sources.list
echo 'deb http://ports.ubuntu.com/ubuntu-ports/ trusty-updates main restricted universe multiverse' >> /etc/apt/sources.list
echo 'deb http://ports.ubuntu.com/ubuntu-ports/ trusty-security main restricted universe multiverse' >> /etc/apt/sources.list

and then execute:

apt-get update

** DISCLAIMER: don’t use restricted repositories if you don’t know what you are doing.

6. Installing something…

Suppose that you wanna install nmap from your phone, you just have to:

apt-get install nmap

You can install almost anything… from proxies, webservers to databases… Even the GUI applications can run here. The only one limitation is that the android kernel is usually compiled with fewer options (Eg. loop device missing, or something like…)

7. Automounting and running it again…

If you are running CM11, you could use /data/local/userinit.d/ to create an startup for ubuntu. Then, if you wanna run something, you just have to execute fewer commands:

File /data/local/userinit.d/40ubuntu

#!/system/xbin/bash
cd /
mount -o rw,remount /
mkdir /kali
mount -o ro,remount /
export SDCARD=/kali
export ROOT=/kali
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH
export HOME=/root
mount -o exec,dev,suid -t ext4 /dev/block/vold/179:9 $SDCARD
for f in dev dev/pts proc sys ; do mount -o bind /$f $ROOT/$f ; done

And… don’t forget to execute:

chmod 700 /data/local/userinit.d/40ubuntu

And then, every time you wanna get into ubuntu, you should execute:

export SDCARD=/ubuntu
export ROOT=/ubuntu
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH
export HOME=/root
chroot $ROOT /bin/bash -l

Now enjoy your ubuntu 🙂

Leave a Reply