What are Partitions in Linux?
In Linux, a partition is a division of a physical hard drive into separate sections, allowing you to use different file systems or manage data more efficiently. Each partition can have its own file system and be mounted at different directories. Partitions allow for the separation of system files, user data, and application files, making it easier to manage and secure data.
How Partitions Work in Linux
Partitions are used to divide storage devices into isolated sections. Each partition has its own space and may store specific data like the operating system, user files, or swap space. In Linux, partitions are usually referenced by names like /dev/sda1, /dev/sda2, etc., where:
- /dev/sda refers to the storage device.
- The number (e.g., 1, 2) refers to the partition on the device.
Key Partition Types:
- Primary Partition: The main partitions that can store data or the operating system.
- Extended Partition: Used to create more partitions if the limit of primary partitions is reached (4 partitions max on MBR disks).
- Logical Partition: Resides within an extended partition to allow more partitions.
Common File Systems for partitions include:
- ext4
- xfs
- btrfs
- swap (for virtual memory)
Important Commands of Partitions
- Mounting Partitions
- To mount a partition to a directory (e.g., /mnt):
mount /dev/sda1 /mnt
- 6. To make it permanent, add an entry in /etc/fstab.
Unmounting Partitions
umount /dev/sda1
Checking Partition Disk Usage
df -h
Checking Filesystem for Errors
fsck /dev/sda1
Resizing a Partition (Without Data Loss)
First, unmount the partition:
umount /dev/sda1
Then resize (e.g., to 100GB):
resize2fs /dev/sda1 100G
Managing Swap Partition
Enable swap:
swapon /dev/sda2
Disable swap
swapoff /dev/sda2
mkfs.ext4 /dev/sdb1
- Mount the Partition:
mount /dev/sdb1 /mnt/new_partition