[ASUS ZenFone 8] bnsmb:
Safe mode is a feature in Android phones where all third-party apps on the device become inaccessible when enabled.

Doing a Google search on Android safe mode, I found a lot of information that is outdated and no longer applies to the current Android version.

So this small HowTo contains some infos about using the Android safe mode in Android 13 ; the infos should also be valid for Android 12 but I did not test that yet.

How to check if the phone is booted into safe mode in the Android GU

In safe mode Android prints the string "Safe mode" in the lower left corner

How to check if the phone is booted into safe mode in a shell as root user

The root user on the phone can check the value of the property persist.sys.safemode; this property is set to 1 in safe mode and does not exist if not in safe mode

in safe mode:

ASUS_I006D:/ # id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
ASUS_I006D:/ #

ASUS_I006D:/ # getprop persist.sys.safemode
1
ASUS_I006D:/ #

in normal mode:

ASUS_I006D:/ # id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
ASUS_I006D:/ #
ASUS_I006D:/ # getprop persist.sys.safemode
ASUS_I006D:/ #

How to check if the phone is booted into safe mode in a shell as non-root user

Non-root user can not read the property persist.sys.safemode. Non-root user can use the output of the command dumpsys to check if the phone is booted into safe mode using this command:

dumpsys display | grep mSafeMode

e.g.:

in safe mode:

ASUS_I006D:/ $ id
uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),1078(ext_data_rw),1079(ext_obb_rw),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats),3009(readproc),3011(uhid),3012(readtracefs) context=u:r:shell:s0
ASUS_I006D:/ $
ASUS_I006D:/ $ dumpsys display | grep mSafeMode
mSafeMode=true
ASUS_I006D:/ $

in normal mode;

ASUS_I006D:/ $ id
uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),1078(ext_data_rw),1079(ext_obb_rw),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats),3009(readproc),3011(uhid),3012(readtracefs) context=u:r:shell:s0
ASUS_I006D:/ $
ASUS_I006D:/ $ dumpsys display | grep mSafeMode
mSafeMode=false
ASUS_I006D:/ $

How to enable the safe mode for the next reboot in the Android GUI

To enable safe mode via the Android GUI do :

Presse the power button until the reboot dialog with Power Off, Restart, etc appears. Now long tap on the PowerOff button in that dialog until the dialog to reboot into safe mode appears and press ok in that dialog. The phone will then be booted into safe mode.

How to enable the safe mode for the next reboot via shell command

To enable safe mode for the next reboot of the Android OS when in the running Android OS open a shell (either local or via adb), become root user and, issue

setprop persist.sys.safemode 1

reboot

Note that Android will automatically clear this property after booting into the normal mode again

How to enable the safe mode for the next reboot when booted into the recovery

The current value for the property persist.sys.safemode is stored in the file

/data/property/persistent_properties

e.g:

ASUS_I006D:/ # grep safe /data/property/persistent_properties
persist.sys.safemode
ASUS_I006D:/ #

Unfortunately this is a binary file with an unknown format:

ASUS_I006D:/ # file /data/property/persistent_properties
/data/property/persistent_properties: data
ASUS_I006D:/ #

So it's not clear how to enable safe mode for the next reboot while booted in to recovery.

A very ugly workaround to enable safe mode for the next reboot while the phone is booted from the recovery can be implemented using a Magisk init script (if Magisk is installed on the phohe, of course - see How to run a script at every boot using Magisk for details):

ASUS_I006D:/ # cat /data/adb/post-fs-data.d/enable_safe_mode
/data/adb/magisk/magisk64 resetprop -n persist.sys.safemode 1
#
# make sure booting into safe mode is only a onetime job!
#
rm /data/adb/post-fs-data.d/enable_safe_mode
ASUS_I006D:/ #

Notes:

Using this method the propery persist.sys.safemode is not set after booting into safe mode and the safe mode can only be determind by the output of the dumpsys command, e.g.:

ASUS_I006D:/ # id
uid=0(root) gid=0(root) groups=0(root) context=u:r:magisk:s0
ASUS_I006D:/ #
ASUS_I006D:/ # getprop persist.sys.safemode

ASUS_I006D:/ #
ASUS_I006D:/ # dumpsys display|grep mSafeMode
mSafeMode=true
ASUS_I006D:/ #

Be aware that without deleting the Magisk init script for the workaround the phone will always boot into safe mode.

How to exit the safe mode

To exit the safe mode Just reboot the phone

How to disable safe mode

Disabling safe mode is only possible using an Android MDM (= Mobile Device Manager). For further details see this thread :

https://xdaforums.com/t/blocking-safe-mode-and-factory-reset-on-android.4603707/

and these apps:

Sentry

https://github.com/x13a/Sentry?tab=readme-ov-file

The Sentry app is available via F-Droid; I successfully tested disabling safe mode using this app on an ASUS Zenfone 8 running OmniROM 14 (= Android 14)

Test DPC from Google to test Android Device Management (not tested)

https://github.com/googlesamples/android-testdpc

Spoiler content hidden.