How to do partitions of disk in ubuntu #server using cli?

 To partition a drive for installing Ubuntu Server using the command-line interface (CLI), you can use the `fdisk` or `parted` command-line tools. Here, I'll explain how to use both tools to partition a drive:


### Using `fdisk`


1. **Identify the Drive:**

   

   First, identify the drive you want to partition. You can list all available drives using the `lsblk` command.


   ```sh

   lsblk

   ```


2. **Start `fdisk`:**

   

   Start `fdisk` for the drive you want to partition. Replace `/dev/sdX` with the appropriate drive identifier.


   ```sh

   sudo fdisk /dev/sdX

   ```


3. **Create Partitions:**

   

   - Press `n` to create a new partition.

   - Choose the partition type (primary or extended).

   - Specify the starting and ending sectors or the partition size.

   - Repeat the above steps to create additional partitions if needed.


4. **Set Partition Type (Optional):**

   

   If you're creating a bootable partition, you might need to set its type to EFI System (for UEFI) or Linux filesystem (for BIOS/MBR).


   - Press `t` to change the partition type.

   - Enter the partition number.

   - Enter the appropriate partition type code (e.g., `1` for EFI System).


5. **Write Changes and Quit:**

   

   - After creating partitions, press `w` to write changes to the disk and exit `fdisk`.


### Using `parted`


1. **Identify the Drive:**

   

   Similarly, identify the drive you want to partition using `lsblk`.


   ```sh

   lsblk

   ```


2. **Start `parted`:**

   

   Start `parted` for the drive you want to partition.


   ```sh

   sudo parted /dev/sdX

   ```


3. **Create Partitions:**

   

   - Use the `mkpart` command to create a new partition.

   - Specify the partition type (primary or logical), start point, and end point.


   ```sh

   (parted) mkpart primary ext4 1MiB 10GiB

   ```


   This command creates a primary partition of type ext4 starting at 1MiB and ending at 10GiB.


4. **Set Partition Name (Optional):**

   

   You can optionally set a name for the partition using the `name` command.


   ```sh

   (parted) name 1 Ubuntu

   ```


   This command sets the name of the first partition to "Ubuntu".


5. **Set Partition Flag (Optional):**

   

   If you're creating a bootable partition, you might need to set its boot or ESP (EFI System Partition) flag.


   ```sh

   (parted) set 1 boot on

   ```


   This command sets the boot flag on the first partition.


6. **Quit:**

   

   After creating partitions, quit `parted`.


   ```sh

   (parted) quit

   ```


### Final Steps


After partitioning the drive, you can format the partitions using filesystem-specific tools like `mkfs.ext4` for ext4 filesystems, `mkfs.fat` for FAT filesystems, etc. Then proceed with installing Ubuntu Server on the desired partition. Remember to adjust the partition sizes and types according to your requirements and the installation needs.

Comments