Can I delete /lib/modules directory in Linux safely?

centos
kernel

#1

I see that my /lib/modules taking so much space on CentOS 7 Linux box. It is like 300MB:

du -ch /lib/modules

Can i delete /lib/modules directory in Linux safely and what is the purpose of it?


#2

No. Do not delete /lib/modules/ directory. It contains Linux kernel module i.e. Linux device drivers. Your system might not work as expected. What you can do is delete older unused Linux kernels.

1. How to find current Linux kernel version on a CentOS/RHEL/Fedora Linux

Execute

uname -mrs

Sample outputs:

Linux 4.20.6-200.fc29.x86_64 x86_64

2. How to get currently installed Linux versions

Run rpm command or yum command

rpm -qa  | grep kernel-

OR

yum list installed 'kernel-*'

3. Delete unwanted Linux kernel to free up some disk space.

I am using the Linux kernel version 4.20.6-200. I am going to delete older kernel versions such as 4.20.5-200 and 4.20.4-200. Use yum command again to delete:

sudo yum erase kernel-4.20.5-200.fc29.x86_64 kernel-4.20.4-200.fc29.x86_64


In short you can delete all installed and older Linux kernel versions except currently running one. By default I think both Debian/Ubuntu/RHEL/CentOS/Fedora keep only last three kernels.