Linux disk management commands for system administrators
A practical reference for Linux disk management: inspecting devices, partitioning, creating filesystems, mounting persistently, and managing LVM - with the safety notes each command deserves.
Quick answer: The core Linux disk commands are lsblk and df to inspect, fdisk or parted to partition, mkfs to format, mount and /etc/fstab to attach, and pvcreate, vgcreate and lvcreate for LVM. Inspection commands are safe. Partitioning and mkfs destroy data, so confirm the device name with lsblk first.
Key takeaways
- Always run
lsblkfirst and confirm the device name. Most disk disasters are a wrong device name. fdiskandmkfschange data on disk.lsblk,dfandduonly read.- Mount by UUID in
/etc/fstab, and test withmount -abefore you reboot. - A disk that shows full after deleting files usually has a process holding the deleted file open.
- LVM lets you grow a filesystem without repartitioning.
What do the inspection commands show?
Start every disk task by looking. These commands change nothing.
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─rhel-root 253:0 0 17G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 10G 0 disk
Here sdb has no partitions and no mount point. That is your new, empty disk. The example is illustrative. Your device names and sizes will differ.
lsblk -fadds filesystem type, label and UUID.df -hshows used and free space on mounted filesystems.du -sh /var/log/*shows which directories take the space.blkidprints UUIDs, which you need for fstab.
df and du can disagree. df asks the filesystem how much it has allocated. du adds up the files it can see. The gap is explained in the troubleshooting section below.
How do you partition a disk with fdisk?
Use fdisk for MBR or GPT disks of ordinary size, and parted when you need scripting or very large disks. Both do the same job here.
# fdisk /dev/sdb
Command (m for help): g # new GPT partition table
Command (m for help): n # new partition, accept defaults
Command (m for help): w # write changes and exit
Nothing is written until you press w. Until then you can quit with q and nothing has changed. After writing, run lsblk and you should see sdb1.
How do you create a filesystem?
mkfs formats a partition. On RHEL, XFS is the default filesystem, and ext4 is also supported.
# mkfs.xfs /dev/sdb1
# mkfs.ext4 /dev/sdb1 # alternative
mkfs erases whatever filesystem was there. Check the device name twice. A common slip is formatting sda1 when you meant sdb1.
How do you mount it, and make it permanent?
Mount the filesystem on a directory, then add a line to /etc/fstab so it returns after reboot.
# mkdir /data
# mount /dev/sdb1 /data
# blkid /dev/sdb1
/dev/sdb1: UUID="3f5a1c2e-8d7b-4c1a-9e55-0b6f1d2a7c11" TYPE="xfs"
Add this line to /etc/fstab, using your own UUID:
UUID=3f5a1c2e-8d7b-4c1a-9e55-0b6f1d2a7c11 /data xfs defaults 0 0
Use the UUID, not /dev/sdb1. Device names can change between boots when you add disks, but UUIDs do not.
Then test before rebooting:
# umount /data
# mount -a
# df -h /data
If mount -a prints an error, fix fstab now. A bad fstab line can drop a server into emergency mode at boot. On systemd systems you may also need systemctl daemon-reload after editing fstab.
What is LVM and when should you use it?
Use LVM when you expect the volume to grow. It puts a flexible layer between disks and filesystems, so you can add space later without repartitioning.
# pvcreate /dev/sdb
# vgcreate datavg /dev/sdb
# lvcreate -n datalv -L 5G datavg
# mkfs.xfs /dev/datavg/datalv
To grow it later, add free space to the volume group (a new disk with vgextend) and extend the logical volume and filesystem together:
# lvextend -r -L +2G /dev/datavg/datalv
The -r flag resizes the filesystem in the same step. XFS can be grown but not shrunk. For a fuller walk-through read what LVM is and why it is useful.
Why is my disk full when I deleted the files?
If df shows a full disk but du cannot find the files, a running process is probably holding a deleted file open. Space is released only when the process closes it.
# lsof +L1
This lists open files with a link count of zero. Restart the process that holds the file, and the space returns. Also check for inode exhaustion with df -i, which can fill a disk with millions of tiny files while free space looks fine.
Common mistakes
- Editing fstab and rebooting without running
mount -a. - Using
/dev/sdXnames in fstab instead of UUIDs. - Running
mkfson a device that already holds data you need. - Trying to shrink an XFS filesystem. It is not supported.
The Red Hat Enterprise Linux documentation covers storage management for each RHEL release in detail.
Next steps
Disk management is a core part of the RHCSA exam. Practise these steps in a throwaway VM with our RHCSA EX200 course. For daily commands, see the Linux commands cheat sheet.
Related reading
Frequently Asked Questions
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Wow
0
Sad
0
Angry
0