Linux Server on an Android Phone

| 3 min read

A little while ago I wanted to set up a small home server to host some pages. I could have just used some free web hosting but where is the fun in that? So I checked what old devices I have I could repurpose. The first obvious choice was my old HP laptop. It had a tiny bit of a problem: its fans are super loud, I didn't want to listen to them all day.

Then it downed on me: I just bought a new Android phone in November, my previous Sony Z3C can serve as the server! Smartphones of today have way more power than servers in the 90s so it should be able to handle the task, and I might also set up PHP for Wordpress.

There was only a single problem: the touchscreen is broken and doesn't register half of the taps on the screen. It turns out this is an easy problem to solve: scrcpy lets you view the screen of your phone on your computer and also simulates clicks. What is even better, you can use your laptop keyboard to simulate a hardware keyboard for the phone. This will come immensely useful later, as we will have to type quite a few commands.

To use scrcpy, you need to enable USB debugging on the phone. Luckily, I'm an android developer and it was already turned on. Otherwise I would have a hard time setting it, since the touchscreen doesn't work properly.

Scrcpy attached to the phone

Setting up "Ubuntu"

So I can control the device again! Next step is to set up the server. I wanted to make the phone more than a simple webserver, for that I could have downloaded an app. I wanted it to have SSH and the ability to run any (ok, most) Linux programs. After a bit of research there seemed to be two ways: either with or without rooting. I did not want to root the phone as it causes a factory reset, and I'm afraid I won't be able to turn the developer options on again.

So, no-root solutions remain. The basis of the setup is Termux. It is an amazing project: they made a fully functional terminal emulator for Android. What is amazing, they have a repository of packages that were all patched and rebuilt for Android. I could have stopped here, they have Apache, PHP, python, all the goodies I needed. But why not extend the set of available packages to all ARM Linux packages?

Termux loaded

Android is very far from a real Linux OS, especially if you don't have root. The biggest problem is the file system: there are no /usr,/var,/proc folders, the standard libc library is different,…. To be able to run packages built for Ubuntu, one has to fix all of these, plus library loading paths, etc. If you have root access, you can call chroot and set up a contained file system, it is a good starting point. Unfortunately, I do not have root.

Here comes proot into the picture: it is like chroot, without the need of root access. Basically, it runs your application via ptrace and dynamically replaces system calls. I wonder if it has any performance implications but I haven't done any tests yet.

Having proot, we can easily simulate a filesystem, change the user to root, setup a home dir. I chose the easier path and didn't do it all by myself, instead used the Andronix Ubuntu distributions. It needs a single command executed in Termux, and takes care everything itself. When finished, you have a fully simulated Ubuntu on your phone! It can run even X11 apps though I haven't tried it. You can run apt get to install packages like you would do on a real computer.

Termux loaded

No dozing please

One thing that was left to do: disabling android battery optimization on Termux. By default, Android throttles background apps and that's not what you want from your server. Fortunately, this is a single checkbox in the Settings menu.

Final steps

After everything set up, let's try the simple HTTP server from python:

> python3 -m http.server 8000

And the results:

Ubuntu installed