Linux

How to Manage Disk Partitions in Linux Using the parted Command

How to Manage Disk Partitions in Linux Using the parted Command

Managing disk partitions is a crucial task in Linux, especially for system administrators and users who want to optimize their disk usage. One of the most versatile tools for managing disk partitions is parted. This guide will walk you through using the parted command to create, delete, resize, and manage disk partitions effectively.

What is parted?
parted is a command-line utility used to manage disk partitions on Linux. Unlike older tools such as fdisk, which only works with MBR (Master Boot Record) disks, parted supports both MBR and GPT (GUID Partition Table) disks, making it a flexible choice for modern systems.

Prerequisites
Before you start managing partitions, ensure you have:

A Linux-based operating system.
parted installed on your system. It is typically pre-installed on most distributions. If not, you can install it using your package manager.
To check if parted is installed, run:

parted –version

If it’s not installed, you can install it using:

On Ubuntu/Debian

sudo apt update
sudo apt install parted

On Fedora

sudo dnf install parted

On Arch Linux

sudo pacman -S parted

Starting parted
To begin using parted, you need to run it with root privileges. You can specify a disk to manage by providing the disk name (e.g., /dev/sda).

sudo parted /dev/sda

Once you enter parted, you will see a prompt that looks like this:

(parted)
To exit parted, type quit.

 

Common Parted Commands
Here are some essential commands and operations you can perform using parted.

1. Displaying Partition Information
To view the current partitions on the selected disk, use the print command:

(parted) print

This command will display a list of all partitions, their sizes, types, and file systems.

2. Creating a New Partition
To create a new partition, you first need to resize or free up space. After ensuring there is unallocated space, use the following command:

(parted) mkpart primary ext4 10GB 20GB

In this example, you are creating a primary partition of type ext4 that starts at 10GB and ends at 20GB. Adjust the size according to your needs.

3. Resizing a Partition
To resize an existing partition, use the resizepart command. For example, to resize partition 1 to 30GB:

(parted) resizepart 1 30GB

Make sure that the partition is unmounted before resizing it.

4. Deleting a Partition
To delete a partition, use the rm command followed by the partition number:

(parted) rm 1

This command deletes partition 1. Be cautious when deleting partitions, as this action is irreversible.

5. Changing the Partition Type
You can change the file system type of a partition using the set command. For instance, to change partition 1 to swap:

(parted) set 1 swap on

6. Formatting a Partition
After creating a partition, you may need to format it. While parted does not directly format partitions, you can use commands like mkfs outside of parted. For example:

sudo mkfs.ext4 /dev/sda1

Replace /dev/sda1 with the appropriate partition identifier.

7. Checking and Repairing Filesystems
To check the filesystem integrity, use the fsck command:

sudo fsck /dev/sda1

This command checks and repairs the specified partition.

Tips for Using parted
Backup Data: Always back up important data before modifying disk partitions to avoid data loss.
Unmount Partitions: Ensure that partitions are unmounted before performing operations like resizing or deleting.
Check Free Space: Use the print free command to display free space on the disk.

If you would like to improve yourself in server management, you can purchase a server from our site, experiment and improve yourself in an affordable and reliable environment. I wish you good luck.

Thank you for visiting our site. If you want, you can read our article on dd and gparted by clicking the link below.

Install and Use Open Source Disk Partitioning Tool GParted in Linux

How to Use dd in Linux Without Destroying Your Disk

How to Install and Use Duplicity to Automate Backups on Linux

Conclusion

Managing disk partitions is an essential skill for Linux users and administrators. The parted command offers a powerful and flexible way to create, resize, and manage disk partitions on both MBR and GPT disks. By following this guide, you should have a solid understanding of how to use parted effectively.

For more information and advanced usage, refer to the official parted documentation by visiting the GNU Parted Manual.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button