Installing Linux on an Android device
Your Android phone is already powered by the goodness of Linux, but you can enhance it further and make better use of its multi-core processors and oodles of RAM by running a full-blown Linux distro alongside the existing mobile OS.
We can think of many reasons why you'd want a proper Linux PC in your pocket. You can, for example, use it to power a LAMP server that can run web apps and serve web pages. If you're a network admin, you can install your favourite Linux tools and turn the smartphone into a portable network troubleshooting or pen-testing device.
The LinuxOnAndroid project produces an Android app and a couple of shell scripts, and hosts a bunch of Linux distros that you can boot using the app. In a snap, the scripts mount the Linux image within the Android filesystem and the SD card within the Linux filesystem.
They then call on chroot to change the root directory to that of the mounted Linux and open up a shell for you to interact with the mounted Linux system. The scripts also set up SSH for secured remote access, along with VNC to allow you to access this Linux system's graphical desktop.
The scripts only prepare the environment for Linux to run on the device. The Linux image files hosted by the project are just customised Linux environments packaged by the project developers to suit different use-cases and devices.
The important bit is that all the Linux distros offered by the project are made up of ARM packages, and instead of running in a virtual machine they run on the real hardware on your Android smartphone. This is why you can run it on the measliest of devices.
We've managed to run Arch Linux along with the Enlightenment desktop on a Samsung phone with a single-core 1GHz processor and about 400MB RAM.
At the time of writing, the project has stable images for Arch Linux, Debian Lenny, Debian Testing, Fedora 20, Fedora 19, Kali Linux, Ubuntu 13.10, Ubuntu 13.04, and Ubuntu 12.04 LTS.
There are also Alpha images that aren't meant for production use for other distros, including Slackware, Bodhi, OpenSUSE and Ubuntu 14.04. Each of these distros is available in multiple editions.
The Core image is the smallest in size, and includes the minimum set of packages you need to run the distro. This is ideal for creating your own images and include the Openbox window manager.
Next there's the Small image, which ships with the LXDE desktop and its suite of programs. Finally you have the Large image, which includes the KDE Plasma Desktop, LibreOffice and Gimp.
Some distros have additional images as well. Arch Linux produces images with the Enlightenment desktop, and Fedora ships an image with the MATE desktop. You can grab the images with either the ext2 or ext4 filesystem.
The ext4 images are compatible with Android 4.3 while the ext2 images are compatible with earlier versions of Android.
Prep the device
Start by grabbing the Complete Linux Installer app from Google's Play Store. Alternatively you can head to the LinuxOnAndroid project's website and download the open source version of the Android app (but go into the Settings > Security menu and toggle Unknown Sources to enable installation of non-Play Store apps).
The app requires a rooted Android device. The exact procedure necessary to root an Android device varies between models.
Besides the app produced by the LinuxOnAndroid project, you'll also need a VNC viewer to use the graphical desktop.
The project suggests using the popular Android VNC Viewer app. You'll need another app to access Android's built-in command line, but you'll already have this if you've rooted your device. In any case, the project suggests using the Android Terminal Emulator app.
Once you've grabbed these, you can launch the CompleteLinuxInstaller app. Tap on the project's icon in the top-left corner of the screen to reveal the navigation menu, which contains several items. The first two are the most important: Install Guides will take you through the steps required for setting up and installing the various supported Linux distros, and the Launch item is the integrated launcher that you can use to boot into the configured distro.
The other items will bring up various information and tips to assist you. To begin the process of setting up a distro, launch the CompleteLinuxInstaller app, bring up the navigation menu and tap on the Install Guides item. This will bring up a list of distros that you can run on your device. Once you've decided which distro to run, tap on its entry for further instructions.
For this tutorial, let's assume you want to set up Ubuntu. However, note that the process to set up a distro is pretty much similar for all.
Setting up a Linux distro
Once you've selected a distro, the app will display detailed instructions broken down into four pages. On Page 1 the app runs through the basic requirements your device must meet to be able to run a Linux distro. The most important step is to make sure that you've enabled the debugging support.
To do that, head to Settings > Applications > Development and make sure the USB Debugging setting is toggled. On the seond page you get links to download the image for the selected distro.
Tapping on the Download Image button pops up a window with three additional buttons to download one of the three supported image types explained earlier – Large, Small or Core.
The page also mentions details about the offered images, such as the size of the compressed image that you'll download as well as the size the image will take on your SD card once it's been extracted.
Tapping on the image you wish to download brings up another pop up with buttons that'll either download the image from a Sourceforge mirror or via torrent. If you choose the torrent option, the app will download a torrent file, which you'll then have to feed to a torrent client to download the actual image for your selected distro.
You can download the image on a computer and then transfer it on to the SD card on your Android device. In this case, you can safely skip the instructions on this page.
You can uncompress the downloaded file either on the computer or on the device itself. For the latter, you'll need a versatile file manager such as the paid-for Root Explorer app or the free ES File Explorer app.
While compressed files are smaller and are faster to transfer than uncompressed ones, remember that they are tightly compressed and (depending on the specs of your Android device) can take some time to deflate.
However, if you're moving the images on to your device manually, it's best to house them inside a folder. For example, if you're downloading the image for the Ubuntu distro, it's best to uncompress it inside a folder called Ubuntu on your SD card.
Then move on to Page 3, which advises you on extracting the downloaded files. The rest of the page talks about how to boot the extracted images. Once booted, you'll be dropped to a terminal window. Page 4 lists instructions on how to connect to this running Linux installation via a VNC viewer. Remember to note the password listed on this page as you'll need it to connect to the VNC server running inside your distro.
Boot the distro
You're now all set to boot the new distro. Bring up the navigation menu by tapping on the app's icon or swiping from left to right and tapping on the Launch item. This will bring up the app's launcher which is responsible for booting up the distros. Use the pull-down menu on the page to select the distro you wish to boot.
If you get an error saying that the image for the selected distro doesn't exist, it means that the app can't locate the extracted image files for the distro. This can happen if you haven't extracted the downloaded file, or if you've kept it in a non-standard location. In such a case, you'll have to point to the IMG file of the distro manually.
Tap on Settings in the top-right corner inside the Launch screen which brings up a pull-down menu. Tap on Add to open the page to add an entry for your distro. Enter the name for the distro in the space provided and tap on the box with three dots to navigate the filesystem on your Android device and point to the IMG file for the distro. Tap on Save Changes when you're done.
This custom entry from your distro will now be listed in the pull-down menu on the Launch page. When you now select the entry, the app will show you a button to start the distro.
Tap on the button to boot the distro. This will launch the terminal app and grant it super user permission. Press the Enter key in the virtual keyboard to boot the distro. Since this is the first time you've started up the distro, you'll be asked to specify a password for the default user (which in the case of the Ubuntu distro is named ubuntu).
Note that this is the password for the default user account and not the password required to log into the VNC session.
You'll then be asked if you want the distro to start the VNC server (for viewing the graphical desktop) and the SSH server (for accessing the distro remotely over a secure connection).
We suggest you start them both to reap the full benefit of running a Linux distro on your Android device. Next, you'll be asked to enter the screen resolution of the VNC session. Although you can set this to any size, for best experience you should set it to the same resolution as your device.
Remember, however, that when you bring up the virtual keyboard it will hide a part of your desktop. If you are running this on a device with a big screen you can follow the guide on the project's wiki to adjust the screen size to accommodate the soft keys.
That's all the configuration it requires. The app will then prompt you to save the settings as default. It's safe to do so to avoid answering the same questions every time you launch the distro. We'll show you how to alter these settings a little later on the tutorial. For now you should just save the settings as default and let the app boot your distro.
When it's done booting your distro, you'll see a note with the relevant settings you need to connect via VNC to this Linux distro, followed by the standard Linux root prompt. If you're proficient with the Linux command line interface, you can now use this shell to interact with the distro like any desktop distro.
For security purposes, one of the first things you should do is set a new password for the root user. Enter
passwd
in the shell and enter a new password for the root user. If you've asked LinuxOnAndroid to start the SSH server while booting your distro, you can now connect to it from any computer on the network. Enter the
ifconfig
command inside the terminal on your Android device, which will print the IP address of the device, such as 192.168.2.101. To connect to the device launch a terminal on another computer and enter
ssh ubuntu@192.168.2.101
This will prompt you for the password for the ubuntu user which is the one you set when you first booted the distro. To view the graphical desktop running on top of your Linux distro on the Android device, tap on the home button to minimise the terminal app, then tap and launch the VNC app (such as androidVNC).
The VNC app will prompt you for various settings so it can connect to the distro running on the device. Enter localhost in the field that asks you for the IP address of the VNC server and 5900 as the port. In addition to these you'll also need a password to authenticate with the VNC server.
This varies from distro to distro. For the Ubuntu images, the password is ubuntu. For Arch Linux it's archlinux, for the Debian flavours it's debian and so on. The password is listed on the distro's wiki page on the LinuxOnAndroid project's website.
Note that if you are using androidVNC, the app suggests that you change the colour format to 24-bit color (4bpp). Tap the Connect button to initiate the VNC session once you've entered all the information and hey presto! The VNC client will connect to your device and display the graphical desktop that's running inside the distro you're currently running.
Again, the app developers advise androidVNC users to change the input mode to touchpad. To do so, tap on the menu button from inside the VNC session. This will bring up a bunch of options; you need to tap on Input Mode and then select the Touchpad radio button from the list of supported input modes.
You can now interact with and operate the desktop just like you would on a PC. On the androidVNC app, a single tap equates to a left-click. Tap twice to simulate a right-click. If you tap and hold, the app will display three buttons – two to zoom in and out, and one to display the virtual keyboard.
You can also use the package manager to install new apps. Remember, however, that the distro will only be able to install apps that have been ported for the ARM architecture, which is true for almost every popular app.
When you've finished, close the VNC session by logging out of the desktop. Now pull down the app drawer and tap on the terminal session that's been running in the background.
Type exit to shut down the Linux distro. When the distro shuts down you'll have to type the exit command a couple more times to exit and close the terminal session.
Advanced tweaks
You can use the steps described above to download, set up, boot and use any of the supported distros on your device.
However, the app also offers some other options to further customise the distros and tweak your user experience. For starters, you may wish to change the settings of the distro you specified when you first booted into it.
To do so, select the distro you wish to customise from the drop-down list in the Launch section. In addition to the Start Linux button, you'll also see a Configure Linux button. Tap on it to bring up the settings page. Here you'll find checkboxes for enabling and disabling the start of the SSH and VNC servers.
You can also modify the resolution of the VNC screen by altering the values listed on this page. The page offers a couple of interesting options, including the Create 1GB SWAP File checkbox. As you can imagine, when this is toggled, the app will create a swap file for the distro to use.
Make sure you have adequate free space on your SD card before you enable this option. The app can also mount folders from the Android filesystem into the running Linux. Tap on the Configure Mounts button to launch the mount editor. Tap on Options in the top-right corner and then on the Add Mount option.
This pops up a page where you'll have to specify the location of the Android folder you wish to share manually (such as /storage/sdcard0/Downloads), followed by the path where you want to mount it (such as /home/ubuntu/Downloads).
You can add as many folders as you wish. When you're done tap on the Save Changes button and exit the mount editor. When you now boot into the distro, LinuxOnAndroid will mount the listed Android folders in the specified locations inside the distro.
Depending on how you use the distro, sooner or later you'll run out of space inside the image. In such a case you'll have to make a larger image and copy over all the files and folders from inside the existing image to the new one.
Start by firing up a terminal and creating a blank image of the size you want. For example, the command
dd if=/dev/zero of=ubuntuNEW.img bs=1M count=0 seek=4096
will create a 4GB image called ubuntuNEW.img. Then you'll need to format this new image and create a filesystem.
The command
mke2fs -F ubuntuNEW.img
should do the trick. Then copy the image file that you want to inflate from the device into your computer. Let's call it ubuntuOLD.img. Now create two folders on your desktop (ubuntuNEW and ubuntuOLD) to mount these images. The commands
sudo mount -o loop ubuntuOLD.img ubuntuOLD/
and
sudo mount -o loop ubuntuNEW.img ubuntuNEW
will mount the images into their respective folders. Once that's done, copy over all the files from the old image to the new image. The command
sudo cp -rp ubuntuOLD/* ubuntuNEW
will make sure the files and folders are copied along with their ownership permissions. Round up the process by simply unmounting the images with
sudo umount ubuntuOLD
and
sudo umount ubuntuNEW
You can now transfer the ubuntuNEW.img file to your phone, remove the existing image and make sure the distro's entry in the Launch screen points to the new image.
Run Linux without rooting your device
You can run Linux even on an unrooted Android device thanks to the GNURoot app. Despite its name, this app doesn't require a rooted phone. The one major difference between CompleteLinuxInstaller and GNURoot is that the latter only gives you access to the command line interface of the Linux distro.
To run Linux on your Android device via GNURoot, you'll need to grab it from the Google Play Store, as well as a companion app for the specific Linux distro. The app produces companion apps for popular distros, such as Debian, Gentoo, and Fedora.
Remember that these apps are close to 100MB in size, unlike the GNURoot app, which is a just a few KB. Once you have them both installed, fire up GNURoot and use the drop-down menu at the top of the page to select the distro you wish to boot.
Make sure you have already downloaded its companion app. Then tap on the Create New Rootfs button to install the distro which will take some time depending on the resources on your device. When it's done, tap the Launch Rootfs button which will open a terminal and log you in the distro.
If you select the Launch as Fake Root checkbox before launching the distro, you'll be logged in with superuser privileges.
- Enjoyed this article? Expand your knowledge of Linux, get more from your code, and discover the latest open source developments inside Linux Format. Read our sampler today and take advantage of the offer inside.
from Techradar - All the latest technology news