Tuesday, March 17, 2015

Old phone as SSH server

I guess I'm on a kick trying to actually use my pile of old broken-down smart phones.

This time, while I was at work one day and was wanting to access my home network, I was wishing I had a linux machine at home that I could ssh into. My laptop at home is linux, but I don't leave it turned on. The home server is Windows, because it's also my media center, and sadly, Windows Media Center is still the best option for a media center with DVR. I wanted to run a small, light linux machine alongside it, just to be able to get in via ssh from outside, and do whatever from there.

I had a raspberry pi at one point, which would have been perfect, but I sold it. And for things like this, part of the fun is to do it without buying more stuff. So I decided not to buy another pi. I thought about just running a linux VM on the windows machine, but it's already a bit underpowered for what it does (it gets a little slow when watching an HD recording while also simultaneously recording 2 HD shows).

So I dug around in my pile of old phones, and decided to turn one into a proper ubuntu machine. My first attempt was with a Galaxy S. I installed a fairly straightforward chroot'd linux on it, and it was SLOW. I tried doing an apt-get install of mysql, and I gave up after 15 or 20 minutes of waiting during the installation. That phone went back in the bin.

So I pulled out the LG Optimus G E970. It's more-or-less the same hardware as a Nexus 4, so quite a bit better than the old Galaxy S. Unfortunately, it has a busted screen -- the screen turns on and displays properly, but the touchscreen is completely broken. And I didn't have adb/USB debugging enabled before it broke, which has previously foiled any of my plans to do anything with it. But I decided that this time, that wouldn't stop me.

First, I tried the easy solution, and plugged in a USB OTG adapter, which, if a phone supports it, lets it act as a USB host, so I could hopefully just plug a USB mouse in. Nope, no dice. No OTG support.  I have a bluetooth mouse, but how to get bluetooth turned on, and the mouse paired, without USB debugging enabled?

Well, here's what I finally did (with quite a few other mis-steps along the way that I won't bother documenting):

  1. Since the power and volume keys still worked, I could reboot into recovery mode, and work through those menus. So I wiped the current install, and flashed a current version of SlimRom. SlimRom, as it turns out, has USB debugging enabled out of the box.
  2. To disable the lockscreen from adb, I needed root access, which I didn't have. But I read that if you connect adb while in recovery mode, you have full root. So I rebooted again into recovery, connected adb, and thanks to the tips from this post, pulled the /data/data/com.android.providers.settings/databases/settings.db sqlite database off the phone onto my desktop (using the adb pull command), disabled the lockscreen by running the query update secure set value=1 where name='lockscreen.disabled'; and then pushed the file back onto the device. Once rebooted, the lockscreen was disabled.
  3. Next was the tricky part -- blindly stabbing in the dark with adb shell input commands to try to turn on bluetooth and pair with my bluetooth mouse. It took a bit of trial and error, but the solution for my particular phone ended up being:
#swipe down from top:
./adb shell input swipe 100 0 800 100

#tap top right to flip to the tiles settings screen:
./adb shell input tap 750 10

#hit the settings icon:
./adb shell input tap 720 150

#bluetooth toggle on:
./adb shell input tap 720 250

#tap the bluetooth menu:
./adb shell input tap 520 250

#search for devices
./adb shell input tap 600 40

#select the mouse
./adb shell input tap 200 896

After that, my mouse was working! Then it was pretty easy to attach a bluetooth keyboard, install the chrooted linux (the easiest method is to download the Complete Linux Installer from the play store, although there are plenty of bugs in it. If you are playing along at home, here's the bugs I ran into, and their solutions:

  • Clicking "Start Linux" loads the terminal emulator, but nothing happens OR
  • Clicking "Start Linux" loads the terminal emulator, it does a cd and su, and then nothing happens.
Something is broken with the launcher. You need to run the boot script manually. First su, then do sh /data/data/com.zpwebsites.linuxonandroid/bootscript.sh /sdcard/ubuntu/ubuntu.img (adjusting that last bit to wherever you put the linux image)

  • You can't add users (and various apt-get installs also fail because THEY can't add users)
Something in Android's SELinux implementation is breaking things. In a separate terminal, as root, run setenforce 0, which will disable whatever is preventing it, and probably render your device horribly insecure. Oh well.

Once those things got sorted out, I was able to SSH into the phone, and it works just fine as an ubuntu server. It's a tiny bit slow, but nothing unreasonable. It's currently plugged into the media center to stay charged, and a port forwarded on my router to it, so I can access it from anywhere! 

Mission accomplished: one more half-broken phone from my bin has a use!

1 comment:

Nathan Tolbert said...

Turns out there was one more problem: I couldn't resolve any DNS lookups as the regular user, only as root.

This page has the fix: