Skip to content

Instantly share code, notes, and snippets.

@spaceface777
Last active June 13, 2020 10:48
Show Gist options
  • Save spaceface777/6f438ea1760d278ff3f536eccab09876 to your computer and use it in GitHub Desktop.
Save spaceface777/6f438ea1760d278ff3f536eccab09876 to your computer and use it in GitHub Desktop.

Install Ubuntu chroot on Android

1. Install and open Termux

It's on the Play Store, this needs no further explanation lmao

Wakelock

In the Termux notification, click Acquire wakelock. This prevents Android from putting Termux to sleep,
and results in MUCH better performance when your phone's display is off.

2. Install dependencies

apt-get update && apt-get upgrade -y
apt-get install wget proot -y

3. Create Ubuntu chroot

cd ~
mkdir ubuntu
cd ubuntu/
wget https://raw.githubusercontent.com/MFDGaming/ubuntu-in-termux/master/ubuntu.sh -q
chmod +x ubuntu.sh
./ubuntu.sh -y

/proc access

Android >8 has a "security feature" that prevents userspace apps from reading some files in /proc.
To mount all of /proc, then, we need to start the chroot as root.

NOTE: For this reason, this can/should only be done on rooted devices running Android >8 if you want complete access to /proc (you probably do).

mv startubuntu.sh startubuntu_OLD.sh
wget https://gist.githubusercontent.com/spaceface777/6f438ea1760d278ff3f536eccab09876/raw/83ae593cdcbfa3f4ea9df41204975eeb6ab7d412/startubuntu.sh
chmod +x startubuntu.sh

4. chroot init alias

Right now, whenever you want to start your chroot, you need to type bash ubuntu/startubuntu.sh.
If you're like me and are way too lazy to do this every time, you'll want to set up an alias for this.

echo "clear; bash ~/ubuntu/startubuntu.sh" > ../../usr/bin/u
chmod +x ../../usr/bin/u

5. Starting the chroot

If you set up the alias like I told you to before,

u

If not,

bash ~/ubuntu/startubuntu.sh

NOTE: You will have to run this every time you want to enter the chroot,
so you may want to go back and set up the alias now.

6. Installing packages

Now you have a working chroot, but it's barely usable because it has nothing installed. :/
To fix this, let's install some important packages.

apt update -y
apt upgrade -y
apt install nano whiptail

7. Adding a user

Running everything as root is not very secure at all, so let's make you a new user.
Pick a username, and replace it for <user> in the commands below.

adduser <user>

Enter a password and answer the prompts (you can leave everything blank).

A user without sudo isn't very useful, though, so let's give it sudo access.

apt install sudo
usermod -aG sudo <user>

Make sure your sudo works:

su - <user>
sudo true

If it prompts you for your user password, it's working. From now on, we'll use this user account rather than root.

Fixing user permissions

The permissions inside the chroot are very messed up.
Let's fix them for your new user account.

sudo chown -R $USER:$USER ~/
sudo chmod -R o-rwx ~/

8. Hosts setup

In the last step, you may have noticed a warning: sudo: unable to resolve host localhost: Name or service not known To fix this, let's fix the chroot's hosts.

echo "127.0.0.1 localhost.localdomain localhost" > /etc/hosts

9. Installing ssh

You may have noticed that typing commands into your phone is a little uncomfortable.
To fix this, let's install an ssh server so you can connect from your computer.

sudo apt install openssh-server
mkdir -p -m0755 /run/sshd

We need to change the ssh server's config.

sudo nano /etc/ssh/sshd_config

Find the following lines and remove the # at the beginning:

Port
ListenAddress
PubkeyAuthentication

Go back to the Port line and change 22 to any port number >1000.
Any port below 1000 is secure, so it cannot be binded to without root.

I used the port 8022.

Start the ssh server:

/usr/sbin/sshd

pubkey login

I won't go into depth on this because if you want to enable this, you probably know how to set it up OR can google it yourself.

HOWEVER, if you do try to do this, you should keep this in mind
(it took me a few hours to figure out what the issue was):

SELinux protects the .ssh/authorized_keys file

To fix this, you need a rooted device.
exit the chroot and enter a root Android terminal:

su
restorecon -v ubuntu/ubuntu-fs/home/<user>/.ssh/authorized_keys

Reboot the device and restart the chroot and sshd server.
ssh authentication via pubkey should work now.

10. Connecting via SSH

Open a terminal on your computer, and enter:

ssh <user>@<ip> -p <port>

where:

  • <user> is the account you set up earlier
  • <ip> is your phone's IP address
  • <port> is the port you configured earlier

Example: ssh spaceface@192.168.1.100 -p 8022

11. Unminimizing

When logging in via ssh, you may have seen the following message:

This system has been minimized by removing packages and content that are
not required on a system that users do not log into.

To restore this content, you can run the 'unminimize' command.

I really recommend you do this.

sudo unminimize

12. Fixing locale

By default, your chroot's locale is POSIX, which doesn't allow printing Unicode characters.
Let's fix this so that you can use your 𝓯𝓪𝓷𝓬𝔂 Unicode fish shell prompt.

If you don't trust me, you can check your current locale with the locale command.
All entries will either be blank or POSIX.

(Replace en_US with the language you want your shell to have)

sudo apt install locales
sudo locale-gen en_US
sudo locale-gen en_US.UTF-8

Find and select en_US.* using space

sudo dpkg-reconfigure locales

Use the locales:

sudo update-locale LANG=en_US.UTF-8
sudo update-locale LANGUAGE=en_US
sudo update-locale LC_ALL=en_US.UTF-8

Log out and log back in. Your locale should have changed.
(You can check by running the locale command again)

More coming soon

cd $(dirname $0)
su -c "/data/data/com.termux/files/usr/bin/proot --link2symlink -0 -r ubuntu-fs -b /dev -b /proc -b /sys -b ubuntu-fs/tmp:/dev/shm -b /data/data/com.termux -b /:/host-rootfs -b /sdcard -b /storage -b /mnt -w /root /usr/bin/env -i HOME=/root PATH=/usr/local/sbin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/games:/usr/local/games TERM=$TERM LANG=C.UTF-8 /usr/bin/bash --login"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment