WindowsMacSoftwareSettingsSecurityProductivityLinuxAndroidPerformanceConfigurationApple All

How to Partition and Format Disks on Linux

Edited 1 week ago by ExtremeHow Editorial Team

System SetupDisk ManagementCommand LineStorageFile SystemsInstallationUtilitiesSysAdminPerformanceDisk Usage

How to Partition and Format Disks on Linux

This content is available in 7 different language

Managing disks on a Linux system is an essential skill for maintaining and optimizing storage. In this guide, we'll explore the process of partitioning and formatting disks on Linux, from identifying your disk to configuring partitions and choosing a file system. We'll use simple language and offer detailed examples to ensure clarity.

Understanding disk partitions

Disk partitioning is the act of dividing a disk into separate areas, known as partitions, that can be managed independently. Partitioning is important for organizing data, separating operating systems, or configuring a RAID setup. Partitioning allows you to install multiple operating systems, improve system organization, and enhance performance and security.

Identifying your disk

Before partitioning, you must identify the disk installed on your system. You can achieve this using several command-line methods:

Using the 'lsblk' command

The 'lsblk' command lists all available block devices, making it an invaluable tool for identifying disks and their partitions:

lsblk

Executing this command will give an output similar to the following:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 256G 0 disk ├─sda1 8:1 0 512M 0 part /boot ├─sda2 8:2 0 127G 0 part / └─sda3 8:3 0 128G 0 part /home sdb 8:16 0 1T 0 disk

Using the 'fdisk -l' command

The 'fdisk -l' command produces a comprehensive list of the partitions and disks available on your system:

sudo fdisk -l

This command provides detailed information about each disk and its partitions, including their size, type, and identifier.

Partitioning a disk

Once you have identified the disk you want to partition, you can use a variety of tools to partition the disk to suit your needs. In this section, we will explore some popular tools, including 'fdisk', 'parted', and the graphical tool 'GParted'.

Using 'fdisk'

The 'fdisk' utility is a command-line tool for managing disk partitions. Here's how you can use it to create partitions:

  1. Open a terminal and type the following command to start 'fdisk':
sudo fdisk /dev/sdb

Replace '/dev/sdb' with your actual disk identifier.

  1. Press 'n' to create a new partition.
  2. Select whether this is a primary or extended partition. For most purposes, select 'Primary'.
  3. Specify the partition number, first sector and last sector. Press 'Enter' to select the default values for simple configuration.
  4. Once done, press 'w' to write the changes to disk.

Remember that when using 'fdisk' the changes take effect only after writing them.

Usage of 'parted'

'Parted' is another command-line tool that provides more advanced disk management features, such as resizing and copying partitions.

  1. Launch 'parted' with your target disk selected:
sudo parted /dev/sdb
  1. Use the 'mklabel' command to set the partition label. For example, use 'gpt' for large disks:
(parted) mklabel gpt
  1. Create a new partition using the 'mkpart' command:
(parted) mkpart primary ext4 0% 100%

This command creates a partition spanning the entire disk.

Using 'GParted'

'GParted' is a graphical interface for partitioning, allowing you to see a visual representation of your disk and easily interact with partitions:

  1. Install 'GParted' by typing the following:
sudo apt-get install gparted
  1. To start 'GParted' enter the following:
sudo gparted

From the 'GParted' interface, you can create, delete, resize, or move partitions using simple click-and-drag actions.

Formatting partitions

Once the partition is set up, formatting defines how data is stored within the partition. This step assigns a file system to the partition, which is necessary for storing files and managing data efficiently.

Choosing a file system

Common file systems in Linux include:

Formatting using 'mkfs'

The 'mkfs' tool is a command-line utility used to create a file system on a partition:

Formatting in 'ext4'

To format a partition to ext4, use:

sudo mkfs.ext4 /dev/sdb1

'vfat' formatting

To format a partition to vfat, the following command will suffice:

sudo mkfs.vfat /dev/sdb1

Formatting in 'ntfs'

For NTFS, use the 'ntfsprogs' package to ensure the necessary tools are installed:

sudo apt-get install ntfs-3g sudo mkfs.ntfs /dev/sdb1

Putting partitions

After formatting, the partition is ready for use, but it needs to be mounted to access its contents through the file system.

  1. Create a directory to act as a mount point:
sudo mkdir /mnt/mydisk
  1. Mount the partition:
sudo mount /dev/sdb1 /mnt/mydisk

This command mounts the '/dev/sdb1' partition to '/mnt/mydisk', making it accessible for reading and writing.

Making the mount permanent

To persist the mount across reboots, modify the '/etc/fstab' file:

Editing '/etc/fstab'

Open '/etc/fstab' in an editor:

sudo nano /etc/fstab

Add an entry for the new partition:

/dev/sdb1 /mnt/mydisk ext4 defaults 0 2

This line ensures that '/dev/sdb1' is mounted to '/mnt/mydisk' every time the system boots.

Conclusion

Learning how to partition and format disks on Linux is a valuable skill that complements your system administration toolkit. By understanding and applying the concepts covered in this guide - identifying, partitioning, formatting, and mounting disks - you hold the keys to effective disk management, ensuring that your data is organized, accessible, and secure. Revisiting and practicing these concepts regularly will make you adept at managing your Linux system with confidence.

If you find anything wrong with the article content, you can


Comments