Formatting Drive With FDisk (Linux)

In this tutorial I am going to show you how to format and set up a file system for a drive in Linux using fdisk.

In my particular scenario, i am setting up a USB drive with an EXT4 partition for use, but with a little tweaking can be used for all types of storage and all types of file systems.

First of all we need to find where the drive is that we want to work with.

fdisk -l

In my case im on /dev/sda but you may be different. Please make sure you are targeting the correct drive or data loss will occur.

It may be helpful at this point to make sure you know the basic fdisk options are. Obviously for a more in depth list you may just want to use m when already in fdisk, or for a range of available switches, type fdisk -h in the terminal. More to what we are trying to achieve, below are the basic commands relevant to deleting & creating new partitions.

    m – print help
    p – print the partition table
    n – create a new partition
    d – delete a partition
    q – quit without saving changes
    w – write the new partition table and exit

CAUTION – The following will delete all partitions on your drive and subsequently all data. Please make sure if you do not understand or do not grasp the drive designation / location that you have ALL of your data backed up or stop at this point.

So we are going to delete all partitions to make way for our new partition

fdisk /dev/sda
d // Delete
d // Again if more than one...
w // Write Changes

So now if you fdisk -l again you should see an empty drive….
Something like that of below (note no partitions are in place following)

Disk /dev/sda: 14.3 GiB, 15376000000 bytes, 30031250 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

So lets now go ahead and create a partition.
In this case we are using the whole drive but obviously your scenario may be different.
If in deed you are using the whole drive, the defaults are more than adequate.

fdisk /dev/sda
n // New Partition
  // Choose defaults unless setting up >1 partition
w // Write changes

SO now your new partition has been created, however we still have to build the file system.
Before that, you can confirm the partition you just created exists by once again doing an fdisk -l. You can see mine below is no longer an empty drive but returns the following.

Disk /dev/sda: 14.3 GiB, 15376000000 bytes, 30031250 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start      End  Sectors  Size Id Type
/dev/sda1        2048 30031249 30029202 14.3G 83 Linux

So now its time to create the file system. Fairly straight forward..

Be aware that you are no longer dealing with the broad drive designation of /dev/sda but are dealing with the more specific partition that you created of /dev/sda1.

mkfs.ext4 /dev/sda1

There are several other file systems that you can create using the above mkfs command. I used an EXT4 file system, however if for example i wanted to make an EXT3 file system, i would have just changed the above line of code to mkfs.ext3 /dev/sda1.

For your reference, I have listed the available (debian) file system builders below.




Just to be safe we will now do a reboot and recheck all is good upon login.

fdisk -l

There are a couple of different avenues you can take now depending on what you are trying to achieve.

Obviously first and foremost, you can now temporarily mount your fresh drive wherever you want..

mount /dev/sda1/ foo/bar

However secondly, if you want to programmatically mount your drive upon every boot, look into fstab.
I will cover fstab soon but in the meantime Ubuntu has a sufficiently detailed write up that you can reference here.

DEV WordPress on Local Network

The following tutorial has been tested & written on Ubuntu 14.04 LTS running in an Oracle Virtual Machine. The reason i stage this way is to keep my machine as clutter free as possible. When you are not using it, the VM is simply shutdown!

Steps I have taken to set up a Dev WordPress site on my Local Network (Actually Ubuntu Server running in a Virtual Machine), however the following could just as easily be applied to the Raspberry Pi (LAMP installation would be necessary and outside the scope of this tutorial). At present the following has only been tested on Ubuntu Sever.
– Referenced heavily from the WordPress Codex

  • Log into terminal as root
  • Make sure you install Ubuntu with SSH & LAMP options enabled
  • – Set the MySQL password to something memorable & secure. This wil become the MySQL ‘root’ password.

First you have to set up a MySQL user and grant necessary privileges.
Log in to MySQL on the command line using mysql -u root -p where the password is that of what you set upon install.

CREATE USER wpuser@localhost IDENTIFIED BY 'password';
// Make sure the apostrophe stays around the password
GRANT ALL PRIVILEGES ON wordpress.* TO "wpuser"@"localhost" IDENTIFIED BY "password";

Make sure the ‘password’ fields above are changed to something memorable & secure. Good habit, even on a local install.

Download the latest version of WordPress to your web directory (/var/www/html/).

cd /var/www/html/
tar xvfz latest.tar.gz

As a final step you need to grant Apache privileges for the newly created wordpress folder. Failure to do this will result in the following install to fail.

sudo chown -R www-data:www-data /var/www/html/wordpress

Obviously if you are already running as root you can omit the sudo, however just a quick explanation of what the above command is doing for those not so clued up reading this tutorial. (We all need to start somewhere!)

  • (chown) Is a Linux command to change ownership of a file / folder.
  • (-R) As we are working with a directory, this applies the command recursively.
  • (www-data:www-data) Changes owner to user of group (owner-user:owner-group).
  • (/var/www/html/wordpress) The directory / file we are sighting.

Now the WordPress installation is on the server, we can navigate to 'server'/wordpress/ in a web browser and follow the famous five minute install. Remember the name of the MySQL database and user you set up previously. WordPress will give you options if any of the above steps fail.. Particularly permissions.

Following the installation completion, with any luck you should be up and running!