Storage and memory
Disk usage
df -h # free space per filesystem, human readable
df /root -H # a specific mount, powers of 1000
du -sh /var # total size of a directory
du -shc /var/* # size of each entry plus a grand total
du -h --max-depth=1 /var
Block devices
lsblk # list block devices (e.g. to find an SD card)
LVM
/etc/fstab shows how the root filesystem is mounted, which reveals whether LVM
is in use:
- A line starting with
UUID=...or/dev/sdaXis a plain partition. - A line under
/dev/mapper/...indicates LVM.
cat /etc/fstab
lvs -o +devices /dev/mapper/ubuntu--vg-ubuntu--lv
Root partition only uses part of the volume
A fresh Ubuntu LVM install often allocates only part of the disk to the root logical volume. This is deliberate — the unallocated space can be used to create another logical volume, or to grow an existing one on the fly.
Extend the root filesystem (the --resizefs option grows the filesystem after
the volume, in one step):
# Add 5 GB
sudo lvextend --resizefs -L +5G ubuntu-vg/ubuntu-lv
# Use all remaining free space
sudo lvextend --resizefs -l +100%FREE ubuntu-vg/ubuntu-lv
Without --resizefs, the logical volume grows but the filesystem inside it does
not — you then have to run the filesystem's own grow command, for example
resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv (ext4) or xfs_growfs / (XFS).
Memory
free -h # memory and swap usage
htop # interactive process/memory viewer
# Percentage of memory free
free | awk '/Mem/ { printf("free: %.2f %%\n", $4/$2 * 100.0) }'
# Show a specific process tree
top -c -p $(pgrep -d',' -f systemd)
Swap
sudo swapon -s # show current swap
sudo swapoff -a # turn all swap off
# Create a 4 GB swap file
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile # root-only, required
sudo mkswap /swapfile
sudo swapon /swapfile
# Make it permanent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# Swappiness (lower favours RAM over swap)
sysctl vm.swappiness
sudo sysctl vm.swappiness=25
# Persist in /etc/sysctl.conf, then:
sudo sysctl -p
To resize an existing swap file, disable and remove it first, then recreate:
sudo swapoff /swapfile
sudo rm /swapfile
# 16 GB via dd: bs * count = 1M * 16384
sudo dd if=/dev/zero of=/swapfile bs=1M count=16384
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
0600 permission on the swap file is mandatory — mkswap/swapon refuse a
world-readable one.