[Root] Install Ubuntu in chroot on Android without Linux Deploy

🇹🇼 中文版

Termux, a temrminal emulator, could be an good Linux Deploy alternative. You can manually install Ubuntu chroot environment on Android phone without using Linux Deploy .

Ubuntu 22.04 in chroot envrionment running on Android 12, Sony Xperia 5 II. Ubuntu 22.04 in chroot envrionment running on Android 12, Sony Xperia 5 II

1. Install Busybox for Android 12+ devices

Install Fox’s Magisk Module Manager

Search “Magisk Builtin Busybox” and install the module.

Reboot.

2. Download Ubuntu rootfs

Please install Termux first.

  1. Open Termux, install tsu and pulseaudio
1
2
pkg update
pkg install tsu pulseaudio
  1. Create a directory at /data/local/tmp for chroot envrionment
1
2
3
su
mkdir /data/local/tmp/chrootubuntu
cd /data/local/tmp/chrootubuntu
  1. Download latest Ubuntu base rootfs
1
wget https://cdimage.ubuntu.com/ubuntu-base/releases/22.04/release/ubuntu-base-22.04-base-arm64.tar.gz
  1. Unzip the archive. And create a mountpoint for sdcard (internal storage)
1
2
3
tar xpvf ubuntu-base-22.04-base-arm64.tar.gz --numeric-owner

mkdir sdcard
  1. Create a startup script.
1
2
cd ../
vi startu.sh
  1. Type these:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/sh

# The path of Ubuntu rootfs
UBUNTUPATH="/data/local/tmp/chrootubuntu"

# Fix setuid issue
busybox mount -o remount,dev,suid /data

busybox mount --bind /dev $UBUNTUPATH/dev
busybox mount --bind /sys $UBUNTUPATH/sys
busybox mount --bind /proc $UBUNTUPATH/proc
busybox mount -t devpts devpts $UBUNTUPATH/dev/pts

# Mount sdcard
busybox mount --bind /sdcard $UBUNTUPATH/sdcard

# chroot into Ubuntu
busybox chroot $UBUNTUPATH /bin/su - root

# Umount everything after exit
busybox umount $UBUNTUPATH/dev/pts
busybox umount $UBUNTUPATH/dev
busybox umount $UBUNTUPATH/proc
  1. Make the script executable
1
chmod +x startu.sh
  1. Start the script. Then you should see the prompt becomes root@localhost. To logout, type exit.
1
sh startu.sh

3. Install applications

  1. Before updating packages, execute the following commands to solve apt cannot resolve host errors
1
2
echo "nameserver 8.8.8.8" > /etc/resolv.conf
echo "127.0.0.1 localhost" > /etc/hosts
  1. Then fix Download is performed unsandboxed as root warning:
1
2
3
4
5
groupadd -g 3003 aid_inet
groupadd -g 3004 aid_net_raw
groupadd -g 1003 aid_graphics
usermod -g 3003 -G 3003,3004 -a _apt
usermod -G 3003 -a root
  1. Now you can update packages:
1
2
apt update
apt upgrade
  1. Install common tools:
1
apt install vim net-tools sudo git

3.1. Start SSH service in chroot

  1. Install OpenSSH
1
sudo apt install openssh-client openssh-server
  1. Change root password
1
passwd root
  1. Start SSH services.
1
2
mkdir /run/sshd
/usr/sbin/sshd -D &

You can check the IP address of the phone by executing ifconfig. Then you can SSH into the phone from your PC.

3.2. Install XFCE4 and VNC server

  1. Install XFCE4 desktop environment:
1
2
apt install xfce4 xfce4-goodies
update-alternatives --config x-terminal-emulator
  1. Install VNC server
1
apt install tigervnc-standalone-server tigervnc-xorg-extension
  1. Setup password for VNC server
1
vncpasswd
  1. Create a startup script for VNC server: vim ~/.vnc/xstartup
1
2
3
4
5
#!/bin/bash
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADRESS
export PULSE_SERVER=127.0.0.1 && pulseaudio --start --disable-shm=1 --exit-idle-time=-1
exec startxfce4
  1. Make it executable
1
chmod +x ~/.vnc/xstartup
  1. Start VNC server. Then open AVNC and type localhost:5901 to view the desktop enviromment.
1
vncserver -localhost no
  1. If the terminal failed to launch, try to mount devpts
1
mount -t devpts devpts /dev/pts
  1. Remember to stop VNC server after closing AVNC app:
1
vncserver -kill :1

3.3. Start PulseAudio

Execute the following commands in Termux (not chroot. Swipe from left edge to open a new session)

1
2
pulseaudio --start --load="module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1" --exit-idle-time=-1
pacmd load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1

3.4. Add a normal user

  1. Setup the timezone to Asia/Taipei
1
ln -sf /usr/share/zoneinfo/Asia/Taipei /etc/localtime
  1. Add a new user named user
1
2
3
4
groupadd storage
groupadd wheel
useradd -m -g users -G wheel,audio,video,storage -s /bin/bash user
passwd user
  1. Execute: vi sudoers. Add user to sudoers
1
user    ALL=(ALL:ALL) ALL
  1. To start vnc server as normal user. Create a vnc startup file at user’s home direcotry like you did before.

  2. Now you can type su user to switch to user. And you can modify the scipt o starting chroot , change chroot . /bin/su - root to chroot . /bin/su - user.

3.5. Do not install Snap packages

When you type sudo apt install firefox, Ubuntu will try to download snap version of Firefox. However, Snap will not work in chroot because there is no systemd on Android. Therefore, you should avoid installing snap packages.

For exmaple, you can add Mozilla’s ppa to install firefox-esr instead of fireofx:

1
2
3
sudo add-apt-repository ppa:mozillateam/ppa
sudo apt-get update
sudo apt-get install firefox-esr

And you could disable Snap by doing this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
apt-get autopurge snapd

cat <<EOF | sudo tee /etc/apt/preferences.d/nosnap.pref
# To prevent repository packages from triggering the installation of Snap,
# this file forbids snapd from being installed by APT.
# For more information: https://linuxmint-user-guide.readthedocs.io/en/latest/snap.html
Package: snapd
Pin: release a=*
Pin-Priority: -10
EOF

4. References


Thanks for your reading. You are welcome to share articles of Ivon's Blog (ivonblog.com). Please include the original URL when reprinting or citing articles, and abide by CC BY-NC-ND 4.0 license. For commercial use, please write an e-mail to me.

written by human, not by AI

If this article is helpful to you please consider supporting me.

Leave a comment

Choose the commenting system you like.

Please login to your Github account to leave a comment. You can post codes here for discussion. For images, please upload to Imgur and copy links. Your comments would be visible to everyone on Github Discussions.

This comment system is provided by Disqus, you may see some Disqus ADs on this page. You can leave a comment anonymously but you won't receive reply notifications.