Skip to content

Instantly share code, notes, and snippets.

@egorFiNE
Last active July 14, 2024 08:23
Show Gist options
  • Save egorFiNE/30ee7910ca4b7b9b706d385e432764e0 to your computer and use it in GitHub Desktop.
Save egorFiNE/30ee7910ca4b7b9b706d385e432764e0 to your computer and use it in GitHub Desktop.
Script to remove as much systemd & other instanity from Ubuntu Server (only!) as practically possible
# first cleanup
apt -y purge --auto-remove systemd-timesyncd python3-systemd lxd-installer snapd systemd-resolved multipath-tools polkitd libpolkit-gobject-1-0 udisks2 open-iscsi systemd-hwe-hwdb update-notifier-common ubuntu-release-upgrader-core landscape-common unattended-upgrades apport uuid-runtime apparmor dbus dbus-daemon dbus-session-bus-common dbus-user-session dbus-system-bus-common
# we need network-synced time
apt -y install chrony
# at this point there should be no packages in "uninstalled not purged" state, but let's keep the command line here for refs
# dpkg --purge `dpkg -l | grep ^rc | awk '{print $2}'`
# cleanup after cleaning up
rm -rf /lib/udev/hwdb.d /var/lib/update-notifier /var/lib/ubuntu-release-upgrader /var/log/unattended-upgrades /var/lib/update-manager /etc/apparmor.d/ /var/run/dbus /var/lib/dbus /etc/xml /etc/sgml /usr/lib/systemd/system-shutdown /etc/cloud
# bring back sane resolv.conf
# it still going to be replaced by ifupdown later on, but we want to have resolver functional for the rest of the script
rm -f /etc/resolv.conf
echo 'nameserver 1.1.1.1' > /etc/resolv.conf
# bring back ssh daemon and get rid of socket activation
systemctl disable --now ssh.socket
systemctl mask ssh.socket
systemctl enable --now ssh.service
# now disabling the systemd masterpieces
SHIT="apt-daily.service apt-daily-upgrade.service systemd-rfkill.socket systemd-fsckd.socket systemd-journald-audit.socket systemd-initctl.socket dm-event.socket systemd-journald.service systemd-journald-dev-log.socket systemd-journald.socket systemd-journal-flush.service systemd-logind.service"
systemctl stop $SHIT
systemctl disable $SHIT
# it's not enough to disable and stop, they WILL reenable themselves at one point; here's why "masking" was invented
systemctl mask $SHIT
# won't help anyway, you'll need to run this script again after each major apt update
# remove binary logs
rm -rf /var/log/journal
# Not sure if we need these:
systemctl kill --kill-who=all apt-daily.service
systemctl kill --kill-who=all apt-daily-upgrade.service
# get rid of systemd timers, all of them
for i in `systemctl list-unit-files --type=timer --all --plain --no-legend | awk '{print $1}'`
do
systemctl disable $i
systemctl mask $i
done
# get rid of "useful" cronjobs, as in: updating motd, updating apt, rebuilding man pages, etc
rm -v /etc/cron.*/*
# bring back basic cron stuff that is actually needed
echo '/usr/sbin/logrotate /etc/logrotate.conf' > /etc/cron.daily/logrotate
echo 'fstrim -v -a' > /etc/cron.weekly/fstrim
chmod +x /etc/cron.*/*
# remove systemd and capabilities from pam chain
cd /etc/pam.d
cat common-session | grep -v systemd > tmp && mv tmp common-session
cat common-auth | grep -v pam_cap.so > tmp && mv tmp common-auth
cd -
# unmerging filesystems is not possible anymore either, we'll have to eat this up
rm -rf /*is-merged*
echo -n "What follows next is the removal of netplan and reinstall of ifupdown. ^C here if you are not sure"
read
# remove netplan and install ifupdown
apt -y purge --auto-remove netplan.io
apt -y install ifupdown
# write down basic interfaces
cat > /etc/network/interfaces <<EOF
auto lo
iface lo inet loopback
#auto enp0s1
#iface enp0s1 inet dhcp
EOF
# TODO: perhaps, awk the first network interface and specify it as dhcp one in `/etc/interfaces`?
# ifup enp0s1
# cleanup after cleanup
rm -rf /usr/share/netplan/netplan_cli/cli/commands /usr/lib/python3/dist-packages/netplan /etc/netplan
SHIT="systemd-networkd.service systemd-networkd.socket"
# can't be disabled, so we must mask and it won't come up after reboot
systemctl mask $SHIT
echo "Please reboot"
@egorFiNE
Copy link
Author

I'd love some comments on this. What am I missing? What else is there to cleanup?

@bsaunder2002
Copy link

PLEASE keep up the fight!
This did not work for me on first try. So tired of this fight. VM deploys. No networking. Console mode only. What? There is no /etc/.../.../..../somethingnetwork file? No example file with stuff comment out? I have KNOW systemd to use it? Really.
Debian's own use something else for init says, this is NOT a joke...
"The easiest time to choose an init other than systemd is at installation time (since systemd's packages will refuse to be removed if systemd is running). "
What? My system is so mucked up you cant tell it to get rid of stuff that is broken?
Seriously, 30 years of linux, 20 years in production on clusters and stuff...
Why am I frustrate?
I DONT know sysvinit...not at all. Rough idea about /etc, rough idea about rcX.d, rough idea about counting and 1 being lower than K05. Oh and I do KNOW bash, cat, grep, vi.
So after running your script...it didnt work. Dont worry...I can figure it out.
How?
bash -x /etc/rc0.d/K01networking start
that told me...lots of systemd smeared all over the place...not just /etc but /lib (for shell scripts? do you know what a lib is?)...all over the place.
I will kill this dragon. Feed back here and into the debian wiki.
We need to pull the needle out of the arm. This is too complex...it does not consider the Unix philosophy of do one thing, simple, well.

Thank you for trying...wonder if this comment (which you can tell has some emotion) will be posted...coincidence that the site is owned by the employer of the guy who designed systemd?
I need my tin foil hat.
SystemDead...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment