Categories
Guides NAT VPS

Reduce Debian 10 to Minimal Environment

The following post will quickly guide you to reduce Debian/Ubuntu installations size by removing, in a safety way, non-essential packages. This may useful in VPS instances where limited disk space is available, around 2GB or less. I’ll be using a NAT VPS instance based on OpenVZ with 4GB of disk space under Debian 10 but it will work on KVM based instances as well.

Step 1: Updating the OS

As you probably know, the first step after installing any OS is to updating all its packages and dependencies, you can do that by executing:

apt update && apt upgrade -y && apt dist-upgrade -y && apt autoremove -y

Note: I’ve logged in as root, if you don’t have access to the root account, you must use “sudo” before every command, i.e: sudo apt update && sudo apt upgrade -y &&…

Step 2: Identifying Essential Packages

You can identify required, important, standard, optional and extra packages by executing:

dpkg-query -Wf '${Package;-40}${Priority}\n' | sort -b -k2,2 -k1,1

All required and important packages should not be removed, but this can be confusing because some packages like openssh-server are required to access to the server via SSH. After several testings, I’ve concluded that to set up a minimal environment under Debian 10, these packages are essential:

base-files base-passwd bash bsdutils coreutils dash debianutils diffutils dpkg findutils grep gzip hostname ifupdown init-system-helpers libc-bin login ncurses-base ncurses-bin perl-base sed sysvinit-utils openssh-server tar util-linux wget zip sudo wide-dhcpv6-client htop initramfs-tools iptables locales net-tools resolvconf sudo systemd-sysv

You may need to install additional packages like: python, in case you need to work with it but since I don’t, I won’t install it.

Step 3: Installing Debfoster

This is a useful tool available in the Debian/Ubuntu native repositories, according to its description there: “debfoster is a wrapper program for apt and dpkg. When first run, it will ask you which of the installed packages you want to keep installed.

You can install Debfoster by executing:

apt install debfoster -y

Now you need to make a list of essential packages (see Step 2) and Debfoster will remove every package and its dependencies that is not included on the list. You can generate the list by executing:

OpenVZ:

echo -e 'base-files\nbase-passwd\nbash\nbsdutils\ncoreutils\ndash\ndebianutils\ndiffutils\ndpkg\nfindutils\ngrep\ngzip\nhostname\nifupdown\ninit-system-helpers\nlibc-bin\nlogin\nncurses-base\nncurses-bin\nperl-base\nsed\nsysvinit-utils\nopenssh-server\ntar\nutil-linux\nwget\nzip\nsudo\nwide-dhcpv6-client\nhtop\ninitramfs-tools\niptables\nlocales\nnet-tools\nopenssh-server\nresolvconf\nsudo\nsystemd-sysv' > /root/essentials

KVM:

echo -e 'base-files\nbase-passwd\nbash\nbsdutils\ncoreutils\ndash\ndebianutils\ndiffutils\ndpkg\nfindutils\ngrep\ngzip\nhostname\nifupdown\ninit-system-helpers\nlibc-bin\nlogin\nncurses-base\nncurses-bin\nperl-base\nsed\nsysvinit-utils\nopenssh-server\ntar\nutil-linux\nwget\nzip\nsudo\nwide-dhcpv6-client\nhtop\ninitramfs-tools\niptables\nlocales\nnet-tools\nopenssh-server\nresolvconf\nsudo\nsystemd-sysv\nlinux-image-amd64' > /root/essentials

Now Debfoster will remove everything that is not included on that list, do that by executing:

debfoster -fk /root/essentials

You will see the list of packages that Debfoster will remove, press Y.

Note: on KVM based instances, don’t remove Kernel and Grub related data, you will be ask about during Debfoster removing process. Also, if you get the “Failed to fetch” error during apt update/install you may need to recreate the resolv.conf file, do that by executing:

echo -e "nameserver 8.8.8.8\nnameserver 8.8.4.4" > /etc/resolv.conf; systemctl restart systemd-resolved; cat /etc/resolv.conf

Step 4: Removing OS Cache Files

As a final step, you can remove all cache and log directories and unnecessary locale files by executing:

rm -rf /var/cache/* /var/lib/apt/lists/* /usr/share/locale/* /usr/share/man/* /usr/share/X11/locale/* /root/.cache/* /var/log/* /usr/share/man/?? /usr/share/man/??_*

Optionally, you can save around 10 MB on disk space by compress the initramfs, do that by executing:

echo 'COMPRESS=xz' > /etc/initramfs-tools/conf.d/compress

Reboot you system and check the disk space usage by executing:

df- h

Results:

Conclusion:

There you have it, I’ve reduced a 805 MB Debian 10 installation to 278 MB of disk space. I hope this post guide you to create minimal environments under Debian/Ubuntu installations. Check WebHorizon amazing deals here.

Sources:

https://askubuntu.com/questions/79665/keep-only-essential-packages
https://ostechnix.com/debfoster-keep-only-essential-packages-in-debian-and-ubuntu/
https://wiki.debian.org/ReduceDebian
https://talk.lowendspirit.com/index.php?p=/discussion/3527/looking-someone-to-make-minimal-openvz-templates-for-us
https://unix.stackexchange.com/questions/23156/can-i-safely-remove-var-cache/226681