Skip to content
CoreMacs
Mac infrastructureGuide

launchctl bootstrap gui/501 over SSH: "Could not find domain for" and 125 on a headless Mac

The gui/UID launchd domain only exists after someone logs in at the Mac's GUI, so bootstrapping an agent there over SSH fails. Log in once (or enable automatic login) and install from a GUI Terminal, or run the service as a LaunchDaemon with UserName.

CoreMacs News Desk·Published ·Last verified ·7 min read
A Mac mini seen from the rear on a light desk, showing its power socket, Ethernet port, Thunderbolt ports, HDMI and USB ports.

The gui/<uid> launchd domain is the user-login domain, and it only exists after that user has logged in at the Mac's graphical login window. Over SSH on a Mac where nobody has logged in, there is nothing to bootstrap into, so launchctl fails with "Could not find domain for" or error 125. Either create the GUI session (log in once through Screen Sharing, install from a Terminal there, and turn on automatic login), or, for services that do not need the GUI, install the plist as a LaunchDaemon in /Library/LaunchDaemons with a UserName key and bootstrap it into the system domain.

Which errors point to this problem

Depending on the tool and the domain you target, you will see one of these:

"launchctl" failed: Could not find domain for
Could not enable service: 125: Domain does not support specified action
Service cannot load in requested session
Bootstrap failed: 134: Unknown error: 134
Load failed: 5: Input/output error

The first is listed in GitLab's macOS runner documentation as the error you get when managing the runner over SSH instead of a local GUI terminal. The second came from brew services start syncthing, which ran /bin/launchctl enable gui/503/homebrew.mxcl.syncthing on macOS 14.6.1, as reported on the Syncthing forum. The 134 pair appeared in gitlab-runner issue 29324 when the reporter tried user/501 on a headless EC2 Mac, and the last line came from the deprecated launchctl load in the same report.

Why gui/<uid> does not exist over SSH

The launchctl(1) man page defines three per-user targets:

  • login/<asid>: a user-login domain, "created when the user logs in at the GUI".
  • gui/<uid>: another form of the login specifier, addressed by user instead of audit session.
  • user/<uid>: the user domain, which "may exist independently of a logged-in user".

An SSH login does not create a GUI login, so gui/501 simply is not there. The man page does not explain the 125 code; the message itself says only "Domain does not support specified action". In the Syncthing report, the service started once the same user logged in at the GUI.

Switching to user/<uid> usually fails differently. LaunchAgents are tied to session types. TN2083 (archived, written for Mac OS X 10.5) says that if the plist sets no LimitLoadToSessionType, launchd assumes Aqua, the GUI login session. That would be consistent with the "Service cannot load in requested session" error, but Apple does not document the cause of the 134 error.

How to check which context you are in

launchctl managername
launchctl print gui/$(id -u)
launchctl print user/$(id -u)
launchctl error 125

managername prints the name of the job manager for your current context; the man page ties it to the session types used by LimitLoadToSessionType. Run it once over SSH and once in a Terminal inside a GUI login and compare. If print gui/$(id -u) fails while print user/$(id -u) works, the user has no GUI login. The man page warns that print output is not an API, so do not parse it in scripts.

Fix 1: create the GUI session the agent expects

Use this for agents that need the window server or a login session. GitLab Runner is one: its documentation says the runner runs as a user-mode LaunchAgent, that "The LaunchAgent requires a graphical login session to bootstrap", and that running it as a LaunchDaemon is not supported.

  1. Connect with Screen Sharing or another VNC client and log in as the account that will run the service. On a cloud Mac, follow the provider's instructions for setting a password and enabling Screen Sharing.
  2. Open Terminal in that GUI session and run the installer there, for example:
    gitlab-runner install
    gitlab-runner start
    For your own plist, bootstrap it from the same Terminal:
    launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/<label>.plist
  3. Turn on automatic login so the session comes back after a reboot: on macOS Ventura 13 or later, System Settings > Users & Groups > "Automatically log in as". Apple notes that manual login is required when FileVault is on, so automatic login is not available on a FileVault-encrypted Mac.

Automatic login means anyone who can restart the machine gets that user's session. Weigh that against your physical and network exposure.

Fix 2: run it as a LaunchDaemon with UserName

Use this for services that only need files and the network, such as a sync daemon. The launchd.plist(5) man page says UserName and GroupName are only honoured for services loaded into the privileged system domain, and that LimitLoadToSessionType only applies to agents, because there are no distinct sessions in the system context.

  1. Copy the agent plist to /Library/LaunchDaemons/<label>.plist and add the account to run as:
    <key>UserName</key>
    <string>ec2-user</string>
  2. Remove LimitLoadToSessionType if present. Point StandardOutPath and StandardErrorPath at directories that exist and that the user can write; the issue reporter had to change a default log path that did not exist.
  3. Fix ownership and permissions. launchctl(1) requires LaunchDaemons to be owned by root and configuration files to disallow group and world writes:
    sudo chown root:wheel /Library/LaunchDaemons/<label>.plist
    sudo chmod 644 /Library/LaunchDaemons/<label>.plist
    The issue reporter found he needed 600 for the job to start at boot; launchctl(1) itself only requires that the file not be writable by group or others, which 644 satisfies. If the daemon does not start at boot with 644, try 600.
  4. Enable and load it in the system domain:
    sudo launchctl enable system/<label>
    sudo launchctl bootstrap system /Library/LaunchDaemons/<label>.plist

What you give up with a LaunchDaemon

  • No GUI. TN2083 says a daemon is not allowed to connect to the window server and should only use services from the global namespace. Jobs that need a GUI session belong in Fix 1.
  • No privacy prompts. Syncthing's maintainer points out that a daemon cannot show permission dialogs, so grant Full Disk Access to the binary beforehand rather than expecting a prompt.
  • Keychain. In issue 29324 the login keychain was not available to the daemon; the reporter added SessionCreate and created a dedicated keychain inside the job.
  • Vendor support. GitLab documents the runner as LaunchAgent-only. The daemon setup in issue 29324 is a user workaround, and the reporter also notes that launchd sends SIGTERM while the runner expects SIGQUIT for a graceful stop.

How to confirm it worked

sudo launchctl print system/<label>
launchctl print gui/$(id -u)/<label>

Use the first for a daemon and the second, from the GUI session, for an agent. Check the state, PID and last exit status. Then reboot and check again before anyone logs in; on a Mac without FileVault, a daemon should be running, an agent only after automatic login.

If FileVault is on, the fdesetup(8) man page says the data volume stays locked after boot until an account authenticates with a password, so nothing in /Library/LaunchDaemons runs until an account unlocks the data volume with its password. On a headless Mac you can do that at the login window through a remote console, or over SSH: the man page documents remote unlocking via SSH when Remote Login is enabled, and Apple's platform security guide says this works on a Mac with Apple silicon with macOS 26 or later. Enable Remote Login before the restart, then connect with SSH and an account password; the first connection only unlocks the disk and is then dropped, so reconnect. For a planned restart, sudo fdesetup authrestart bypasses the initial unlock once; the man page warns it may not work on all systems.

How to avoid it next time

  • Do not use launchctl load in install scripts: launchctl(1) says load and unload return zero except on usage errors, which hides failures. Use bootstrap and bootout and check the exit status.
  • Decide per service whether it needs a GUI login before provisioning, and make the choice explicit in the plist location: ~/Library/LaunchAgents or /Library/LaunchAgents for agents, /Library/LaunchDaemons for daemons.
  • In provisioning scripts that run over SSH, test launchctl print gui/$(id -u) first and stop with a clear message instead of letting an installer fail with 125.

Sources

Share