Wednesday, April 10, 2019

How to Create a Live Linux USB Drive with Persistence

A Live USB drive is a flash drive with which you can boot your computer into an operating system directly from the drive.  There are many reasons one might want to use a Live USB, such as data recovery, infiltration, trying out a new operating system before installing, or simply creating a system you can experiment with and not worry about breaking.

Persistence refers to the ability to save files to the USB drive between sessions.  If you create a Live USB drive without persistence, every time you boot from the drive you will be presented with a brand new system, with no changes carried over from any previous sessions.  Persistence allows you to save files or make changes to the system, and have them "persist" after rebooting the drive.  You can see how this would be useful.

Below I will outline the very simple procedure of creating a Live Linux USB drive with persistence using either Windows or Linux.  The Linux operating system I will be utilizing for this example will be Ubuntu, but the same procedure can be used with several different operating systems.

Creating a Live Linux USB Drive Using Windows and Linux Live USB Creator


The first step in creating a Live USB is to download the operating system you would like to use.  For this example I will be using Ubuntu, which is a very user friendly Linux operating system based on Debian.  It can be downloaded from here:  https://www.ubuntu.com/download/desktop

Next you will need to download the software to create the live USB.  We will be using Linux Live USB Creator, which can be downloaded from here: https://www.linuxliveusb.com/en/download


Once your download is complete, go ahead and run Linux Live USB Creator.

Step1: Choose your Key - This is where you will select your USB drive.  Make 100% sure you pick the correct device, or else you could completely overwrite the wrong drive.

Step2: Chose a Source - Select the Linux image file you downloaded earlier.  If you haven't moved it, then it should be located in your download folder.

Step3: Persistence - Go ahead and move the slider all the way to the right (4090 MB) for a full 4Gb of persistent storage.

Step4: Options - If you are unsure about any of the three options under this section, you can go ahead and select them all, although none of them are required to create the Live USB.

Step5: Create - Click the lightning bolt when you are ready to create the live USB.  This may take a little while (20 mins or so) depending on the computer you are using to create the drive.

Once Linux Live USB Creator tells you that it is finished, safely eject your USB drive from your computer, and you are done!

To test your drive, simply shut down your computer, insert your new Live USB, and power-on the computer.  It should now boot into the operating system you chose to create the Live USB.

If your computer ignores the Live USB and boots into it's usual operating system then you need to adjust your boot options.  This is not difficult, and can typically be done by pressing F2, F8, or F10 during start-up, but this will differ from computer to computer, and is beyond the scope of this tutorial.  If you are having difficulties adjusting your boot sequence, here are a few resources from other websites that may be able to help:
https://www.lifewire.com/change-the-boot-order-in-bios-2624528
http://www.boot-disk.com/boot_priority.htm


If you chose Ubuntu, as I did for this tutorial, when you first boot from the Live USB, you will be given the choice to "Try Ubuntu" or "Install Ubuntu", and of course for our purposes here you only want to "Try Ubuntu".  However, this does point out that if you ever wanted to install Ubuntu you could use this same Live USB for that as well.



And that's it.  Explore your new operating system.  Have fun with it.  Break it if you want to; it doesn't matter, you can easily make a new one by simply overwriting the USB drive and starting from scratch.  Have Fun!


Creating a Live Linux USB Drive Using UNetbootin on Windows or Linux


Another option for creating a live USB on Windows is UNetbootin, which can be downloaded from here:  https://unetbootin.github.io/

I won't go into the details of how to run the UNetbootin Live USB creator, because it is fairly self explanatory, but I wanted to mention it here because not only can Unetbootin be used on Windows, but they also have a version for Linux.  So, if you want to use a Linux machine to create a live USB drive, but you don't want to go through the command line method outlined below, you can use UNetbootin.

To install UNetbootin from the Ubuntu PPA use the following commands:

sudo add-apt-repository ppa:gezakovacs/ppa
sudo apt-get update
sudo apt-get install unetbootin

If you are looking for UNetbootin binaries to use with other flavors of Linux, you can find them here:  https://unetbootin.github.io/linux_download.html

The one downfall of Unetbootin is that as of this tutorial, it can only create a Live USB with persistence for Ubuntu.  All other Linux distributions can only be made into Live USBs without persistence.


Creating a Live Linux USB Using the Linux Command Line


If you are already running Linux, there are still many good reasons to create a Live USB, and it is even easier to do than it was with Windows.  You don't need to download any additional software, since you can create a Live USB from the Linux command line with just a few simple steps.

Disclaimer: While this is any easy process, it also has the potential to cause great destruction.  The command dd used below is nicknamed "disk destroyer" because it can and will overwrite any disk you tell you it to.  Be very careful when using this command.


Before we get started, we need to download our Linux operating system image just like we did in the earlier section for Windows.  This time I am going to create a Live USB with the Kali Linux operating system, which can be downloaded from here:  https://www.kali.org/downloads/

Once you have your operating system image, go ahead and insert your USB drive and open up a terminal.  The first thing we need to do is to identify our USB drive using lsblk.  As you can see in the example output below, our USB drive is named sdb.  Be 100% sure you have identified the correct label of the USB.  If you are unsure, remove the USB drive and run lsblk again to see which volume is gone.  Reinsert the USB to assure the correct drive reappears.

Once you are sure you have identified the USB drive, unmount it using the umount command:

umount /dev/sdb




Now locate your Linux disk image.  If you haven't moved it, it should be in your Downloads directory.  Now we are going to write the disk image to the USB drive using the dd command.  As I mentioned before, be very careful using dd, it will overwrite anything you point it at, so if you are not careful you could overwrite your computer's hard drive.

The syntax of our dd command will look like this:

dd if=/path/to/input_file of=/path/to/output/device bs=4M conv=fsync

"/path/to/input_file" will be our system disk image.  "/path/to/output/device" will be our USB drive.  bs is the block size, and conv=fsync will prevent dd from returning before it is done writing to our drive.  So, the command for this particular example will look like this:

dd if=kali-linux-2019.1a-amd64.iso of=/dev/sdb bs=4M conv=fsync


Congratulations, you have now created a bootable Live USB drive from the Linux command line.  But wait, we're not done quite yet.  We still need to add persistence.  This procedure can be slightly different depending on the operating system, so I will first show you how to do this with Kali.  This procedure should work with most Debian derivatives.  Next I will show you how to do this with Ubuntu, which has a slightly different procedure.


Adding Persistence to a Kali Live USB (and other Debian derivatives)


If we run lsblk again after creating our Live USB, we will see that we now have two partitions, sdb1 and sdb2.  Together they take up about 4G of out 15G USB drive, so we have plenty of space remaining.


We are now going to use fdisk to add a third partition using the empty space left on the drive.  To start the utility, use the command fdisk preceded by sudo, and followed by the name of the device we want to edit:

sudo fdisk /dev/sdb

You will now be presented with the fdisk interface.  Type 'n' to create a new partition.  After that we will just need to press [enter] to select the default responses, which are 'p' for primary partition type, the first available sector as the first sector, and the last available sector as the last sector.  Of course if you don't want to use all the remaining space you can feel free to select anything you want.

When this is done, type 'w' to write the changes to the drive and exit out of fdisk.  If you want to take a look at your shiny new partition you can run lsblk again.


The next several commands are going to be run as root, so I am going to use sudo su to become the root user.  Be very careful when operating as root.  With great power comes great responsibility.

sudo su

We need to format the new partition as ext4 using mkfs.  Use the following command, being sure to adjust the device name to reflect your newly created partition if it is different than mine.

mkfs.ext4 /dev/sdb3

Next we need to mount the partition, so we will create a directory in /mnt using mkdir, and then mount our partition to that directory using the mount command.

mkdir /mnt/temp
mount /dev/sdb3 /mnt/temp

Finally we need to create a file on the partition named persistence.conf, which should only contain the text /union.  This can be accomplished with the following command:

echo “/ union” > /mnt/temp/persistence.conf

Now we simply unmount the drive with the umount command and then type exit to return to our regular user.

umount /dev/sdb3
exit



That's it!  We're done.  Pull out your USB.  Power down your computer.  Insert the USB and boot the computer back up.  You should boot into the operating system you created on your Live USB.


No comments:

Post a Comment