Aller au contenu principal

Ubuntu Server 2TB Hard Drive Partition and Format Guide

  • Fact : I got a newly installed 2TB hard drive on an Ubuntu server.
  • Problem : Needs to be partition and formated.
  • Hypothese : Using the buildin command like fdisk and mkfs, will be set up with a single partition formatted as ext4.

Prerequisites

A 2TB hard drive installed on the Ubuntu server. Administrative (root) access or sudo privileges.

Steps

1. Identify the New Hard Drive

  1. Open a terminal or SSH in your device.

  2. Run the following command to list all disk devices:

    lsblk
  3. Identify the new 2TB drive (e.g., /dev/sdb). It will typically show as unmounted with no partitions or look for the right size. Note the device name (e.g., /dev/sdb).

    Caution: Double-check the device name to avoid accidentally modifying the wrong drive.

2. Partition the Drive

  1. Start the fdisk utility for the new drive (replace /dev/sdb with your drive’s device name):

    sudo fdisk /dev/sdb
  2. At the fdisk prompt, type the following commands:

    • g to create a new GPT partition table (suitable for 2TB drives).
    • n to create a new partition.
      • Press Enter to accept the default partition number (1).
      • Press Enter to accept the default first sector.
      • Press Enter to accept the default last sector (uses the entire drive).
    • w to write the changes and exit. (It might say : The partition table has been altered)
  3. Verify the new partition:

    lsblk

    You should see a new partition (e.g., /dev/sdb1).

3. Format the Partition

  1. Format the new partition as ext4 (replace /dev/sdb1 with your partition’s name):
    sudo mkfs.ext4 /dev/sdb1
  2. Wait for the formatting to complete (this may take a few minutes).

4. Mount the Drive

  1. Create a mount point (e.g., /mnt/newdrive):
    sudo mkdir /mnt/newdrive
  2. Mount the formatted partition:
    sudo mount /dev/sdb1 /mnt/newdrive
  3. Verify the drive is mounted:
    df -h
    You should see the new drive listed with its mount point.

5. Configure Automatic Mounting

To ensure the drive mounts automatically on boot:

  1. Find the UUID of the new partition:
    sudo blkid /dev/sdb1
    Note the UUID for /dev/sdb1 (e.g., 1234-5678-9012-3456).
  2. Edit the /etc/fstab file:
    sudo nano /etc/fstab
  3. Add the following line at the end of the file (replace UUID with your partition’s UUID):
    /dev/disk/by-uuid/1234-5678-9012-3456 /mnt/newdrive ext4 defaults 0 2
  4. Save and exit (Ctrl+O, Enter, Ctrl+X).
  5. Test the fstab configuration:
    sudo mount -a
    If no errors appear, the configuration is correct.

6. Set Permissions (Optional)

To allow a specific user or group to access the drive:

  1. Change ownership (replace username with the desired user):
    sudo chown -R username:username /mnt/newdrive
  2. Adjust permissions as needed:
    sudo chmod -R 755 /mnt/newdrive

Verification

  • Check the drive’s status:
    lsblk
    df -h
  • Ensure you can read/write to the mount point:
    touch /mnt/newdrive/testfile
    ls /mnt/newdrive