Disk đầy là vấn đề rất phổ biến khi dùng VPS lâu dài. Khi disk đầy, web server crash, database lỗi, không thể ghi log. Bài này hướng dẫn tìm nguyên nhân và xử lý nhanh.
Bước 1: Kiểm tra tình trạng disk
# Xem dung lượng tổng thể
df -h
# Xem inode (nếu inode đầy dù dung lượng còn)
df -i
# Xem thư mục nào chiếm nhiều nhất
du -sh /* 2>/dev/null | sort -rh | head -20
# Xem chi tiết trong /var
du -sh /var/* 2>/dev/null | sort -rh | head -20
Bước 2: Tìm file lớn
# Tìm 20 file lớn nhất toàn hệ thống
find / -type f -size +100M 2>/dev/null | head -20
# Tìm file lớn trong /var/log
find /var/log -type f -size +50M 2>/dev/null
# Tìm file lớn trong home
find /home /root -type f -size +100M 2>/dev/null
Bước 3: Dọn log hệ thống
# Xem log journal đang chiếm bao nhiêu
journalctl --disk-usage
# Xoá log journal cũ hơn 3 ngày
journalctl --vacuum-time=3d
# Giữ tối đa 200MB
journalctl --vacuum-size=200M
# Xoá log nginx/apache
truncate -s 0 /var/log/nginx/access.log
truncate -s 0 /var/log/nginx/error.log
# Dọn log rotation ngay
logrotate -f /etc/logrotate.conf
Bước 4: Dọn apt cache và gói không dùng
# Xem cache apt đang chiếm bao nhiêu
du -sh /var/cache/apt/
# Xoá toàn bộ cache apt
apt clean
# Xoá gói không còn cần thiết
apt autoremove -y
# Xoá kernel cũ
apt autoremove --purge -y
Bước 5: Dọn các file khác
# Xoá temp files
rm -rf /tmp/*
rm -rf /var/tmp/*
# Docker images/containers (nếu dùng Docker)
docker system df
docker system prune -a
# Nén log cũ
gzip /var/log/*.log.1 2>/dev/null
Bước 6: Mở rộng LVM (nếu dùng LVM)
Nếu VPS dùng LVM và bạn đã nâng cấp disk trên panel H2Cloud:
# Xem partition hiện tại
lsblk
fdisk -l
# Xem LVM info
pvs
vgs
lvs
# Resize physical volume
pvresize /dev/vda3
# Mở rộng logical volume (thêm 100% free space)
lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
# Resize filesystem (ext4)
resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
# Kiểm tra kết quả
df -h
Cấu hình logrotate để tránh đầy disk
nano /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
nginx -s reopen
endscript
}
Giám sát disk tự động
# Thêm cron job cảnh báo khi disk dùng trên 80%
crontab -e
# Thêm dòng:
0 9 * * * df -h / | awk 'NR==2{if(int($5)>80) print "CANH BAO: Disk " $5}' Có thể bạn cần xem thêm
Bài viết đã được kiểm duyệt bởi VPS HC Team
Cập nhật lần cuối: 16/06/2026