Linux

How to Install and Use sfdisk on Linux

How to Install and Use sfdisk on Linux

sfdisk is a command-line utility in Linux used for partitioning disk drives. It allows users to create, modify, and display partition tables without needing a graphical interface. This guide will walk you through the installation and usage of sfdisk, helping you manage disk partitions effectively.

Prerequisites
Before you begin, ensure you have:

A Linux distribution installed (this guide is applicable to most).
Sudo privileges to install packages and modify disk partitions.
A backup of important data, as partitioning can result in data loss if done incorrectly.

Step 1: Install sfdisk
Most Linux distributions come with sfdisk pre-installed as part of the util-linux package. However, if it’s not installed, you can easily add it.

On Debian/Ubuntu-based Systems
Open the terminal and run:

sudo apt update
sudo apt install util-linux

On Red Hat/CentOS-based Systems
For Red Hat or CentOS, use:

sudo yum install util-linux

On Fedora
On Fedora, execute:

sudo dnf install util-linux

On Arch Linux
For Arch Linux, simply run:

sudo pacman -S util-linux

Once installed, you can verify the installation by checking the version:

sfdisk –version

Step 2: Using sfdisk
Displaying Disk Partitions
To list the current partition layout of a disk, use the following command, replacing /dev/sdX with your actual device name:

sudo sfdisk -l /dev/sdX

This command will show you the partition table for the specified disk. To find the correct device name, you can use lsblk:

lsblk

Creating a New Partition
To create a new partition, you need to provide a script file that specifies the partition layout. For example, create a text file named partitions.txt:

nano partitions.txt

Add the following lines to the file:

/dev/sdX1 : start= 2048, size= 102400, type=83
/dev/sdX2 : start= 104448, size= 204800, type=83

In this example, replace /dev/sdX with your disk name, and adjust the start, size, and type as needed. The type 83 represents a Linux filesystem.

Once your file is ready, you can apply it using:

sudo sfdisk /dev/sdX < partitions.txt Modifying Existing Partitions To modify an existing partition, you can use the same text file method. First, list the current partitions to understand their layout: bash Copy code sudo sfdisk -d /dev/sdX > current_partitions.txt

Edit the current_partitions.txt file:

nano current_partitions.txt

Make the necessary changes, then apply the updated partition layout:

sudo sfdisk /dev/sdX < current_partitions.txt Backing Up and Restoring Partition Tables You can easily back up your partition table with: bash Copy code sudo sfdisk -d /dev/sdX > backup_partitions.txt

To restore the partition table from the backup, use:

sudo sfdisk /dev/sdX < backup_partitions.txt

Example: Creating a New Partition
Suppose you want to create a new partition on /dev/sdb. First, check the existing partitions:

sudo sfdisk -l /dev/sdb

Next, create a layout file, for example, new_partition.txt:

/dev/sdb1 : start= 2048, size= 204800, type=83

Then apply the layout:

sudo sfdisk /dev/sdb < new_partition.txt

Step 3: Formatting the New Partition
After creating a new partition, you’ll need to format it before use. For example, to format /dev/sdb1 as ext4:

sudo mkfs.ext4 /dev/sdb1

Step 4: Mounting the Partition
Once formatted, you can mount the new partition. Create a mount point:

sudo mkdir /mnt/new_partition

Then mount it:

sudo mount /dev/sdb1 /mnt/new_partition

To make the mount persistent across reboots, add an entry to /etc/fstab:

sudo nano /etc/fstab

Add the following line:

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

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.

Main Page

Conclusion

You have successfully installed and learned how to use sfdisk on Linux to manage disk partitions. This powerful tool allows for efficient partition management directly from the command line, making it suitable for both novice and advanced users.

For further reading and advanced options, consult the official man page:

man sfdisk

Make sure to always back up your data before making changes to disk partitions, as errors can lead to data loss.

Related Articles

Leave a Reply

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

Back to top button