【Linux】清除磁盘空间

Posted by 西维蜀黍 on 2021-10-09, Last Modified on 2022-02-19

Problem

But for some reason I kept getting 28: No space left on device when I run apt-get update. I tried to solve this by trying apt-get clean, which seemed to work for a little bit but then still had the same issue.

No space left on device” is a prompt that is generated when your disk space is completely filled. There is a chance of encountering this issue, even when the disk space isn’t really full. This might happen when your server’s disk space runs out of Inodes.

查看占用

查看当前物理disk占用情况

$ lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0                       7:0    0 55.4M  1 loop /snap/core18/1944
loop1                       7:1    0 55.4M  1 loop /snap/core18/2128
loop2                       7:2    0 61.8M  1 loop /snap/core20/1081
loop3                       7:3    0 67.3M  1 loop /snap/lxd/21545
loop4                       7:4    0 70.3M  1 loop /snap/lxd/21029
loop5                       7:5    0 61.9M  1 loop /snap/core20/1169
loop6                       7:6    0 32.4M  1 loop /snap/snapd/13270
loop7                       7:7    0 32.3M  1 loop /snap/snapd/13170
sda                         8:0    0  200G  0 disk
|-sda1                      8:1    0  512M  0 part /boot/efi
|-sda2                      8:2    0    1G  0 part /boot
`-sda3                      8:3    0 48.5G  0 part
  `-ubuntu--vg-ubuntu--lv 253:0    0 34.3G  0 lvm  /

查看当前文件系统占用情况

$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
udev                               1.9G     0  1.9G   0% /dev
tmpfs                              394M  996K  393M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   34G   24G  8.2G  75% /
tmpfs                              2.0G     0  2.0G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda2                          976M  300M  609M  34% /boot
/dev/loop0                          56M   56M     0 100% /snap/core18/1944
/dev/sda1                          511M  5.3M  506M   2% /boot/efi
/dev/loop1                          56M   56M     0 100% /snap/core18/2128
/dev/loop3                          68M   68M     0 100% /snap/lxd/21545
/dev/loop2                          62M   62M     0 100% /snap/core20/1081
/dev/loop4                          71M   71M     0 100% /snap/lxd/21029
/dev/loop5                          62M   62M     0 100% /snap/core20/1169
/dev/loop6                          33M   33M     0 100% /snap/snapd/13270
/dev/loop7                          33M   33M     0 100% /snap/snapd/13170
tmpfs                              394M  176K  394M   1% /run/user/1000

查看LVM卷组的信息

$ sudo vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <48.50 GiB
  PE Size               4.00 MiB
  Total PE              12415
  Alloc PE / Size       8768 / 34.25 GiB
  Free  PE / Size       3647 / <14.25 GiB
  VG UUID               OWAYr3-DITL-hFT3-eyHu-Yf2t-nKsP-62GTHG

清理APT缓存

View Cache Space

$ sudo du -sh /var/cache/apt 

一旦决定清理apt缓存,清理命令如下:

$ sudo apt-get clean

apt clean 命令将删除保留在apt缓存中的所有软件包。 当然如果你本地网络并不快速,你也可以不用清理,避免安装软件时下载大量软件包耗费时间。

Get rid of packages that are no longer required [Recommended]

If you read the apt-get commands guide, you might have come across the apt-get command option ‘autoremove’.

This option removes libs and packages that were installed automatically to satisfy the dependencies of an installed package. If that package is removed, these automatically installed packages are useless in the system.

It also removes old Linux kernels that were installed from automatically in the system upgrade.

It’s a no-brainer command that you can run from time to time to make some free space on your Ubuntu system:

sudo apt-get autoremove

Clear systemd journal logs

The problem is that over the time, these logs take a considerable amount of disk space. You can check the log size with this command:

journalctl --disk-usage

Use Bleachbit

$ sudo apt install bleachbit

$ sudo bleachbit

Reference