Environment
This tutorial was created using the Slackware 15 terminal.
Quick Guide
If you already know what you're doing and just need additional information or have forgotten a command, this quick guide is for you.
# bash
# List disks and partitions lsblk # Manage partitions gdisk /dev/sdX # In gdisk p List partitions ? Show help n Add partition d Remove partition c Change partition name t Change partition type x Enable advanced mode w Write changes to disk # Partition types 8300 Linux filesystem (ext4, xfs, etc) 8200 Linux swap 0700 Microsoft basic data (NTFS, FAT) # Format with ext4 mkfs -t ext4 /dev/sdc1 # Format with NTFS mkntfs -f /dev/sdc1
Next, a complete tutorial with examples and detailed explanations about disk partitioning and formatting in Linux.
Disk and Partition Management
To start, list the disks and their partitions using the lsblk command.
# bash
lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sda 8:0 0 465.8G 0 disk ├─sda1 8:1 0 10G 0 part /tmp ├─sda2 8:2 0 10G 0 part /var └─sda3 8:3 0 445.8G 0 part /home sdb 8:16 0 29.8G 0 disk ├─sdb1 8:17 0 100M 0 part /boot/efi └─sdb2 8:18 0 29.7G 0 part / sdc 8:32 1 14.6G 0 disk └─sdc1 8:33 1 14.6G 0 part
In this article, the disk to be manipulated is identified as sdc. It has a pre-existing partition identified as sdc1. To correctly identify the disk you want to manipulate, check the size, existing partitions, and the mount point (if any).
If you still have doubts, run the lsblk command with the disk disconnected, then connect the disk and run the command again. This way, you can compare the outputs and correctly identify the disk.
MBR or GPT
Whenever supported, the best choice is to use the GPT partition table. It offers greater security, support for larger capacity disks, and allows for a larger number of partitions.
The MBR table should only be used in specific cases, such as: Older systems that use legacy BIOS or external disks that will be used on multiple hardware, to ensure maximum compatibility.
To manipulate partitions on GPT disks, use gdisk, while for MBR, use fdisk. To start partitioning, execute gdisk /dev/sdX, where X is the specific identification of your disk.
# bash
gdisk /dev/sdc
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help):
Now we are inside the gdisk command console. Clarifying:
GPT: present The disk is configured with GPT.
MBR: protective There is a protective MBR, which prevents older tools (that only recognize MBR) from interpreting the disk as unpartitioned and attempting to format it.
Inside the gdisk console, you can use the following commands:
- Type p to list the current partitions on the disk.
- Type ? to display the list of available commands.
- Type x to enable advanced features (experts only).
- Type ? again to see the full list of advanced commands.
Destroying the GPT Partition Table
Let's completely remove the partition tables from the disk, both GPT and the protective MBR. Why do this? To change the partition table. By destroying it, the disk will be unformatted, as if it came from the factory, allowing you to choose between creating an MBR partition table with fdisk or recreating the GPT table with gdisk.
# bash
gdisk /dev/sdc Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Command (? for help): x # Enable advanced commands Expert command (? for help): z # Remove GPT table About to wipe out GPT on /dev/sdc. Proceed? (Y/N): y # Confirm removal GPT data structures destroyed! You may now partition the disk using fdisk or other utilities. Blank out MBR? (Y/N): y # Remove protective MBR
At the end of the command, gdisk will exit. Enter again and notice that the disk no longer has any partition table.
# bash
gdisk /dev/sdc
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries in memory.
Command (? for help):
When creating new partitions on a disk without partition tables, gdisk will automatically create the GPT structure, along with the protective MBR.
Creating Partitions in Linux
Let's start by creating a single partition for Linux, using all the available space on the disk.
# bash
gdisk /dev/sdc Command (? for help): n # To add a new partition Partition number (1-128, default 1): Enter ↲ # Partition number First sector (34-30529502, default = 2048) or {+-}size{KMGTP}: Enter ↲ Last sector (2048-30529502, default = 30529502) or {+-}size{KMGTP}: Enter ↲ Current type is 8300 (Linux filesystem) Hex code or GUID (L to show codes, Enter = 8300): Enter ↲ Command (? for help): p # List partitions Number Start (sector) End (sector) Size Code Name 1 2048 30529502 14.6 GiB 8300 Linux filesystem Command (? for help): w # Write changes to disk Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): y # Confirm OK; writing new GUID partition table (GPT) to /dev/sdc. The operation has completed successfully.
- n Starts the process to add a new partition
- Partition number (1-128, default 1) Identification number of the partition within the partition table. Allows individual manipulation of each partition. Pressing Enter ↲ will use the next available sequential number.
- First sector First sector from which the partition will be created (start of the partition). Unless you know what you're doing, press Enter ↲ to use the first available sector.
- Last sector Last sector, up to where the partition should extend. This option comes pre-configured with the last available sector. If you want to create a single partition with all the disk space, just press Enter ↲.
- {+-}size{KMGTP} Kilo Mega Giga Tera Peta Bytes. To create partitions with custom sizes, set the value of the last sector with the desired size, for example, +250G. This will create a 250 Gigabyte partition.
- Current type is 8300 (Linux filesystem) Indicates the current partition type.
- Hex code or GUID (L to show codes, Enter = 8300): To list other types, press L + Enter ↲. A search prompt will be available, type what you're looking for and press Enter ↲. Press Enter ↲ without typing anything to display the full list of available types.
- p lists partitions, w writes the partition to disk, and y confirms.
When it comes to creating and deleting partitions, all changes configured in gdisk are only written to disk after using the w (write) option. Therefore, if you make any mistakes during the process, simply exit the program (q) to discard the changes.
On the other hand, when you finish the desired changes, don't forget to write the changes to disk using the w option before exiting. Otherwise, all your work will be lost.
Here are the codes for the most common partition types for Linux and Windows:
- 8300 (Linux filesystem) To use Linux filesystems, such as ext4 and xfs.
- 8200 (Linux swap) To create a Linux swap partition.
- 0700 (Microsoft basic data) To create partitions compatible with Windows, such as NTFS or FAT.
Search for other systems.
Changing the Partition Name
In the gdisk console, use the c command followed by the partition index (if there is more than one). Type the new name and write the changes with w. Note that the new name will only be recognized by the system after formatting the partition.
Deleting a Partition
Inside gdisk, use the d option and then the index number of the partition you want to delete. Write the changes to disk with w.
If there is only a single partition, it will be deleted automatically, without the need to specify the index number. Use p to list the partitions and confirm if the changes were applied correctly.
Before Formatting
Partitions in Linux are identified by the disk name followed by a sequential number. In our example, the disk is sdc, and its partitions would be sdc1, sdc2, and so on.
Use the lsblk command to find the name assigned to your partition. In our case, it will be considered sdc1. Make sure the partitions were created with the appropriate types before formatting them.
Formatting with ext4
# bash
# Format as ext4 mkfs -t ext4 /dev/sdc1 # If a filesystem is present, a warning may be displayed /dev/sdc1 contains a vfat file system labelled 'WINDOWS' Proceed anyway? (y,N) y # Confirm
After completion, the partition will be ready for use on Linux.
If this is your case, identify the mount point and change the permission to your user and group:
# bash
lsblk sdc 8:32 1 14.6G 0 disk └─sdc1 8:33 1 14.6G 0 part /run/media/your_user/your_partition chown -R your_user:your_group /run/media/your_user/your_partition
Changing Type and Name
Continuing from the previous example, it will be demonstrated how to change the type and name of the partition before formatting with NTFS. If you have already created the partition with the appropriate type, skip the next step.
# bash
gdisk /dev/sdc Command (? for help): p # List partitions Number Start (sector) End (sector) Size Code Name 1 2048 30529502 14.6 GiB 8300 Linux Command (? for help): t # Change type Using 1 Current type is 8300 (Linux filesystem) Hex code or GUID (L to show codes, Enter = 8300): 0700 # New type Changed type of partition to 'Microsoft basic data' Command (? for help): c # Change name Using 1 Enter name: Windows # New name Command (? for help): p # List partitions Number Start (sector End (sector) Size Code Name 1 2048 30529502 14.6 GiB 0700 Windows Command (? for help): w # Write to disk Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): y # Confirm OK; writing new GUID partition table (GPT) to /dev/sdc. The operation has completed successfully.
Formatting with NTFS
# bash
mkntfs -f /dev/sdc1
Cluster size has been automatically set to 4096 bytes.
Creating NTFS volume structures.
mkntfs completed successfully. Have a nice day.
Use the -f or --fast option for a quick format. Otherwise, the process may take hours, depending on the disk size.
MBR Partition Table
To manage partitions with the MBR table, use the fdisk command. Below, a complete practical example will be demonstrated, which includes:
- Removing the GPT partition table from a disk.
- Creating two new partitions using the MBR table.
- Formatting them, one with ext4 and the other with NTFS.
This example uses all the knowledge base provided in this tutorial.
# bash
# Our disk: sdc gdisk /dev/sdc # Enable advanced commands Command (? for help): x # Destroy GPT structure and exit Expert command (? for help): z # Confirm About to wipe out GPT on /dev/sdc. Proceed? (Y/N): y GPT data structures destroyed! You may now partition the disk using fdisk or other utilities. # Remove protective MBR Blank out MBR? (Y/N): y # Start fdisk fdisk /dev/sdc # Create a new partition Command (m for help): n # Set type as primary Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p # Set partition index (default: 1) Partition number (1-4, default 1): Enter ↲ # Set first sector (default: 2048) First sector (2048-30529535, default 2048): Enter ↲ # Set partition size to 7 Gigabytes Last sector, +/-sectors or +/-size{K,M,G,T,P}: +7G Created a new partition 1 of type 'Linux' and of size 7 GiB. Partition #1 contains a ntfs signature. # Remove old configuration Do you want to remove the signature? [Y]es/[N]o: y The signature will be removed by a write command. # Create a second partition Command (m for help): n # Set type as primary Partition type p primary (1 primary, 0 extended, 3 free) e extended (container for logical partitions) Select (default p): p # Set partition index (default: 2) Partition number (2-4, default 2): Enter ↲ # Set first sector (default: next available) First sector (14682112-30529535, default 14682112): Enter ↲ # Set last sector, using the rest of the disk Last sector, +/-sectors or +/-size{K,M,G,T,P}: Enter ↲ Created a new partition 2 of type 'Linux' and of size 7.6 GiB. # Show newly created partitions Command (m for help): p Device Boot Start End Sectors Size Id Type /dev/sdc1 2048 14682111 14680064 7G 83 Linux /dev/sdc2 14682112 30529535 15847424 7.6G 83 Linux Filesystem/RAID signature on partition 1 will be wiped. # Change partition 2 type to NTFS Command (m for help): t # Choose partition number 2 Partition number (1,2, default 2): 2 # Set type as HPFS/NTFS/exFAT (code 07) Hex code or alias (type L to list all): 07 Changed type of partition 'Empty' to 'HPFS/NTFS/exFAT'. # List partitions Command (m for help): p Device Boot Start End Sectors Size Id Type /dev/sdc1 2048 14682111 14680064 7G 83 Linux /dev/sdc2 14682112 30529535 15847424 7.6G 7 HPFS/NTFS/exFAT # Write changes to disk Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks. # Formatting partition 1 with ext4 # -O 64bit: Enable 64-bit filesystem support mkfs -t ext4 -O 64bit /dev/sdc1 # Confirm formatting mke2fs 1.46.5 (30-Dec-2021) /dev/sdc1 contains `DOS/MBR boot sector...` Proceed anyway? (y,N) y Creating filesystem with 1835008 4k blocks and 458752 inodes Filesystem UUID: 1b5d99d0-9647-4cd5-96e4-b91f49ffab0f Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done # Formatting partition 2 with NTFS mkntfs -f /dev/sdc2 Cluster size has been automatically set to 4096 bytes. Creating NTFS volume structures. mkntfs completed successfully. Have a nice day. # Label the ext4 partition as "Linux" e2label /dev/sdc1 "Linux" # Label the NTFS partition as "Windows" ntfslabel /dev/sdc2 "Windows"
Partitions being recognized in KDE ↓
References
The basis of this article was built with personal knowledge and experience, combined with research and testing.