0%

Debian on Android: Install Debian from Scratch

Outline

In this blog, you will know how to install Debian for an Android device with root privileges.

No Termux. No Linux Deploy. No Black Box.

Only Debian offical installer. Everything is under your control.

Prerequisites

  • root privileges
  • busybox

With Magisk installed, you can get both root privileges and busybox.

Install Debian with debootstrap

Switch to root user

Most commands should be executed with root privileges.

With adb, you can type:

1
2
adb shell
su

Prepare necessary commands

debootstrap requires several basic commands to run.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# The most important program is busybox, and we can use busybox binary from Magisk
export BUSYBOX=/data/adb/magisk/busybox

# Set the base directory of our Linux distributions
export LINUX_DIST=/data/linux_dist
mkdir -p $LINUX_DIST

# Prepare necessary utils required by debootstrap
LINUX_UTILS=$LINUX_DIST/utils
mkdir -p $LINUX_UTILS
ln -s $BUSYBOX $LINUX_UTILS/ar
ln -s $BUSYBOX $LINUX_UTILS/wget
ln -s $BUSYBOX $LINUX_UTILS/xzcat
export PATH=$LINUX_UTILS:$PATH

Setup debian system image path and mount point

1
2
3
4
5
6
# Set the debian inst directory
export DEBINST=$LINUX_DIST/debinst
mkdir -p $DEBINST

# Debian rootfs image path
export DEBIMG=/sdcard/debian.img

Create a img file as rootfs

1
2
3
dd if=/dev/zero of=$DEBIMG bs=1G count=10
mkfs.ext4 $DEBIMG
$BUSYBOX mount -o loop $DEBIMG $DEBINST

Build pkgdetail (on your pc/server)

1
2
wget -O pkgdetails.c https://salsa.debian.org/installer-team/base-installer/-/raw/master/pkgdetails.c?inline=false
aarch64-linux-gnu-gcc -static -o pkgdetail pkgdetails.c

Transfer pkgdetail to Android device

Take adb as an example.

1
adb push pkgdetail /sdcard

Install debootstrap

Download debootstrap and prepare pkgdetails for it.
Note: the dest file is pkgdetails, not pkgdetail

1
2
3
4
5
6
7
8
9
10
11
12
# Download debootstrap
cd $LINUX_DIST
wget -O debootstrap.tgz http://mirrors.aliyun.com/debian/pool/main/d/debootstrap/debootstrap_1.0.133.tar.gz
tar -xf $LINUX_DIST/debootstrap.tgz

# Set the necessary environment: DEBOOTSTRAP_DIR
export DEBOOTSTRAP_DIR=$LINUX_DIST/debootstrap

# Put pkgdetail to $LINUX_DIST/pkgdetails
# !!! Note that the dest file is: pkgdetails (not pkgdetail)
cp /sdcard/pkgdetail $DEBOOTSTRAP_DIR/pkgdetails
chmod +x $DEBOOTSTRAP_DIR/pkgdetails

Install Debian with debootstrap

1
2
# Install Debian
$DEBOOTSTRAP_DIR/debootstrap --arch arm64 stable $DEBINST http://mirrors.aliyun.com/debian/

Configure Debian

Chroot to Debian: chroot-debian.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
BUSYBOX=/data/adb/magisk/busybox
DEBINST=/data/linux_dist/debinst

$BUSYBOX mount --rbind /dev $DEBINST/dev/
$BUSYBOX mount --rbind /proc $DEBINST/proc/
$BUSYBOX mount --rbind /sys $DEBINST/sys/
$BUSYBOX mount -t tmpfs tmpfs $DEBINST/tmp/

$BUSYBOX chroot $DEBINST /bin/env --ignore-environment su -l

# $BUSYBOX umount $DEBINST/dev/
# $BUSYBOX umount $DEBINST/proc/
# $BUSYBOX umount $DEBINST/sys/
# $BUSYBOX umount $DEBINST/tmp/

Fix network error inside Debian

1
2
3
4
5
6
7
8
9
10
11
12
# Add network related groups required by Android kernel
groupadd -g 3001 aid_bt
groupadd -g 3002 aid_bt_net
groupadd -g 3003 aid_inet
groupadd -g 3004 aid_net_raw
groupadd -g 3005 aid_admin

# Add root to network groups so that root has network access rights
usermod -aG aid_bt,aid_bt_net,aid_inet,aid_net_raw,aid_admin root

# Set gid of _apt as aid_inet to fix apt network error
usermod -g aid_inet _apt

Configure DNS resolution

1
2
3
4
5
6
cat >/etc/resolv.conf <<EOL
domain lan
search lan
nameserver 223.5.5.5
nameserver 223.6.6.6
EOL

Enjoy your Debian!

1
apt update && apt install neovim

Troubles

Android kernel does not support System V IPC

Applications that rely on System V IPC, such as PostgreSQL, cannot be run directly in Debian on Android.

Possible solutions: Termux emulates System V IPC.

Refer to:

Android kernel allows only specific groups to access the network

That’s why we add root to some groups and modify gid of _apt to aid_inet

Refer to:

If your application fail to run, check if it’s a network issue!

For example, mysqld run as user mysql, and its default group is mysql.

1
mysql:x:104:104:MySQL Server,,,:/nonexistent:/bin/false

Without modifying its group to aid_inet, service start mysql / mysqld would fail.

If you do not change the group of user mysql to aid_inet, command service start mysql / mysqld will fail.

Optional Steps

Modern bashrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# ~/.bashrc: executed by bash(1) for non-login shells.

# Note: PS1 and umask are already set in /etc/profile. You should not
# need this unless you want different defaults for root.
PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "
# umask 022

export TERM=xterm-256color

# You may uncomment the following lines if you want `ls' to be colorized:
export LS_OPTIONS='--color=auto'
eval "`dircolors`"
alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
# alias l='ls $LS_OPTIONS -lA'
#
# Some more alias to avoid making mistakes:
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

Update apt source list

Refer to Tsinghua Mirrors - Debian

Install sysvinit-core to run daemons

1
2
apt update && apt install sysvinit-core
# apt remove systemd

Configure timezone

1
dpkg-reconfigure tzdata

Configure locales

1
2
apt install locales
dpkg-reconfigure locales

Install and configure mariadb

Install mariadb with proper apt source

1
apt install mariadb-server

You must change group of user mysql to aid_inet

1
usermod -g aid_inet mysql

If package sysvinit-core is installed, start mysqld with

1
service mysql start

Without sysvinit-core, you can run

1
mysqld

Configure bash completion

1
apt install bash-completion

References