This tutorial provides a comprehensive guide to managing storage in Ubuntu. You’ll learn how to view disk usage, partition drives, format filesystems, mount storage devices, and automate storage tasks. Each step builds on the previous one, with practical examples and engaging exercises to reinforce your learning.
Step 1: Step 1: View Disk Usage with `df` and `du`
Before managing storage, you need to understand how your disk space is being used. Use the df command to view disk usage across all mounted filesystems and du to check the size of specific directories.
- df -h displays disk usage in a human-readable format.
- du -sh /path/to/directory shows the total size of a directory.
df -h
du -sh /home/user/DocumentsPractice Exercise
Your server is running out of space. Use df and du to identify which directory is consuming the most space. Write a command to find the top 5 largest directories in /var.
Show Solution
# Solution: Find the top 5 largest directories in /var
du -ah /var | sort -rh | head -n 5
# Explanation: `du -ah` lists all files and directories with sizes, `sort -rh` sorts them in reverse order by size, and `head -n 5` shows the top 5 results.Step 2: Step 2: Partition a Disk with `fdisk`
Partitioning allows you to divide a disk into logical sections. Use fdisk to create, delete, and manage partitions.
- List disks with
lsblk. - Open a disk for partitioning with
sudo fdisk /dev/sdX. - Use
nto create a new partition,dto delete, andwto save changes.
sudo fdisk /dev/sdb
# Follow the prompts to create a new partitionPractice Exercise
You have a new 1TB disk (/dev/sdc). Partition it into two equal-sized partitions: one for data storage and one for backups. Write the steps and commands you would use.
Show Solution
# Solution: Partition /dev/sdc into two equal-sized partitions
sudo fdisk /dev/sdc
# Press 'n' to create a new partition, then 'p' for primary.
# Set the first partition size to 500GB.
# Repeat for the second partition.
# Press 'w' to save changes.
# Explanation: This creates two 500GB partitions for data and backups.Step 3: Step 3: Format a Partition with `mkfs`
After partitioning, format the partition with a filesystem. Use mkfs to create a filesystem like ext4, which is commonly used in Ubuntu.
- sudo mkfs.ext4 /dev/sdX1 formats the partition with ext4.
sudo mkfs.ext4 /dev/sdb1Practice Exercise
You need to format a partition (/dev/sdd1) for a database that requires high performance. Research and choose a filesystem optimized for speed (e.g., XFS or ext4). Write the command to format it.
Show Solution
# Solution: Format /dev/sdd1 with XFS for high performance
sudo mkfs.xfs /dev/sdd1
# Explanation: XFS is optimized for large files and high-speed operations, making it ideal for databases.Step 4: Step 4: Mount a Filesystem
Mounting makes a filesystem accessible. Use mount to attach a partition to a directory.
- sudo mount /dev/sdX1 /mnt/mydrive mounts the partition to /mnt/mydrive.
- Use umount to unmount.
sudo mount /dev/sdb1 /mnt/mydrivePractice Exercise
You want to mount a partition (/dev/sde1) to /data and ensure it persists after reboot. Write the commands to mount it and add it to /etc/fstab.
Show Solution
# Solution: Mount /dev/sde1 to /data and add to /etc/fstab
sudo mount /dev/sde1 /data
# Add this line to /etc/fstab:
/dev/sde1 /data ext4 defaults 0 2
# Explanation: The `defaults` option ensures the partition is mounted with standard settings, and `0 2` specifies the dump and fsck order.Step 5: Step 5: Automate Storage Tasks with `cron`
Automate repetitive storage tasks like backups or cleanup using cron. Edit the crontab with crontab -e to schedule tasks.
- Example: 0 2 * * * /path/to/backup.sh runs a backup script daily at 2 AM.
crontab -e
# Add the following line to schedule a daily backup:
0 2 * * * /home/user/backup.shPractice Exercise
You need to clean up temporary files in /tmp every Sunday at midnight. Write a cron job to delete files older than 7 days.
Show Solution
# Solution: Cron job to clean up /tmp
0 0 * * 0 find /tmp -type f -mtime +7 -exec rm {} \;
# Explanation: This cron job runs at midnight every Sunday and deletes files in /tmp older than 7 days.Step 6: Step 6: Monitor Disk Health with `smartctl`
Monitor disk health to prevent data loss. Use smartctl from the smartmontools package to check disk status.
- Install with sudo apt install smartmontools.
- Check disk health with sudo smartctl -a /dev/sdX.
sudo apt install smartmontools
sudo smartctl -a /dev/sdaPractice Exercise
Your server has multiple disks. Write a script to check the health of all disks and log the results to /var/log/disk_health.log.
Show Solution
# Solution: Script to check disk health
#!/bin/bash
for disk in /dev/sd{a..d}; do
echo "Checking $disk..." >> /var/log/disk_health.log
sudo smartctl -a $disk >> /var/log/disk_health.log
done
# Explanation: This script iterates through disks /dev/sda to /dev/sdd and logs their health status.Step 7: Step 7: Resize a Filesystem with `resize2fs`
Resize a filesystem to use additional space or shrink it. Use resize2fs for ext4 filesystems.
- Resize after expanding a partition: sudo resize2fs /dev/sdX1.
sudo resize2fs /dev/sdb1Practice Exercise
You expanded a partition (/dev/sdf1) from 100GB to 200GB. Write the commands to resize the filesystem to use the new space.
Show Solution
# Solution: Resize /dev/sdf1 to use the new space
sudo resize2fs /dev/sdf1
# Explanation: This command resizes the ext4 filesystem to fill the expanded partition.Step 8: Step 8: Use LVM for Flexible Storage Management
Logical Volume Manager (LVM) allows flexible storage management. Create physical volumes, volume groups, and logical volumes.
- Create a physical volume: sudo pvcreate /dev/sdX.
- Create a volume group: sudo vgcreate myvg /dev/sdX.
- Create a logical volume: sudo lvcreate -L 50G -n mylv myvg.
sudo pvcreate /dev/sdb
sudo vgcreate myvg /dev/sdb
sudo lvcreate -L 50G -n mylv myvgPractice Exercise
You have two disks (/dev/sdg and /dev/sdh). Create a volume group named datavg and a 100GB logical volume named datalv. Write the commands.
Show Solution
# Solution: Create a volume group and logical volume
sudo pvcreate /dev/sdg /dev/sdh
sudo vgcreate datavg /dev/sdg /dev/sdh
sudo lvcreate -L 100G -n datalv datavg
# Explanation: This creates a 100GB logical volume using space from both disks.Step 9: Step 9: Backup and Restore with `rsync`
Use rsync for efficient backups. It synchronizes files and directories between locations.
- Backup: rsync -av /source /destination.
- Restore: Reverse the source and destination.
rsync -av /home/user/Documents /backup/Practice Exercise
You need to back up /var/www to an external drive (/mnt/backup). Write a command to perform the backup and exclude log files in /var/www/logs.
Show Solution
# Solution: Backup /var/www excluding logs
rsync -av --exclude='logs' /var/www /mnt/backup/
# Explanation: The `--exclude` option skips the `logs` directory during the backup.Step 10: Step 10: Secure Storage with Encryption
Encrypt sensitive data using LUKS (Linux Unified Key Setup).
- Encrypt a partition: sudo cryptsetup luksFormat /dev/sdX.
- Open the encrypted partition: sudo cryptsetup open /dev/sdX myencrypted.
sudo cryptsetup luksFormat /dev/sdb1
sudo cryptsetup open /dev/sdb1 myencryptedPractice Exercise
You have a partition (/dev/sdi1) for storing sensitive data. Encrypt it with LUKS, format it with ext4, and mount it to /secure. Write the commands.
Show Solution
# Solution: Encrypt, format, and mount /dev/sdi1
sudo cryptsetup luksFormat /dev/sdi1
sudo cryptsetup open /dev/sdi1 secure
sudo mkfs.ext4 /dev/mapper/secure
sudo mount /dev/mapper/secure /secure
# Explanation: This encrypts the partition, formats it, and mounts it to /secure.Sign in to take Cornell notes on this lesson — they save automatically and stay with your account.