Deploying a Linux server is really easy these days thanks to the abundance of cloud server providers like Linode, Digital Ocean etc. This enables not-so-expert people, like me, to deploy and manage their own Linux servers.
As you start to run your own server, you might feel the need to clean up your system from time to time to free up the valuable disk space.
In this tutorial, I’ll share some easy to use actionable tips to free up a few GBs of disk on your Linux server.
4 simple ways to clean Linux server
Some of these tips are more for Ubuntu servers because that’s what I use. Some tips are general and can be used in any Linux distribution. Please don’t blindly run the commands. First learn what the commands do.
1. Remove old kernel headers and unnecessary packages
As you keep updating your system, it will collect unnecessary packages and dependencies that are not needed anymore.
Similarly, your system will get kernel updates and then the older kernels won’t be of much use.
Ubuntu and Debian has a handy tool that takes care of such jobs in a single command. Just run this command:
$ sudo apt autoremove
You’ll be pleasantly surprised to see that it readily frees up a few 100s of MBs.
2. Clear old systemd journal logs
You won’t realize but the logs can take up a considerable amount of distance. Most Linux distributions use systemd these days and systemd stores the logs in /var/log/journal.
You can use the du command to check the size of the /var/log/journal directory:
$ du -hs /var/log/journal/
Now, don’t go on and delete the log directory. There is better ways to handle these logs. For example, you can clear all the logs older than ten days in this manner:
$ journalctl --vacuum-time=10d
You may read about more ways to clear systemd journal logs.
3. Use Docker? Remove unused images and containers
If you use Docker containers, you may want to check on the unused containers and images. Usually, the images and containers are stored in /var/lib/docker directory but you can verify that with the command docker info.
If you check the size of the directory and you think it’s way too big, you should check the images and containers. If you see unused ones, you may remove those docker images and containers.
4. Check the biggest directories in your system and see if they are supposed to be that big
This is a tedious task but it does help in finding unusually large directories.
Start from the root directory and list the top 10 biggest directories.
$ du -h . --max-depth=1 | sort -n -r | head -n 10
Then go into one of the biggest directories and repeat the above command.
The idea is to check whether the biggest directories should be that big? For example, in some cases, the Docker files like
/var/lib/docker/aufs could be really big.
When you find something like that, you question whether it is normal for the directories to take that much of space. Based on the answer, you can decide whether or not to clean up.
I hope you find these tips helpful in cleaning up your Linux server. If you have something valuable to add, please let me know!