In this article, “Useful LVM commands with examples,” we will explore a selection of essential LVM commands commonly encountered by Linux sysadmins, engineers, and enthusiasts
Discover 25 essential Linux commands for efficient storage management. Learn how to monitor disk usage, manage partitions, create filesystems, and optimize storage performance with detailed examples and practical tips.
Managing storage on a Linux system is crucial for maintaining performance and ensuring data integrity. Whether you’re a seasoned sysadmin or a newcomer to Linux, mastering storage management commands can make a significant difference. This guide explores 25 essential Linux commands for storage management, complete with examples and tips to help you get the most out of your system. By the end of this post, you’ll be equipped with the knowledge to efficiently manage your storage devices, optimize performance, and ensure data integrity.
1. |
The df command provides a quick overview of disk space usage. It displays the amount of available disk space on the file systems, which is essential for monitoring and managing your storage.
df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 392M 1.1M 391M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 48G 7.6G 38G 17% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/vda2 2.0G 181M 1.7G 10% /boot
tmpfs 392M 12K 392M 1% /run/user/1000
The output above is from an Ubuntu 24.04 basic server instance.
Filesystem | Size | Used | Avail | Use% | Mounted on |
---|---|---|---|---|---|
/dev/mapper/ubuntu--vg-ubuntu--lv | 48G | 7.6G | 38G | 17% | / |
Photo by admingeek from Infotechys
2. |
Use the du
command to estimate file space usage. This is particularly useful for identifying large directories and files, which can help in optimizing storage.
du -sh /var/log
The -s
option provides a summary, and -h
makes the output human-readable. Regular use of du
can help you stay on top of disk usage and prevent space shortages.
3. |
The lsblk
command lists information about all available or specified block devices. It provides a detailed tree view of your storage devices, partitions, and mount points.
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 74.2M 1 loop /snap/core22/1380
loop1 7:1 0 102.4M 1 loop /snap/lxd/28463
loop2 7:2 0 152.1M 1 loop /snap/lxd/26200
loop3 7:3 0 73.9M 1 loop /snap/core22/864
loop4 7:4 0 40.9M 1 loop /snap/snapd/20290
loop5 7:5 0 38.8M 1 loop /snap/snapd/21759
vda 253:0 0 100G 0 disk
├─vda1 253:1 0 1M 0 part
├─vda2 253:2 0 2G 0 part /boot
└─vda3 253:3 0 98G 0 part
└─ubuntu--vg-ubuntu--lv 252:0 0 49G 0 lvm /
The lsblk
command output provides a detailed view of the block devices and their mount points on the system. Here’s a brief summary:
loop0
to loop5
): These are loopback devices, often used for mounting filesystems from files. They are mounted on various /snap
directories./boot
./
).4. |
The fdisk
command is used for disk partitioning. It allows you to create, delete, and manage partitions on your storage devices.
sudo fdisk /dev/sda
After entering the command, you’ll enter an interactive menu where you can create, delete, or modify partitions. Proper partition management is critical for efficient storage utilization.
5. |
The mkfs
command is used to create a filesystem on a partition. Different filesystems have various features and performance characteristics.
sudo mkfs.ext4 /dev/sda1
This example formats the partition as an ext4 filesystem, which is a common choice for Linux systems due to its robustness and performance.
6. |
To access a filesystem, you need to mount it. The mount
command attaches the filesystem to the system’s directory tree.
sudo mount /dev/sda1 /mnt
This command mounts the partition to the /mnt
directory. Properly mounting filesystems ensures that data is accessible and managed correctly.
7. |
Use the umount
command to unmount a filesystem. This is necessary before removing or making changes to the storage device.
sudo umount /mnt
Ensure no processes are using the filesystem before unmounting it to avoid data corruption.
8. |
The blkid
command displays or modifies block device attributes. It provides essential information like UUIDs and filesystem types.
sudo blkid
This command lists all block devices along with their UUIDs and other attributes, which is useful for scripts and automation.
9. |
The parted
command is another tool for partitioning disks. It supports both GPT and MBR partition tables.
sudo parted /dev/sda
It provides an interactive interface for managing partitions, making it a versatile tool for disk management.
10. |
To resize a filesystem, use the resize2fs
command. This is useful when you need to adjust the size of your partitions.
sudo resize2fs /dev/sda1 40G
This resizes the filesystem on /dev/sda1
to 40GB, allowing you to optimize storage allocation.
11. |
The tune2fs
command adjusts tunable filesystem parameters. This can enhance performance and reliability.
sudo tune2fs -l /dev/sda1
This command lists the filesystem parameters of /dev/sda1
. You can modify these parameters to suit your needs.
12. |
Use e2fsck
to check a filesystem for errors. Regular checks help maintain filesystem integrity.
sudo e2fsck -f /dev/sda1
The -f
option forces a check even if the filesystem appears clean, ensuring thorough verification.
13. |
Remount a filesystem with different options using mount
. This can be useful for changing mount parameters without unmounting.
sudo mount -o remount,ro /mnt
This remounts the filesystem at /mnt
as read-only, useful for maintenance tasks.
14. |
The swapoff
command disables swapping on a device. This is necessary before resizing or removing a swap partition.
sudo swapoff /dev/sda2
Managing swap space effectively can improve system performance.
15. |
The swapon
command enables swapping on a device. This activates the swap space, providing additional virtual memory.
sudo swapon /dev/sda2
This command activates the swap space on /dev/sda2
, ensuring efficient memory usage.
16. |
The mkswap
command initializes a swap area. This is essential for creating new swap partitions.
sudo mkswap /dev/sda2
This sets up the swap space on /dev/sda2
, preparing it for use.
17. |
Use the lvcreate
command to create a logical volume in a volume group. Logical volumes provide flexibility in storage management.
sudo lvcreate -L 10G -n myvol myvg
This creates a 10GB logical volume named myvol
in the volume group myvg
, enabling better storage utilization.
18. |
The lvextend
command extends the size of a logical volume. This is useful when you need more storage space.
sudo lvextend -L +5G /dev/myvg/myvol
This increases the size of myvol
by 5GB, allowing you to expand storage without downtime.
19. |
The lvremove
command deletes a logical volume. Use this command with caution, as it permanently removes the logical volume.
sudo lvremove /dev/myvg/myvol
20. |
The vgcreate
command creates a volume group. Volume groups allow you to pool physical storage into a single logical unit.
sudo vgcreate myvg /dev/sda1
This creates a volume group named myvg
using the /dev/sda1
partition, simplifying storage management.
21. |
The vgextend
command adds a physical volume to an existing volume group. This increases the storage capacity of the volume group.
sudo vgextend myvg /dev/sdb1
This adds /dev/sdb1
to the myvg
volume group, providing additional storage space.
22. |
The vgreduce
command removes a physical volume from a volume group. This is useful for reorganizing storage.
sudo vgreduce myvg /dev/sdb1
This removes /dev/sdb1
from the myvg
volume group, allowing you to repurpose or decommission storage.
23. |
The vgremove
command deletes a volume group. Use this command when you need to dismantle a volume group.
sudo vgremove myvg
This removes the volume group myvg
, freeing up the physical storage.
24. |
The pvcreate
command initializes a physical volume for use in a volume group. This is the first step in setting up logical volumes.
sudo pvcreate /dev/sda1
This prepares /dev/sda1
as a physical volume, ready for inclusion in a volume group.
25. |
The pvs
command provides a summary of physical volumes. It offers an overview of the physical storage in your volume groups.
sudo pvs
PV VG Fmt Attr PSize PFree
/dev/vda3 ubuntu-vg lvm2 a-- <98.00g 49.00g
This command displays information about all physical volumes on the system, aiding in storage management.
PV | VG | Fmt | Attr | PSize | PFree |
---|---|---|---|---|---|
/dev/sda3 | ubuntu-vg | lvm2 | a– | 50.00g | 0 |
Mastering these 25 essential Linux commands for storage management will significantly enhance your ability to manage and maintain your system’s storage. From basic commands like df
and du
to advanced LVM management commands like lvcreate
and vgextend
, these tools are indispensable. By integrating these commands into your workflow, you’ll ensure efficient and effective storage management on your Linux system.
Proper storage management is not just about keeping your system running smoothly; it’s about optimizing performance, ensuring data integrity, and being prepared for any situation. Whether you’re expanding storage, troubleshooting issues, or planning for future growth, these commands will serve as your toolkit. Stay tuned for more Linux tips and tricks to enhance your sysadmin skills!
Related Posts
In this article, “Useful LVM commands with examples,” we will explore a selection of essential LVM commands commonly encountered by Linux sysadmins, engineers, and enthusiasts
Discover 20 useful sed commands for text processing in Linux. Learn how to perform substitutions, deletions, insertions, and more with practical examples and tips. Enhance
Discover the 25 basic Docker commands every beginner needs to know. This comprehensive guide covers everything from pulling images to managing containers, complete with examples