Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux headers are consuming a lot of disk space on the EC2 machine. Is it safe to manually delete the headers with rm command?

EC2 machine on aws is filled with multiple linux headers and currently / is 100% full on disk space. I ran commands like apt-get autoremove but it is throwing an error saying

No apport report written because the error message indicates a disk full error
dpkg-deb: error: subprocess paste was killed by signal (Broken pipe)

Can I remove the old headers by the rm command? Is it safe? and if I remove with rm command, should I do any clean up after that? Here is the list of headers:

du -h -d1 /usr/src/ | sort -hr
2.8G    /usr/src/
106M    /usr/src/linux-aws-headers-4.4.0-1099
106M    /usr/src/linux-aws-headers-4.4.0-1098
106M    /usr/src/linux-aws-headers-4.4.0-1096
106M    /usr/src/linux-aws-headers-4.4.0-1095
106M    /usr/src/linux-aws-headers-4.4.0-1094
106M    /usr/src/linux-aws-headers-4.4.0-1092
106M    /usr/src/linux-aws-headers-4.4.0-1090
106M    /usr/src/linux-aws-headers-4.4.0-1088
106M    /usr/src/linux-aws-headers-4.4.0-1087
106M    /usr/src/linux-aws-headers-4.4.0-1085
106M    /usr/src/linux-aws-headers-4.4.0-1084
106M    /usr/src/linux-aws-headers-4.4.0-1083
106M    /usr/src/linux-aws-headers-4.4.0-1079
106M    /usr/src/linux-aws-headers-4.4.0-1077
106M    /usr/src/linux-aws-headers-4.4.0-1075
106M    /usr/src/linux-aws-headers-4.4.0-1074
106M    /usr/src/linux-aws-headers-4.4.0-1072
106M    /usr/src/linux-aws-headers-4.4.0-1070
106M    /usr/src/linux-aws-headers-4.4.0-1069
106M    /usr/src/linux-aws-headers-4.4.0-1066
106M    /usr/src/linux-aws-headers-4.4.0-1065
106M    /usr/src/linux-aws-headers-4.4.0-1062
106M    /usr/src/linux-aws-headers-4.4.0-1061
106M    /usr/src/linux-aws-headers-4.4.0-1060
13M /usr/src/linux-headers-4.4.0-1100-aws
13M /usr/src/linux-headers-4.4.0-1099-aws
13M /usr/src/linux-headers-4.4.0-1098-aws
13M /usr/src/linux-headers-4.4.0-1096-aws
13M /usr/src/linux-headers-4.4.0-1095-aws
13M /usr/src/linux-headers-4.4.0-1094-aws
13M /usr/src/linux-headers-4.4.0-1092-aws
13M /usr/src/linux-headers-4.4.0-1090-aws
13M /usr/src/linux-headers-4.4.0-1088-aws
13M /usr/src/linux-headers-4.4.0-1087-aws
13M /usr/src/linux-headers-4.4.0-1085-aws
13M /usr/src/linux-headers-4.4.0-1084-aws
13M /usr/src/linux-headers-4.4.0-1083-aws
13M /usr/src/linux-headers-4.4.0-1079-aws
13M /usr/src/linux-headers-4.4.0-1077-aws
13M /usr/src/linux-headers-4.4.0-1075-aws
13M /usr/src/linux-headers-4.4.0-1074-aws
13M /usr/src/linux-headers-4.4.0-1072-aws
13M /usr/src/linux-headers-4.4.0-1070-aws
13M /usr/src/linux-headers-4.4.0-1069-aws
13M /usr/src/linux-headers-4.4.0-1066-aws
13M /usr/src/linux-headers-4.4.0-1065-aws
13M /usr/src/linux-headers-4.4.0-1062-aws
13M /usr/src/linux-headers-4.4.0-1061-aws
13M /usr/src/linux-headers-4.4.0-1060-aws

df command output:

df -k
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              499316       0    499316   0% /dev
tmpfs             101444   10740     90704  11% /run
/dev/xvda1       8065444 8049060         0 100% /
tmpfs             507212       0    507212   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs             507212       0    507212   0% /sys/fs/cgroup
/dev/xvdf        5029504   10244   4740732   1% /data
tmpfs             101444       0    101444   0% /run/user/1000
like image 610
hithyshi Avatar asked Feb 16 '20 11:02

hithyshi


People also ask

Can I remove linux headers?

Here is the tutorial to learn how to uninstall linux headers with apt-get command. Step 1: Open a terminal with 'su' access and enter the command as shown below. Step 2: The command reads the package lists and proceeds with the uninstallation. Use 'apt-get autoremove' to remove them.

Where are linux headers stored?

The system's libc headers are usually installed at the default location /usr/include and the kernel headers in subdirectories under that (most notably /usr/include/linux and /usr/include/asm).

What are the linux headers?

linux-headers is a package providing the Linux kernel headers. These are part of the kernel, although they are shipped separately (further reasoning is available: [1]). The headers act as an interface between internal kernel components and also between userspace and the kernel.


4 Answers

To delete linux headers safely, always use:

$ sudo apt-get autoremove

Sometimes the above command can give error like this:

Reading package lists... Error!
E: Could not create temporary file for /var/cache... (28: No space left on device)
E: The package lists or status file could not be parsed or opened.

In this case, delete one or two old headers manually. To do this first go to /usr/src folder and list the files according to date (oldest last) using below command:

$ cd /usr/src
$ ls -lt

Then choose an old header (e.g. linux-aws-headers-4.4.0-1067) and delete it using this command:

$ sudo rm -r linux-aws-headers-4.4.0-1067

After doing this, the command sudo apt-get autoremove should work without any problem.

like image 153
martinho Avatar answered Oct 19 '22 10:10

martinho


This command worked for me:

 apt-get -f autoremove
like image 14
jpereira Avatar answered Oct 19 '22 10:10

jpereira


Context

I was getting the same error as OP. To be able to run the command suggested in jpereira's answer (apt-get -f autoremove), I first had to free about 10MB of space.

But when I ran it, something weird happened where apt-get said it was going to remove all the linux-headers (and some other) packages, but it first tried to install another package (snapd), but because there was no space to install it, the process failed.

The next time I tried running the command, it was as if apt-get had forgotten about the linux-headers (and the other) packages. So I went ahead and attempted a manual removal of one of the packages:

sudo apt-get remove linux-aws-headers-4.4.0-1065

This command, successfully removed 2 directories from the /usr/src directory. It removed the directory with the same name as the package and the package of the same name but with the -aws moved to the end.

To remove all of them I ran this command, BUT DO NOT USE IT because I ended up removing some packages that would have not been removed by the autoremove functionality and I also did not remove all the packages that would have been removed by the autoremove functionality.

cd /usr/src
while read pkg; do sudo apt-get -y remove $pkg; done < <(find . -maxdepth 1 -type d -name "linux*" -printf "%f\n")

Recommended Solution

The perfect solution would have been to run the following commands, but you first need to copy the list of modules that autoremove initially said that it was going to remove:

echo "<PASTE COPIED LIST OF PACKAGES HERE>" | cut -c3- | tr " " "\n"

Make sure that the output of that command is a list of the packages where each package is in one line. If that works, then go ahead and run something similar to the command I used before:

while read pkg; do sudo apt-get -y remove $pkg; done < <(echo "<PASTE COPIED LIST OF PACKAGES HERE>" | cut -c3- | tr " " "\n")

More context

Running the recommended solution (after running the not recommended one) was also good for me because that removed some linux-image and linux-modules packages that were also occupying a lot of space but somewhere else!

The first command (not recommended one) freed about 3.7G of space and the second one (recommended one) freed an additional 1.7G. However, if you first run the recommended command, that command alone should free all the space that needs to be freed.

like image 4
alejandro Avatar answered Oct 19 '22 12:10

alejandro


I automated removal of the unneeded directories with this shell code:

sudo apt-get autoremove
cd /usr/src
sudo rm -r $(comm -23 <(ls /usr/src) <(apt-file search /usr/src/* | awk -F / '{print $4}' | sort -u))

The last line deletes every directory that is present in ls /usr/src but is not present in a list of owned files in that directory. The command ended up deleting all but the most recent directory. Before running for real, I suggest changing rm to echo rm to see what would be removed.

like image 1
piojo Avatar answered Oct 19 '22 10:10

piojo