Boot into rescue mode with disabled root account

If you need to get a root shell on a NixOS[1] machine and the root account is disabled/locked (and/or you’ve forgotten all your passwords), here’s how to do it (without booting a live ISO).

When the machine boots, interrupt the bootloader and add this to the bootloader command-line:

[...] rescue systemd.setenv=SYSTEMD_SULOGIN_FORCE=1

(Without SYSTEMD_SULOGIN_FORCE=1 you will not be able to get a shell if the root account is disabled/locked.)

Ref. sulogin-shell: Use force if SYSTEMD_SULOGIN_FORCE set · systemd/systemd@33eb44f · GitHub

[1]: Or any GNU/Linux distro really.


Thanks! I found this helpful when nixos-infect broke the network configuration of a host.

IME, this does not work when the root password is set; the code you linked also says:

* `$SYSTEMD_SULOGIN_FORCE=1` — This skips asking for the root password if the
  root password is not available (such as when the root account is locked).