How to create a Linux Server on your Old Android Phone with Termux

photo

A story about a little phone...

This is the story of my Moto G 1st Gen. I got it way past its hey-day in February 2016 but it was my first foray into the world of Android (yeah, I know, really late) and I really had a fondness for it. That little phone kicked its way into 2017 but alas, it started to hold me down so I finally put it away. Up until last week, it was put away, forgotten. But now it has a second chance! Right now that phone is chugging along, hosting the bot I made from my last post and off my (very generous Portuguese) friend Diogo's home Linux server.

Now it's local I don't need to worry about stuff like timezones and I can keep a very close eye on it.

Enter Termux [to save the day!]

This powerful Android application turns your phone into a mean, lean, Linux machine by providing a powerful yet lightweight terminal. The app can be found on the Google PlayStore and all it needs is a simple install, that's it! To host my Telegram bot on the Moto G, first I installed the app, a few convenient add-ons, set up SSH, installed Python3 and boom, it came alive. Now I'll cover those steps in not-so-few-words.

Requirements

  • Android 5.0 and up
  • Internet Connection
  • 'Bout 30 minutes

Yep that's it

; )

Getting Setup with The Core Application

OK so after a quick visit to the Google PlayStore and install of Termux, you'll be presented with a command-line interface (CLI) upon starting Termux. Now just a few things to get going.

  • Tap the screen to get the keyboard up, hold down on the volume up button on your phone and tap the letter 'q' on the keyboard. You'll see a couple keys pop up above your keyboard like ESC, CTRL, etc. These will be useful when you're typing up commands from your phone.
  • Type pkg install coreutils. This allows for commands like dir to be used to view the contents of directories
  • Type termux-setup-storage which will allow you to copy files to and from your local phone storage

 

A few things to note

Although Termux does give you a very good and workable Linux terminal, it's not completely the same and this is very keenly seen in the directory structure. Here are a few directory locations to know about that will make your life easier

  • $PREFIX/etc  --same as in Linux
  • $PREFIX/var  --same as in Linux
  • $HOME   -- home location for Termux

More info on this can be found at https://wiki.termux.com/wiki/Differences_from_Linux

Handy phone shortcuts

  • Volume up + a   -- move cursor to left
  • Volume up + d  --move cursor to right
  • Volume down + a   --move cursor to beginning of line
  • Volume down + e  -- move cursor to end of line

More can be found at  https://termux.com/touch-keyboard.html

SSSSHHHHHHH I'm typing something...

Quick note on SSH (secure shell): If you don't know what this is, it's essentially a means of using your Linux system's interface remotely through a terminal screen. If you're on Windows, you'd connect to a Linux device using PuTTY which is an SSH client you can find here

To get SSH up and running can be a cinch but you have to follow my steps without questioning them. I'll explain what each does but you won't fully get it until you've done it yourself like twice (this is if you're new to this stuff, if you're an advanced Linux user then you'll have an easier time)

Ok, let's begin

#pkg install openssh      --this is the service we'll be using for hosting an SSH server on our phone, when you're prompted as to if you'd like to install it type 'yes' and press enter
#ssh-keygen     --for all the prompts you get here just press enter 
#cd $HOME/.ssh    --navigates to the directory that holds your ssh key files which you'll need to connect to it from other devices
#cat id_rsa.pub >> authorized_keys   -- this puts the public key file into a list of authorized keys by the device, need this for making external connections
#chmod 700 $HOME/.ssh/*    --grants admin privileges to the folder holding the key file, necessary for making external connections
#sshd    --This starts the SSH service, you need this running so you can connect to your device


Accessing from Windows

Ok so to get set up with it on Windows this is what you'll have to have in place:

  • A USB cable to send files to and from your phone
  • PuTTY and PuTTYgen, which can both be found at the following URL: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

 

Now back to the phone

#cd $HOME/.ssh     -- takes us back to the directory where the key files are located, just in case you've lost your way
#cp id_rsa /sdcard  -- this copies the private key file from Termux to your phone's local storage 

SPEED ROUND

 

OK now I'm going to speed through the rest of this because it's back to comfy Windows. First you copy the id_rsa file to your Windows computer, load it into PuTTYgen and save it as a PPK. Open PuTTY and go into Connection>>SSH >>Auth and load it in. Enter your device's IP address (which you can find by typing in ifconfig in the Termux shell) and for the port number, enter 8022. After that, start the connection, accept any prompts and for the username just press enter (Termux doesn't allow username-password logins as of yet)

SSssssssss

Now let's not forget our very lovely friend Python and to get that up all I did was use the command pkg install python which defaults to Python3 and boom that was it, I could host my Telegram bot

Phew

Yep, you're finally done and you feel that huge sense of achievement. You're welcome. Thanks again for visiting my blog and I hope I see you again next week!

 

 

; )

One thought on “How to create a Linux Server on your Old Android Phone with Termux”

Leave a Reply

Your email address will not be published. Required fields are marked *