- Unix Commands Reference
- Unix Commands - Home
addpart Command in Linux
addpart is a Linux command that informs the operating system kernel about a new partition you want to create on a disk. It doesn’t change anything on the disk, it just lets the system know that the partition exists. The addpart command is extremely useful in a case when you need to manage partitions without affecting your existing data.
Table of Contents
- How to Install addpart Command in Linux?
- Syntax for addpart Command in Linux
- Examples of addpart Command in Linux
How to Install addpart Command in Linux?
On most Linux systems, the addpart command is preinstalled. However, in case you encounter an addpart command not found error, you can install the command by installing the util-linux utility on the system. This util-linux utility includes different Linux packages and commands, including the addpart command.
The command to install the util-linux utility is different on different Linux systems, which are provided below −
Debian Based Systems
Ubuntu, Debian, and Linux Mint users can install util-linux utility on their systems from the following command −
sudo apt install util-linux -y
Alpine Linux
If you are using Alpine Linux, you can install util-linux on the system using the below-given command −
apk add util-linux
Arch Linux
For Arch Linux users, the util-linux utility can be installed from the following command −
sudo pacman -S util-linux
CentOS
If you are running CentOS, then use the following command to install util-linux on your system −
sudo yum install util-linux
Fedora
On Fedora, you can install util-linux from the below-provided command −
sudo dnf install util-linux
Syntax for addpart Command in Linux
The basic syntax to use addpart command on Linux is provided below −
addpart <disk device> <partition number> <start> <length>
Here,
- <disk device> specifies the disk device, for example /dev/sda0, /dev/sda1 and so on.
- <partition number> specifies the desired partition number.
- <start> is the beginning of the partition specified in 512-byte sectors.
- <length> is the length of the partition, which is also in 512-byte sectors.
Note − You must use all options provided above with the addpart command, skipping any option will result in getting the error “addpart: not enough arguments” on the terminal.
There are some other additional options you can use with the addpart command on Linux, such as −
- -V or --version to find the version of the addpart command on Linux.
- --help to open the addpart command help section on the terminal.
Examples of addpart Command in Linux
Let’s explore some examples of the addpart command on Linux −
Add a New Partition
If you want to inform your kernel about the presence of a new partition, you can use the addpart command. Let’s say, we have a device mounted on /dev/sda and we want to inform the Linux kernel about a new partition number 3, starting at sector 2048 and with a length of 2097152 sectors. The following command can then be executed to perform this operation on your Linux system −
sudo addpart /dev/sda 3 2048 2097152
Note − If you are not a root user, you must use sudo with the addpart command on your Linux system. The reason is that it is system system-related task and can not be done by a regular user until the user has the sudo privileges to operate.
Create a New Extended Partition
To inform the Linux kernel about creating a new extended partition that spans the entire disk on /dev/sdb, you can use the following addpart command −
sudo addpart /dev/sdb 2 0 100
Here, the partition number is 2, it starts at sector 0, and ‘100’ indicates that it will span the entire disk.
Points to Note
You must replace the partition number, start, and length with the values that best suit your case. The start and length are usually provided in the sectors and the number of sectors can be calculated by dividing the partition size in bytes by the sector size (usually 512 bytes or 4096 bytes for newer drives).
Also, ensure that you have the necessary permissions and that the disk is not in use when you run the addpart command.
Conclusion
addpart command is a handy tool for managing disk partitions on your Linux system without affecting the existing data on the disk. In this guide, we have covered the installation of the addpart command, its syntax, different available options, and provided practical examples.
By understanding how to use the addpart command, you can confidently manage partitions on your Linux system while keeping your data safe.