How can /proc/sys values be changed at boot?

sysctl.conf doesn't exist on Android and echoing values to the proc files isn't persistent.


Use init's write command in /init.rc or any other .rc file. You may also create your own:

# /system/etc/init/custom.rc

on property:sys.boot_completed=1
    write /proc/sys/kernel/hostname MyDroid

Set permissions and reboot device.

~# chown 0.0 /system/etc/init/custom.rc
~# chmod 0644 /system/etc/init/custom.rc
~# chcon u:object_r:system_file:s0 /system/etc/init/custom.rc

You may also use some early trigger like on init or on boot but your value may get overwritten by some later trigger.

Also some values aren't allowed by SELinux to be written by init. E.g. /proc/sys/vm/drop_caches and /proc/sys/vm/dirty_ratio are writable only by vold. In this case you can use exec command which allows changing SELinux label before command execution.

# /data/sysctl.conf

# /system/etc/init/custom.rc

on property:sys.boot_completed=1
    exec u:r:magisk:s0 -- /system/bin/sysctl -p /data/sysctl.conf

* sysctl is toybox applet. Or you may use busybox.

For other options see this answer. Some apps may also provide similar functionality but I never used one.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.