Formatting disks in linux

How to format disk in Linux

Formatting a disk will get it ready for use as a storage device on your Linux system. The process involves partitioning the disk, adding a file system to the partition (this is the “formatting” part), and then mounting the partition to some path where you plan to access it from. This might sound complex or like a lot of steps, but it really only takes a few minutes.

This process will wipe all the data from your hard disk and get it ready for use under Linux or another system. In this tutorial, we will cover the step by step instructions to format a hard drive or solid state drive on a Linux system. We will show the steps for both command line and GUI methods, so you can follow along with set of instructions you are most comfortable with. Let’s get started.

In this tutorial you will learn:

  • How to format a disk drive in Linux via GUI
  • How to format a disk drive in Linux via command line

How to format disk in Linux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software gdisk, gparted
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to format a disk on Linux via command line

DID YOU KNOW?
If your intention is to use the hard drive as a primary disk for a Linux installation, then no partitioning is required as any decent Linux operating system will do the job for you during the installation process.

Open a terminal and type the following commands to format your hard disk drive. Here we will be using the gdisk command line tool, which should already be installed by default on all major Linux distros.

    First, let’s figure out how to identify the disk drive we wish to format. The name should start with /dev/sd and then a letter. Type the following command in terminal to see:

Find your device name in the fdisk output

In our example, the disk drive has been assigned the /dev/sdb device path. To create a new partition on the empty disk, we will provide it as an argument to gdisk :

Partition number (1-128, default 1): 1
First sector (34-6291455966, default = 2048) or size: Last sector (2048-6291455966, default = 6291455966) or size:
Current type is 8300 (Linux filesystem) Hex code or GUID (L to show codes, Enter = 8300): Changed type of partition to 'Linux filesystem'
Command (? for help): w Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): y OK; writing new GUID partition table (GPT) to /dev/sdb. The operation has completed successfully.

We are finished partitioning the disk drive with the gdisk utility

You will also need to reply Y to the confirmation prompt to verify that you indeed want to write these changes to the disk, and overwrite existing data.
Since our block device is /dev/sdb , and we just created partition number 1 on the disk, that means our new partition is accessible under the path /dev/sdb1 . Next, we still need to add a file system to our hard disk. Use the mkfs command to format the disk with any file system you would like. In this example, we are using ext4, which is the recommended file system for new HDDs and SSDs on Linux:

$ sudo mkdir -p /media/disk $ sudo mount /dev/sdb1 /media/disk

That is all there is to it. You can now access your newly formatted drive under the /mount/disk directory or where ever you decided to mount it. To make the drive get mounted automatically, see our guide on configuring the /etc/fstab file.

Читайте также:  Linux mint настройка файла подкачки

How to format a disk on Linux via GUI

There are many GUI programs which can also be used to format a hard disk on Linux. In this tutorial, we will focus on using gparted , which may or may not already be installed by default on your system.

To install gparted on Ubuntu, Debian, and Linux Mint:

To install gparted on Arch Linux and Manjaro:

After it is installed, follow the steps below to use gparted to format a disk on Linux:

  1. Get started by searching for the gparted application in your desktop envrionment’s app launcher. You will be prompted for the root password upon opening the program. Search for and open the gparted appYou must supply your root password in order to use gparted
  2. The first thing we need to do is select the correct device that we will be working with from the upper right corner. On our test system, this would be /dev/sdb as indicated in the screenshot below. First select the correct hard disk to work with in gparted
  • As you can see, your disk currently has no partitions. Let’s add one by going to Device > Create Partition Table. Select to create a new partition table from the Device menu
  • Next, select the type of partition you would like to create. We will by sticking with gpt in our tutorial but feel free to pick something else. GPT is recommended for hard disks greater than 2 TB in size. Select partition type and confirm that these changes will erase current disk data
  • Now that the partition table has been created, we can move forward with adding a new partition to the hard disk. This option can be found by heading to Partition > New. Select to create a new partition
  • On this menu, we get to select the size of our new partition. Rather than working with exact values, feel free to use the sliders with your mouse in order to configure the size that you want. For our example, we will simply make one partition that spans the entire size of the hard disk. Click ‘Add’ when ready to proceed. Select size of partition and optionally a name

    NOTE
    ext4 is the recommended file system to use on Linux systems, unless you have a special reason to format the disk with some other kind.

  • The changes are not yet written to the disk, but we can see in the preview pane the configurations that we are about to apply. Once you are ready, click on Edit > Apply All Operations. Applying the changes to disk
  • Afterwards, you can open your system’s GUI Disks application, select the partition we created earlier, and then mount the partition. Be sure to choose a path where you would like the partition to be accessible from. Mounting the partition via Disks GUI application

    Closing Thoughts

    In this tutorial, we saw how to partition a hard drive from command line and GUI on a Linux system. Managing partitions is a dangerous task that should be performed with the utmost caution. The command line and GUI both prove as viable methods for managing partitions, especially in the case of parted and gparted , which are closely related tools.

    Comments and Discussions

    Источник

    How to Format a Drive in Linux

    Formatting a drive is necessary whenever you are trying to erase data on a drive or partition or to create a new partition. Before formatting a partition or drive, it is strongly recommended to make sure that there is nothing important there, as formatting may erase the data for good.

    This guide shows you how to format a drive in Linux.

    Formatting a Drive in Linux

    There are two primary ways of formatting a drive in Linux: using the CLI and using the GUI. For general users, using a GUI tool may be more comfortable, whereas advanced or professional users may find the CLI method better suits them.

    Either way, performing a disk format requires root access or sudo privileges. However, in certain situations, a different user can perform drive format if there is write permission for that drive. This article was written with the assumption that you have root access to your system.

    The drive formatting process can be a bit tricky. To use the device storage, a drive must have a partition table with or without partitions. The partitions will allow the OS to access the storage. By formatting the drive, we are formatting the partition. If there are multiple partitions, then you have to perform the formatting process for all of them.

    Formatting a Partition using the GUI Method

    For managing disks and partitions (and related functions), GParted is the best option. GParted is a free and open-source tool used for managing disk partitions. This tool comes with tons of features and support for numerous file systems.

    There are two ways to use GParted. You can install GParted on the running system or use the bootable image to perform the disk format. Both methods come with their own pros and cons. The good part is, whichever method you follow, the UI of GParted works the same.

    First, we will show you how to install GParted onto your system. Depending on the Linux distro that you are running on your system, use the appropriate one.

    For Debian/Ubuntu and derivatives:

    For Fedora and derivatives:

    For openSUSE and derivatives:

    To use GParted from the boot, download the GParted bootable ISO here. You can also make a bootable CD, DVD, or USB flash drive. Check out how to make a bootable USB flash drive on Linux here. Once you have prepared the system, boot into the CD, DVD, or USB flash drive.

    From this point on, GParted behaves the same. For convenience, I will be demonstrating using GParted installed on Ubuntu.

    Launch GParted, and it will ask for the root password.

    From the top-right corner, select the device on which the operation is to be performed.

    Next, you must decide the target partition. For an in-depth guide on how to create and manage partitions using GParted, check out how to use GParted here. In this case, the target partition is the /dev/sda5 partition.

    As you can see, the partition is currently mounted. Before performing any action, the partition must be unmounted. Right-click and select “Unmount.”

    Now, the partition is ready to be formatted. If the device is a USB flash drive, then file systems like FAT32, NTFS, and EXT4 are generally the best option. If the device is an HDD, then it is better to go for EXT4. You can also go for a different format if there is a need for a specific file system. In my case, I will right-click and select “Format to” >> “fat32.”

    An interesting feature of GParted is that it does not write the changes to the drive instantly. This way, even if you misconfigure the drive, you can easily discard the changes. To make the changes permanent, click “Apply All Operations.”

    GParted will now ask you for confirmation. In this example, after performing the formatting, the partition will lose all its data. Click “Apply” to confirm.

    Voila! The partition has been formatted successfully!

    Formatting a Partition Using the CLI method

    This method is relatively shorter than the previous one. However, this method is only recommended for advanced users, as it comes with a higher chance of damage to the system. This method is less failsafe than GParted, so you must be super careful.

    Connect the device to the computer. By default, most Linux distros will mount all the partitions of the device. Assuming it hosts a single partition, it will be mounted automatically. Check out the list of mounted filesystems below.

    It is not allowed to format a partition that is currently mounted. To unmount the partition, run the following command.

    Now, you can format the partition with your desired filesystem. Run the command that suits your needs.

    Источник

  • Оцените статью
    Adblock
    detector