How to run shell scripts at boot time as root on LineageOS 18.1 without Magisk

Enable developer settings.

Enable USB debugging (adb) and rooted debugging.
Warning: rooted debugging is a very dangerous feature, don't forget to turn it off when you've done!

Attach your Android phone to a computer via USB cable, verify that device is detected by running adb devices.

Run adb root.

Run adb remount /. This will remount root filesystem in rw mode.

Run adb shell to get the root shell on your phone.

On the phone, go to /system/etc/init and create a file named myboot.rc with following content:

on boot
    exec u:r:su:s0 root root -- /system/etc/myboot.sh

Then create /system/etc/myboot.sh, write your script and make it executable.

Note: the first line of the script must be #!/bin/sh.

Note: If you want to use iptables in this script (or some other external binary), use full path (/system/bin/iptables) instead of just iptables. The /system/bin directory seems to be missing from the $PATH when the script is launched.

Then open the /system/addon.d/50-lineage.sh file, find the list_files function and add paths to files you've just created (relative to /system/) to the list. It is to backup and restore listed files on upgrade. It should look like this:

list_files() {
cat <<EOF
etc/hosts
etc/myboot.sh
etc/init/myboot.rc
EOF
}

This is all, now exit root shell, disable rooted debugging, reboot your phone and see if it worked.

If you have any comments, contact me by email.