Running Ubuntu 19.10 from a bootable USB stick.

Running Ubuntu 19.10 from a bootable USB stick.

Now, tech how-tos are not what I tend to write about. However, my nephew (Jamal) recently mentioned that he was thinking of playing around with Linux. I can’t remember if he asked, or I suggested, but the discussion got around to running from a USB key. Your reasons for wanting to do this may vary. I have a locked down corporate laptop and don’t want to travel with two devices. You might just want something a little more permanent than a live CD.

In any case I had done this in the past, and offered my (rather eclectic) notes to help J. As it happened, my Linux on a stick went walkabout recently. I think it fell off my keyring at some point. So I’m in the process of building another which gives me an opportunity to clean up my notes and improve the process.

So what exactly are these instructions for?

These are the steps I used to build a bootable USB with a full persistent Ubuntu install. The goal is to produce a bootable USB just like you’d use to install Linux from, that can theoretically boot on any EFI enabled PC that your distro supports and to be able to use that install just like you would a normal installation to hard drive.

We’ll be building a pretty stock standard Ubuntu 19.10 with just a few tweaks to help it run better from a USB, and hopefully reduce the flash wear on the device. Wear may not even be a “thing” any more with modern USB keys, but these changes can’t really hurt, and they make the system more responsive.

You’ll need the following:

  1. A Laptop/PC (ideally with a high speed USB port)
  2. A couple of small 8Gb USB key for the installation. Any old USBs will do. Speed/quality is not important.
  3. A 32Gb+ USB key we’ll install to. I’ve used a Sandisk Ultra Fit 64Gb it’s responsive, and low profile. So low profile you’ll probably lose it like I did.

STEP 1: Prepare the installer and the target USB stick.

First, lets get ourselves a bootable Ubuntu installer. We’ll use this as both a rescue disk and install to prepare the big drive and do the install to it.

  1. Get a copy of the latest Ubuntu iso from https://ubuntu.com/download . Burn it to the one of the small USBs using the steps in the guide suitable for the hardware you have (Mac/Linux/Windows) This one looks OK https://websiteforstudents.com/ubuntu-19-10-eoan-ermine-is-released-heres-how-to-install/
  2. If you’re doing this from one PC, you’ll also need a second small USB for file transfers. Copy the Ubuntu ISO image to the second small USB now. You’ll need it shortly.
  3. Once you’ve created the bootable installer insert insert it, restart the PC and boot from USB using the BIOS menu.
  4. When the Grub boot menu appears, Choose “try Ubuntu without installing”

Now, we’ll use this temporary OS running in RAM Disk to create a second bootable Linux USB with “persistence”. We will never use this drive as is. However, creating a persistent drive nicely partitions the target drive and ensures all the hairy stuff like EFI partitions are created and initialised correctly.

Note that all the software you’re installing from here on on the installer USB is only in RAM, if you shutdown or reboot it’s all gone and you’ll need to start again from this point.

  1. Once you’ve booted to the desktop connect to WiFi and load the instructions at https://www.howtogeek.com/howto/14912/create-a-persistent-bootable-ubuntu-usb-flash-drive/
  2. Install mkusb as per the guide. i.e. open a terminal and run the following commands;

     sudo add-apt-repository universe
     sudo add-apt-repository ppa:mkusb/ppa
     sudo apt-get update
     sudo apt install --install-recommends mkusb mkusb-nox usb-pack-efi
    
  3. You’ll need a copy of the Ubuntu ISO on the installer. Use the copy you put on the second small USB stick. Insert it and copy that ISO to /var/log.
  4. Remove the small USB and insert the big one we’re going to install to.
  5. At this point you should use a command like lsblk to identify your target (big) drive. It will probably be /dev/sdb (depending on your hardware).

Now we’ll run mkusb to create the persistent drive

  1. Run the tool to format the target drive

     sudo mkusb
    
  2. Choose “i” Install, and “p” persistent and when it asks for the ISO to use point it at the file you copied to /var/log
  3. Choose UPEFI, 100% for the persistence, and wait for it to complete.
  4. Once that’s done don’t reboot.

STEP 2: Install The OS to the target (big) USB

You now have 2 bootable USB’s. The small one you’re currently running from, and a newly created large target USB. Now we’re going to install Ubuntu onto the big one. We’ll delete the persistent partition and the 2Gb ISO partition because we won’t ever actually run the big disk as an installer, and we need to create some space to install Ubuntu into.

  1. Open the “Install Ubuntu” icon from the desktop. It may ask you whether you want to unmount /dev/sdb, if so, do that.
  2. As the installer progresses choose your language, keyboard, and connect to WiFi if you want.
  3. At the “Installation Type” choose “Something else”.

Now you’re at the manual partitioning and installer page. For the purposes of this guide we’ll assume you’re installing to a 64Gb USB on /dev/sdb. Adjust your steps if your set-up differs.

  1. Locate /dev/sdb in the list. It should contain 4 partitions. An EFI, a 2Gb ISO, a 60+Gb ext2 and possibly a DOS partition.
  2. Delete the 2Gb ISO partition and the 60+Gb ext2 one using the “-“ button.
  3. Select the free space row under /dev/sdb and press “+” to Create a EXT4 primary partition. Use “/” as the mount point. Let’s assume that partition is called /dev/sdb3.
  4. Important! In the “Device for bootloader installation:” section choose the partition you just created (/dev/sdb3). Don’t choose the /dev/sdb, and definitely don’t choose your hard drive otherwise you’ll end up like I did with grub as your PC’s main boot loader. Incidentally, if the installer overwrites the windows boot loader with grub (though your fault or its), then all you need to do if you boot into a black and white test screen is type “exit”. That should drop out and chain to your normal boot loader…. Hopefully.

This is a very simple install, one mount point and no swap. Feel free to partition the drive differently if you wish. You could add swap if your PC/laptop needs it. I’ve run on a 8Gb Lenovo Carbon that seems to work fine without swap.

  1. Complete the install by choosing password etc, grab a beer or make a cuppa, and wait for everything to copy.
  2. Don’t reboot when it’s done.

We are very nearly done. We just have to do a little bit of surgery to update the grub config on the EFI partition to boot the new install. The installer may have ejected the target USB, if so you’ll need to temporarily mount your new Linux partition and the EFI

  1. Locate the EFI and EXT4 partitions on the target USB (/dev/sdb)

     sudo fdisk -l /dev/sdb
    
  2. Create a mount point for the EFI partition and mount it, assuming the EFI partition is on /dev/sdb2.

     sudo mkdir /media/efi
     sudo mount /dev/sdb2 /media/efi
    
  3. Check the EXT4 partition is mounted using the “mount” command if not do the same for it using these commands. This assumes that the partition is on /dev/sdb3 and we’ll mount it to /media/targetusb. If that partition is already mounted, you can skip this step and use wherever it’s currently mounted instead in the next step’s copy commands.

     sudo mkdir /media/targetusb
     sudo mount /dev/sdb3 /media/targetusb
    
  4. Copy the grub config and image files from the newly created Ubuntu install to the EFI partition so they’ll be used when booting from the BIOS. J, If you get this far you’re on you own for the moment. I’m fairly sure the system will work just copying the grub.cfg, but I did copy the kernel image as well. Unfortunately I left my drive at home so I can’t check it. I’ll fix the following steps in the next couple of days (when I also spell check this guide)

     sudo cp /media/targetusb/boot/grub.cfg /media/efi/efi/boot
     sudo cp -r /media/targetusb/boot/XXXXXX /media/efi/efi/boot
    

That’s it you’re done (for this step). The Target (big) USB should now contain a bootable USB with Ubuntu installed. Restart, eject the install disk, leave/insert the big USB and boot from BIOS using the newly created drive. It should open up to an Ubuntu that looks almost identical to the one you installed from except it’s probably got a first time boot wizard screen.

Complete/close the wizard and test the new install by creating a file on the desktop, then rebooting. If the file is still there, You’re done!

STEP 3: Optimise the new install for running from USB

There are a couple of things we should do to improve performance on USB. This will also help reduce “wear” on the flash, helping make it last longer. For this section you’ll need to create and/or edit some system files. As I’m familiar with “vi” for this kind of thing I use the command “sudo vi the-file-i-need-to-edit”. If you don’t know how to use vi, feel free to use emacs, or a gui editor like sublime (what I’m using to write this).

First, let’s turn off access time writes. For this you need to edit /etc/fstab and add the “noatime” parameter to the “/” mount.

  1. Edit /etc/fstab and add “noatime” to the line with the “/” mount point. It should look something like this.

     UUID=GIUD-OF-YOUR-USB-DRIVE /               ext4    noatime,errors=remount-ro 0       1
    

Now some guides suggest moving /run and /tmp to ramdisk, but it looks like this has already been done in this distro. The idea is that we want to reduce unnecessary writes to the USB by creating temporary filesystems in memory. But we’ll go ome better. Instead, what we’ll do is install a package called goanysync. This also creates a ramdisk for busy directorys, but it periodically syncs them back to actual USB. That way the files persist between reboots. Handy if you need to inspect a log.

First install the goanysync package.

  1. Download the goanysync debian package using this link https://github.com/downloads/wor/goanysync/goanysync_1.02-1_i386.deb Save the package when prompted by your browser
  2. Open your Downloads folder, and find the package.
  3. Right click and choose open with the Software Installer. Follow the prompts to install.

I found that goanysync didn’t work “out of the box” after rebooting as the /run/goanysync directory wasn’t persisting between boots. Probably because /run is a ram disk. So we’ll use the rc.local service to create the directory at boot. You can also use this script if you need to run any other commands at system start.

  1. Create a file called “/etc/rc.local” with the following contents.

     #!/bin/bash
     if [ ! -d "/run/goanysync" ]; then mkdir /run/goanysync; fi
    
  2. Make the file executable.

     chmod ugo+x /etc/rc.local
    
  3. Edit the goanysyc service file /lib/systemd/system/goanysync.service to make it run after the rc-local service. i.e. make it should look something like this.

     [Unit]
     Description=goanysync directory symlinker and content syncer
     Wants=local-fs.target
     After=rc-local.service
    	
     [Service]
     Type=oneshot
     RemainAfterExit=yes
     ExecStart=/usr/bin/goanysync start
     ExecStop=/usr/bin/goanysync stop
    	
     [Install]
     WantedBy=multi-user.target
    
  4. Edit WHATTOSYNC in /etc/goanysync.conf (the config file) to add the log and browser dirs. You can play with this to add more if you want. It should look like this (with your login instead of cain)

     #
     # /etc/goanysync.conf
     #
    
     # Define tmpfs base path, this is where WHATTOSYNC directories will be linked.
     TMPFS = /dev/shm/goanysync
    
     # rsync binary file to use.
     RSYNC_BIN = /usr/bin/rsync
    
     # Define source directories in the WHATTOSYNC comma-separated list. These
     # directories content will be moved under TMPFS path and the directory itself
     # replaced by symlink to the aforementioned path.
     #
     # An example could be:
     #
     WHATTOSYNC = /var/log, /home/cain/.mozilla/firefox /home/cain/.cache
    
  5. Enable Both goanysync and the rc-local service to run at boot with the following commands.

     sudo systemctl enable rc-local
     sudo systemctl enable goanysync
    
  6. Reboot, and check. The commmand “ls -l /var” should show the log directory as a synlink. If not check the services with the following commands

     sudo systemctl status rc-local
     sudo systemctl status goanysync
    

Finally, lets get to the clock. Linux sets the system clock to UTC which is the sensible thing to do. Unfortunately Windows doesn’t, it uses local time. Which means as you dual boot back and forward one or the other will of off unless you live in London. It’s not ideal, but the best solution is to make Linux use local time as well. We’ll let windows take care of daylight saving time etc.

  1. From a terminal run the following command.

     sudo timedatectl set-local-rtc 1 --adjust-system-clock
    
  2. Make sure the time is correct, and adjust as necessary.

Step 4: Other customisation you might want to do

I use a Mac about 20% of the time, so making the new install look a little like my mac book pro makes using the desktop a little smoother. Fortunately Ubuntu 19.10 is already using gnome and has the tweaks tool so the process is fairly painless.

Lets do the superficial changes first.

  1. From Firefox download The Mojave theme https://www.gnome-look.org/p/1275087/
  2. Navigate to your Download folder and Unzip the file. (right click and select “Extract here”)
  3. Create a directory called “.themes” in your home directory and copy the extracted folder there.
  4. Open the Gnome “Tweaks” tool from the Applications menu and use the “Appearance” menu to activate the theme.

Now Lets install a nice mac like icon pack, It’s not that the gnome icons are bad, it’s just that it reduces confusion when hunting looking for a particular function/app.

  1. Download the El-Capitan icons zip from https://github.com/keeferrourke/la-capitaine-icon-theme/archive/v0.6.1.zip
  2. Navigate to the Downloads folder and unzip (right click and select “Extract Here”)
  3. Move the extracted folder to the “.icons” folder in your home directory.
  4. Use the Tweaks tool to select the icon theme.

Finally let make the dock a little more mac like. We’ll use the Dash to dock, and install it from source.

  1. Install git, download the dock and build it. From a terminal window run these commands.

     sudo apt install git
     sudo apt install gettext
     mkdir ~/dev
     cd ~/dev
     git clone https://github.com/micheleg/dash-to-dock.git
     cd dash-to-dock
     make
     make install
    
  2. Now restart the desktop. Press Alt-F2 then “r” then “enter”
  3. Open the Tweaks tool and enable it from the “Extensions” menu.
  4. Configure the extension using the gear icon in the menu. Set it to the bottom of the screen, set the background to while and opacity to about 40.
  5. I also adjusted the size down a little for my laptop, and toggled panel model until I got it looking how I liked.

There you go! You should now have a nice looking functional Linux distro on a bootable USB. It’s a good starting point to customise for your own use.

References

Thanks to Jamal who ran these instructions from scratch and verified that they work and can be understood by someone other than me.

Also, the Following sites:

  1. Ubuntu Linux Distro: https://ubuntu.com/download
  2. Install Instructions at Website for Students https://websiteforstudents.com/ubuntu-19-10-eoan-ermine-is-released-heres-how-to-install/
  3. HowTo Geek for Persistent USB instrucitons https://www.howtogeek.com/howto/14912/create-a-persistent-bootable-ubuntu-usb-flash-drive/
  4. Grub Install HowTo https://howtoubuntu.org/how-to-repair-restore-reinstall-grub-2-with-a-ubuntu-live-cd
  5. Arch Linux article on reducing writes https://wiki.archlinux.org/index.php/Improving_performance#Reduce_disk_reads.2Fwrites
  6. Stack Overflow Question on Running Linux from USB https://superuser.com/questions/1024730/run-linux-from-flash-drive
  7. Restoring MBR from Ubuntu https://help.ubuntu.com/community/Boot-Repair
  8. Make Ubuntu Look like Mac tutorial https://www.omgubuntu.co.uk/2017/03/make-ubuntu-look-like-mac-5-steps
  9. Dash To Dock (https://micheleg.github.io/dash-to-dock/)[https://micheleg.github.io/dash-to-dock/]