zfsjlll

zfsjlll

趁着年轻,好好犯病

Clean up VPS storage space

back

Problem#

Recently, I found that the reserved 10G virtual disk on the virtual machine was unexpectedly full when storing files, so I need to clean up the space.

Steps#

  1. Check disk usage
sudo df -h

image

It can be seen that /dev/sda1 is already using 7.4G of space, which is the capacity after cleaning.

/dev/sda1 is a block device file in Linux, usually used to represent a hard disk partition. In Linux, hard disks are usually represented in the form of /dev/sd*, where * represents a lowercase letter, such as /dev/sda, /dev/sdb, etc.

When a hard disk is divided into multiple partitions, each partition is assigned a device file to represent the partition. Among them, /dev/sda1 represents the first partition, /dev/sda2 represents the second partition, and so on.

  1. Check large files in the disk
sudo du -ah . | sort -rh | head -n 10

The du command can be used to view the disk usage of a specified directory or file, including file size and directory size, etc. The -h option is used to display file sizes in a human-readable format. For example, the following command will search and list the sizes of all files and directories in the current directory, sorted by size:

In this command, -a means to list the sizes of all files and directories, -h means to display file sizes in a human-readable format, . represents the current directory, which can be changed to the directory you want to query, sort -rh means to sort in reverse order by size, and head -n 10 means to only display the top 10 results.

image

  1. Clean up large files using different commands

It was found that /usr occupies a lot of space, but after cleaning, the capacity has hardly changed, so it can be skipped. At the same time, the directory contains a large number of user-installed programs, library files, and other resources, so cleaning up this directory requires careful operation to avoid accidentally deleting important files and causing system problems. The following are the instructions for using the commands.

  • Delete unnecessary language files:
sudo apt autoclean
  • Delete all no longer needed packages:
sudo apt autoremove --purge
  • Delete unnecessary dependencies and configuration files:
sudo apt clean

Next, clean up /var

var is short for variable. This directory contains things that are constantly expanding. We usually put directories that are frequently modified under this directory. This includes various log files.

The . /lib/docker/overlay2 directory is an important directory in the Docker storage driver, used to store the file system layers of Docker images and containers. When using Docker, the size of this directory may gradually increase, occupying a large amount of disk space. Delete unnecessary Docker images and containers, or use Docker's cleanup commands to clean up unused data. Here are some commonly used Docker cleanup command examples:

  • List all stopped containers:
sudo docker ps -a -q -f status=exited
  • Delete all stopped containers:
sudo docker rm $(sudo docker ps -a -q -f status=exited)
  • List all unused images:
sudo docker images -q -f dangling=true
  • Delete all unused images:
sudo docker rmi $(sudo docker images -q -f dangling=true)
  • Clean up all unused data:
sudo docker system prune -a --volumes

Please note that when cleaning Docker data, it is best to back up the Docker images and containers that need to be retained in case important data is accidentally deleted.

Use the logrotate tool to automatically rotate and delete old log files. The logrotate tool can be configured to rotate and delete log files based on time, size, or other conditions to keep the size and number of log files under control.

  • To delete a large file, you can use the following command:
sudo truncate -s 0 /var/log/your_large_file.log
  • This command will truncate the size of the /var/log/your_large_file.log file to zero, effectively emptying the file. Then run the logrotate command to rotate and archive old log files:
sudo logrotate -f /etc/logrotate.conf

The -f parameter forces logrotate to perform the rotation operation immediately, even if it is not necessary to rotate the file at the moment. /etc/logrotate.conf is the default configuration file path for logrotate, and you can use a different configuration file path as needed.

Please note that when using the truncate command, it is best to back up the file that will be emptied in case you need to recover the file content.

Use the journalctl tool to clean up old log messages and data. The journalctl tool can filter log messages by time, priority, source, keywords, etc., as well as limit the number and time range of log messages. Here are some commonly used journalctl command examples:

  • Display the latest 100 system log messages:
sudo journalctl -n 100
  • Display the log messages of a specific service:
sudo journalctl -u your_service_name
  • Display the log messages within a specific time range:
sudo journalctl --since "2022-01-01 00:00:00" --until "2022-02-01 00:00:00"
  • Display the log messages of a specific process:
sudo journalctl _PID=your_process_id

To clean up old log messages and data, you can use the following command:

sudo journalctl --vacuum-size=100M

This command will clean up all unnecessary log messages and data, and limit the size of log files to 100M. You can adjust the value of the --vacuum-size parameter as needed. Please note that this command only cleans up system logs.

Summary#

After performing the above operations, you can free up some space for your VPS or virtual machine.#

References#

Linux System Directory Structure

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.