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.

mkfs.ext2
mkfs.ext3
mkfs.ext4
mkfs.ext4dev

mkfs.fat
mkfs.vfat
mkfs.msdos 

mkfs.bfs   
mkfs.cramfs
mkfs.minix

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

reboot
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.

Raspberry Pi Static IP Address (RP3)

So as you may have already found, things work slightly differently with the new Jessie based Debian OS. If you have hit my last Static IP post and thought things were just not quite right, this ones for you!

It seems that /etc/network/interfaces is overruled by dhcpcd meaning your soon to be static ip has to be set elsewhere.

In the /etc/network/interfaces file, your specific interface (iface) should be left as manual. In fact, if you are working with a fresh install the easiest way is just not to touch it at all! If curiosity gets the better of you, there is also a statement in there telling you to look elsewhere to change the settings.

Extract from /etc/network/interfaces
# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

We do still have to know a few things so if you need a recap have a read of my last post on the subject and gather the info you need.

Particularly we are looking for the following
– netmask
– gateway
Obviously the IP address you want to set, provided it is within bounds is totally up to you.

Fire up /etc/dhcpcd.conf with your favourite text editor

Fairly simple… We are going to add 3 lines to the bottom

static ip_address=192.168.1.199/24
static routers=192.168.1.1
static domain_name_servers=127.0.0.1
// In my case i am running a caching nameserver on localhost
// - Hence the directive of 127.0.0.1 for the nameserver.
// In most cases nameserver will be the same address as your gateway

I hear you thinking “What is the /24 on the tail of the IP address?”
– Well i am going to say “Google It!

In this case it is shorthand for netmask of 255.255.255.0 with a broadcast of 255.
If your netmask or broadcast is different, be careful… It may help to use a calculator to determine the leading count.
I feel the rest speaks for itself.

Save the file & restart networking or reboot.
Confirm we are good by running

ifconfig
route -n
cat /etc/resolv.conf //In most cases your gateway address

Raspberry Pi Static IP Address

UPDATE
– Things have changed a little with the new Jessie based image…
See my new tutorial for Jessie based systems

The tutorial below is suitable for all versions of the Raspberry Pi running a Debian based OS (Raspbian etc). Although the older Raspberry Pi’s such as Model A & Model A+ are near obsolete, the structure is the same. Tested on Raspberry Pi Model B, Model B+ & Raspberry Pi 2.

So you want to set your Raspberry Pi to have a static IP address. This is useful in so many ways whether it be for port forwarding, SSH logins or one of the many other reasons you want to have the same persistent address such as a dev server. The process described below is also the same for all debian based systems, including Ubuntu.

First of all some research needs to be done to determine current network settings so that the static address can be applied correctly. Run the following two commands

ifconfig
route -n

Console

Once the above has been run we can collect the following information
– Gateway
– Netmask
– Network
– Broadcast

Once our needed information has been gathered, network interfaces needs to be modified & networking restarted (pi rebooted). As we are working on a fresh install and to keep it simple, i am going to be using the packaged text editor – nano. Run the following command, making sure it is launched as root.

sudo nano /etc/network/interfaces

We then want to edit our primary interface so that it defines a static ip with the necessary configurations. Make sure you change the address to that of your desired static ip. Obviously make sure it is within the bounds of your network. In my case eth0 is my primary interface, however if for example your primary interface was a wireless connection, the following would be configured under wlan0 and so on.

iface eth0 inet static
address 192.168.1.199
gateway 192.168.1.1
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255

Mine ended up looking like this…

Network/Interfaces

Make sure you then save and reboot (sudo reboot).
Done!