Tutorial: How to install Ubuntu and other Debian based distributions via ‘debootstrap’

To install any Debian based distribution you can not only use the corresponding installation medium like DVD and USB: no, if you have a Debian-based distribution installed on your computer you can use it to create an installation on any USB stick, external HDD. This tutorial will not only lead you through the process, it will also give you the ability to really control what you’re going to install. So let’s get started, shall we?

The first big step to take is finding a mirror for the installation you want to make; I listed below a few for the most common distributions around:

Ubuntu:

Link http://archive.ubuntu.com/ubuntu/
Codenames precise, trusty, vivid, wily, xenial, yakkety

Debian:

Link http://httpredir.debian.org/debian
Codenames sid, jessie, wheezy

Kali:

Link http://http.kali.org/kali
Codenames kali-rolling

Note: The below procedure may slightly differ from distribution to distribution. What follows is the exact process of solving this in Ubuntu. Especially the part about installing a desktop environment may need differently named packages and the sources.list file might be different.

You will want to start with doing a few preparatory installations on the host system from which you ‘re going to install your new system. In the following I will describe the exact procedure for using Ubuntu as your host system. You need gksudo which is in the package gksu, schroot and debootstrap. Those packages are not installed by default, so open a terminal by pressing (ctrl+alt+t) and use the following commands to install the needed packages:

sudo apt-get install gksu debootstrap schroot

Now you can start preparing your USB/HDD for installation by gksudo gparted where you prepare a partition on your actual HDD or prepare an USB HDD/stick for doing the installation on. In my example here, I use a 16 GB USB stick. Create a partition which is at least 8 GB for your installation, in my case I used 15 GB for my ext4 partition and 1 GB for my swap partition. Many will tell you that with recent computers you might not need a swap partition but let’s say we want a system which runs on most computers around even weaker ones having less RAM.

So first you want to go to the ‘devices’ menu and select ‘create partition table’. When asked for the type choose ‘msdos’. Now you should see an entry for unallocated space. Click on this and choose new and make it ‘ext4’ leaving only 1GB as free memory. Then again click on the unallocated space entry and make it 1GB for Linux-Swap. After this you apply your settings to the USB/HDD. Then right-click on the ‘ext4’ entry and choose ‘manage flags’ where you tick the check-mark on `boot` and leave.

Now create a directory to mount your newly created drive/partition to and mount your partition to it. Remember that you may need to change the following lines to fit your situation, especially the drive name.

sudo mkdir /mnt/work
sudo mount /dev/sdb1 /mnt/work/

Now you can put the above mentioned links to use and do the basic installation on which you later build upon. This is done by the debootstrap command.

sudo debootstrap --arch=amd64 xenial /mnt/work http://archive.ubuntu.com/ubuntu/

debootstrap explained:
This command has the following ground syntax, refer to the table below it for more details to the different flags and options.

sudo debootstrap --variant=<variant> --arch=<architecture> <codename> <mount-path> <repository-url>
Flag/Option Possible parameters Comment
–variant= minbase, buildd, fakechroot, scratchbox The option --variant=minbase only installs a very rudimentary system which obviously will miss some of the necessary commands which you will need for working with this solution. So if you not want to install them one by one by hand leave the option --variant= out completely, this way a full-fledged command line system will be installed.
–arch= alpha, amd64, arm, armel, hppa, i386, ia64, m68k, mips, mipsel, powerpc, s390, sparc Note: While it is possible to install a 32-bit system from an 64-bit host, the reverse isn’t possible!
codename trusty, xenial, zesty, artful Note: These are the actual possible codenames for Ubuntu at the time of writing, refer to the table at the top of this tutorial for other distributions codenames.

For more information see the man page for debootstrap.

Now you need to prepare your chroot environment , because you are running from a host machine your new ‘chroot-jail’ needs some resources mounted before you can fully use it. The next few commands do exactly that.

sudo mount -o bind /dev /mnt/work/dev
sudo mount -o bind /dev/pts /mnt/work/dev/pts
sudo mount -t sysfs /sys /mnt/work/sys
sudo mount -t proc /proc /mnt/work/proc
sudo cp /proc/mounts /mnt/work/etc/mtab
sudo cp /etc/resolv.conf /mnt/work/etc/resolv.conf

After that is done, the time has come to finally lock yourself into your newly created ‘chroot-jail’ with sudo chroot /mnt/work/, which will make your command prompt look like this:

root@HOSTNAME:/#

Here is where the completion of your system installation begins, so read carefully and thoroughly. Start by setting the `locale` setting and the `keyboard-configuration` and installing the kernel. Now the procedure for setting the locales:

locale-gen en_US.UTF-8
dpkg-reconfigure locales
localedef -i en_US -c -f UTF-8 en_US.UTF-8

Please note that you only need to set the same ‘locale’ in dpkg-reconfigure locales you have generated before, you can change this later once you have booted your new installation. Following this you need to set the keyboard configuration, make sure you select the proper settings for your keyboard:

dpkg-reconfigure keyboard-configuration

The installation of the kernel is pretty straightforwardly done with a single command. Remember this will as well try to install the ‘grub’ bootloader. If you’re installing using this method a second system on your host, you leave the following mentioned field for installing ‘grub’ empty.

apt-get install linux-image-generic

During the installation `GRUB` asks you which device it should be installed on. You will see a selection like this:

[ ]/dev/sda [NotThisOne, this is your HDD]
[ ]/dev/sdb (31474 MB; ???)
[ ]/dev/sdb1 (30398 MB; /)

Choose /dev/sdb1 and /dev/sdb by moving to the respective field using the arrow keys and pressing space then finally hit return. Now after this installation is completed you can install your editor of choice; As an example, I’ll install `nano`; of course you can install the editor of your choice (vim, emacs, joe … etc.)

apt-get install nano

Userful shortcuts for the nano editor:

Shortcut Explanation
ctrl+x Ends the editing and lets you choose to save the file if there have been changes.
ctrl+o Saves without leaving the editor.
ctrl+shift+^ Allows marking of text.
ctrl+k Cuts out the marked text.
ctrl+u Pastes in previously cut text.
ctrl+w Search function.
ctrl+r Reloads the file and dumps all unsaved edits.
ctrl+g Help. Here you can get a overview of all possible commands.

Now you need to edit your /etc/fstab file: To make this easier, run the command blkid, which should give you an output similar to the following. Remember that this can look totally different on your system so make sure you’re using the right entries in the following step.

/dev/sda1: UUID="8f8f130f-e127-43b6-b797-8bfa3f1e1631" TYPE="ext4" PARTUUID="06bd4abe-01"
/dev/sda5: UUID="8469c58b-b2bb-4551-b47d-c1a7d21e9ebd" TYPE="swap" PARTUUID="06bd4abe-05"
/dev/sdb1: UUID="b08ab271-5619-479c-aa21-8aea7f4e6f3b" TYPE="ext4" PARTUUID="5872a1f8-01"
/dev/sdb2: UUID="c3309cee-731e-4030-93b9-f1d88c12c806" TYPE="swap" PARTUUID="5872a1f8-02"

Since in my example the USB stick is /dev/sdb you will need to copy the last two lines from the terminal by selecting it with your mouse and pressing ctrl+shift+c, then open your editor by typing nano /etc/fstab. Now you can paste the lines after you cleared out the file’s contents by pressing ctrl+shift+c. Now change the two lines around and edit them so they finally look like this:

UUID=c3309cee-731e-4030-93b9-f1d88c12c806 swap swap defaults 0 0
UUID=b08ab271-5619-479c-aa21-8aea7f4e6f3b / ext4 defaults 0 1

To end your editing and save your changes press ctrl+x, then choose ‘y’ to save and press enter to confirm the file name.

The next step in your journey will be filling in the proper /etc/apt/sources.list. You can do so by copy-pasting the following block to your terminal and hit enter once. Remind the following links are for Ubuntu 16.04 LTS! If you’re going to install any other version of Ubuntu or any other Debian based distribution, the links have to be changed to reflect that. For Ubuntu it is enough to change the codename xenial with the version you’re actually installing wherever the codename of your installation occurs. You can do so by editing the file after creating it with nano or your editor of choice.

cat > /etc/apt/sources.list << "EOF"
deb http://archive.ubuntu.com/ubuntu xenial main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu xenial-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu xenial-security main restricted universe multiverse
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
EOF

Now the final configuration packages must be installed, which require starting services via Upstart, these must be disabled in the `chroot` temporarily. You can achieve that by creating a diversion which will be reverted at the end of this process.

dpkg-divert --local --rename --add /sbin/initctl
ln -s /bin/true /sbin/initctl

Now before you proceed further, run an update on the package database and an upgrade by apt-get update followed by apt-get dist-upgrade. The next step can be achieved, at least on Ubuntu in different ways, for other distributions the package names may differ and are for you to find out. On Ubuntu you can install tasksel and run it to install what you want, but note that you should really only install one flavor of desktop enviroment. For a genuine Ubuntu installation you can as well use the following two commands depending if you want to install a server stack or a desktop enviroment.

# this will install ubuntu-server
apt-get install ubuntu-server
# this will install ubuntu-desktop
apt-get install ubuntu-desktop

And next you’re going to create a user and its corresponding home directory. Do those as single steps and not in one command, as this will save you a lot of troubleshooting steps later on.

adduser <user-name>
mkdir -p /home/<user-name>
chown <user-name>:<user-name> /home/<user-name>
chmod 755 /home/<user-name>

The following commands will assign your newly created user to groups it will need to run later Linux as a user. You could do these in a single command like usermod -aG adm,cdrom,sudo,dip,plugdev,lpadmin,sambashare as well, but this is more error prone, so please execute them one by one. If you encounter an error while adding the user to the group ‘sambashare’ don’t worry and ignore the error.

usermod -aG adm <user-name>
usermod -aG cdrom <user-name>
usermod -aG sudo <user-name>
usermod -aG dip <user-name>
usermod -aG plugdev <user-name>
usermod -aG lpadmin <user-name>
usermod -aG sambashare <user-name>

To check if your user is added to the different groups use less /etc/group. You should see for each group listed in the above usermod commands an entry like this (example: sudo), if not redo the last commands.

sudo:x:27:<user-name>

Now it is time to install optional drivers and useful tools you want to have available in your installation. After you have done that it is time to make grub recognize only the OS installed on your new USB HDD/stick. If you did install it earlier as a secondary OS on your host machine you can skip the next step.

echo 'GRUB_DISABLE_OS_PROBER="true"' >> /etc/default/grub
update-grub

Now you’re going to finalize the install by running one last apt-get update followed by apt-get dist-upgrade then you can revert the diversion you created earlier with the two following commands:

rm /sbin/initctl
dpkg-divert --local --rename --remove /sbin/initctl

Now you can reboot onto your new USB installation and have a fully installed Ubuntu system to work with.


Known troubles and ways to fix them:

If you run into the trouble of not being able to start the terminal which seems to be related to a bug in `gnome-terminal` you can check in TTY (ctrl+alt+f1 where you have to login with your user-name and password) the content of `/var/log/syslog`. If it contains lines like the following one:

May 7 22:30:00 HOSTNAME org.gnome.Terminal[1432]: Non UTF-8 locale (ISO-8859-1) is not supported!

To fix this it seems to be enough to execute `sudo dpkg-reconfigure locales` within the TTY and choosing explicitly UTF-8 or using the language settings to apply proper char sets system wide. Afterwards reboot.

6 Comments

    Zachary Wander

    Hello.

    Fabrizio Marana

    Cool! If I would have known this 3 years ago, I wouldn’t have needed to install Ubuntu twice! (but then, now I have lubuntu on my stick and Ubuntu on my laptop)

    Demian

    The blkid’s in the output do not match now.

      Videonauth

      Thank you for pointing that out. revisited the article.

    Carlos

    Will the chroot work with a secodary mount point in /mnt/work/home?, meaning a separate disk for /home

      Carlos

      Ran lsblk and yes it looks like it is working from previous mount on /mnt/work/home

Leave a Reply

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

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.