How to Resize a Partition in Linux?
How to Resize Partition in Linux?
We can resize partition in linux by applying the following methods:
- Discover how to resize partition in Linux seamlessly using powerful tools and commands.
- Safely extend or shrink partitions to meet changing storage needs.
- Prioritize data backup before proceeding and exercise caution to avoid any potential data loss.
- Explore a comprehensive guide to understand concepts and ensure a smooth partition resizing process.
Important Commands
1. fdisk
The fdisk command is a powerful command-line tool that allows you to create, modify, and delete disk partitions on your system. It provides a straightforward interface to work with your disk's partition table. To start using fdisk, open a terminal and run the command with the appropriate disk device. For example, to work with the first hard disk on your system, you would use the following command:
Here are the key operations you can perform using fdisk:
Displaying the Partition Table
To view the current partition table, enter p at the fdisk prompt and press Enter. This will display detailed information about each partition, including their sizes, types, and file systems.
Deleting a Partition
To delete a partition, enter d at the fdisk prompt, followed by the partition number you wish to delete. For example, d 1 will delete the first partition on the disk.
Creating a New Partition
To create a new partition, enter n at the fdisk prompt. fdisk will then guide you through the process of creating a new partition by prompting for details such as the partition type, starting sector, and size.
Saving Changes and Exiting
After performing the desired operations, make sure to save the changes you made to the partition table by entering w at the fdisk prompt. This will write the changes to the disk and exit fdisk.
2. resize2fs
After resize partition in Linux using fdisk, resizing the file system inside the partition is necessary to reflect the changes. The resize2fs command is used to resize ext2, ext3, and ext4 file systems, which are the most commonly used file system types in Linux. The usage of resize2fs is straightforward:
Replace /dev/sdaX with the appropriate partition device name that you want to resize.
It's important to note that the resize2fs command can only be used to resize ext2, ext3, and ext4 file systems. If you're working with a different file system type, you'll need to use a different command specific to that file system to resize partition in Linux.
3. df
The df command (disk free) is used to display information about the disk space usage on file systems. It provides an overview of mounted file systems' total, used, and available disk space. The command syntax is as follows:
Running df without any options or arguments will display the disk space usage for all mounted file systems. By default, df displays sizes in 1K blocks and uses the metric system (base 10) to represent the disk space.
Commonly used options with df to resize partition in Linux include:
- -h or --human-readable:
Displays sizes in a human-readable format (e.g., 1K, 1M, 1G). - -a or --all:
Displays information about all file systems, including the ones that are not mounted. - -i or --inodes:
Shows inode usage instead of block usage.
4. lsblk
The lsblk command lists information about block devices (disks) on your system, including partitions and their relationships. It provides a tree-like representation of the block devices and their attributes. The basic command syntax is:
Running lsblk without any options or arguments will display a list of all block devices and their major and minor numbers, size, and mount points (if applicable).
Commonly used options with lsblk include:
- -a or --all:
Displays all devices, including empty ones. - -l or --list:
Lists devices in a list format, rather than a tree format. - -o or --output <columns>:
Specifies the columns to display. For example, lsblk -o NAME, SIZE, MOUNTPOINT will display only the device name, size, and mount point columns. - -p or --paths:
Shows full device paths instead of abbreviations.
Note: The df and lsblk commands complement each other when it comes to disk space and partition information. df provides information specifically about file systems, while lsblk provides a broader view of block devices and their relationships.
Extending Or Shrinking a Linux Partition
Extending a partition allows you to increase its size and utilize additional disk space.The following steps outline the process of extending a partition on Linux:
Step 1: Identify the Partition
Before extending a partition, you need to identify the disk and the specific partition you want to extend. You can use the fdisk command as described earlier to view the current partition table and identify the partition you wish to resize.
Step 2: Back Up Your Data
Before making any changes to the partition, it's crucial to back up your important data. While resizing is generally safe, having a backup ensures that your data remains secure in case of any unexpected issues.
Step 3: Start fdisk
Open a terminal and run the fdisk command with the appropriate disk device. For example:
Step 4: Delete the Partition
Once you're in the fdisk command prompt, enter d to delete the partition you want to extend. This will remove the partition entry from the partition table, but the data inside the partition will remain intact.
Step 5: Create a New Partition
After deleting the partition, you can now create a new partition with an increased size. Enter n at the fdisk prompt and follow the instructions to create a new partition. Make sure to allocate the desired space for the resized partition.
Step 6: Save the Changes
Once you've created the new partition, save the changes by entering w at the fdisk prompt. This will write the new partition table to the disk.
Step 7: Resize the File System
After resizing the partition using fdisk, resizing the file system inside the partition is essential to reflect the new partition size. Use the resize2fs command to achieve this:
Replace /dev/sdaX with the appropriate partition device name. This command will automatically resize the file system to match the new partition size.
Shrinking a Linux Partition
Some of the steps to shrink a Linux partition are identical however here are the important steps that you need to follow:
To shrink a partition, you first need to resize the file system inside the partition to a smaller size. This step ensures that the file system doesn't occupy more space than the desired partition size. Use the resize2fs command to achieve this:
Replace /dev/sdaX with the appropriate partition device name and <new_size> with the desired size for the file system after shrinking.
Next, start fdisk. Once you're in the fdisk command prompt, enter d to delete the partition you want to shrink. This will remove the partition entry from the partition table, but the data inside the partition will remain intact.
After deleting the partition, you can now create a new partition with a reduced size. Enter n at the fdisk prompt and follow the instructions to create a new partition. Make sure to allocate the desired space for the resized partition.
That's all the steps you needed to follow.
Conclusion
- Resizing a partition in Linux is a powerful operation that allows you to adapt your disk space allocation to suit your needs.
- By using the fdisk and resize2fs commands, you can easily extend or shrink partitions, enabling you to make the most efficient use of your available disk space.
- Remember to exercise caution, and have a backup of your important data before resizing any partitions.