Skip to content

The agent that grows with you

fix(gateway): refuse user-scope systemd install in containers on every fresh-install entry

teknium1c4e8fcda7d7b
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index 649ae0334ad..b778af0bc29 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -2487,6 +2487,32 @@ def print_systemd_scope_conflict_warning() -> None:
print_info(" sudo hermes gateway uninstall --system")
+def refuses_container_user_scope_install(system: bool) -> bool:
+ """True (after printing the guidance) when a fresh USER-scope unit was requested inside a container.
+
+ A systemd container passes ``supports_systemd_services()`` on purpose so ``--system`` keeps working,
+ but a user unit there is not container-scoped: the unit file and its ``default.target.wants`` symlink
+ land in ``~/.config/systemd/user`` — commonly the host's own home bind-mounted in — so the host's
+ ``systemd --user`` enables it too and a second gateway polls the same bot token outside the container.
+ Callers decide between ``sys.exit(1)`` (CLI) and skipping the install (wizard)."""
+ if system or not is_container():
+ return False
+ print_error("Refusing to install a user-scope systemd gateway service inside a container.")
+ _print_info_lines(
+ "The unit file and its enable symlink would be written to the home directory, which is",
+ "commonly the host's own home bind-mounted in — the host's user manager then enables and",
+ "starts the same unit, so a second gateway polls the same bot token outside the container",
+ "(Telegram: 'Conflict: terminated by other getUpdates request').",
+ "",
+ " hermes gateway run # run as the container's main process",
+ " docker run --restart unless-stopped ... # container restart policy",
+ "",
+ "If systemd manages this container (systemd as PID 1), install an isolated system service instead:",
+ " sudo hermes gateway install --system --run-as-user <user>",
+ )
+ return True
+
+
def _require_root_for_system_service(action: str) -> None:
if os.geteuid() != 0: # windows-footgun: ok — POSIX systemd helper, never invoked on Windows
raise SystemScopeRequiresRootError(f"System gateway {action} requires root. Re-run with sudo.", action)
@@ -2572,6 +2598,8 @@ def install_linux_gateway_from_setup(force: bool = False, enable_on_startup: boo
systemd_install(force=force, system=True, run_as_user=run_as_user, enable_on_startup=enable_on_startup)
return scope, True
+ if refuses_container_user_scope_install(system=False):
+ return scope, False
systemd_install(force=force, system=False, enable_on_startup=enable_on_startup)
return scope, True
@@ -6235,16 +6263,7 @@ def _cmd_install(args):
_no_backend_exit("install", "termux")
backend = _service_backend()
if backend == "systemd":
- if is_container() and not system:
- print_error(
- "Refusing to install a user-scope systemd gateway service inside a container."
- )
- _print_info_lines(
- "A bind-mounted home can make that unit visible to the host user manager.",
- "If systemd manages this container, install an isolated system service instead:",
- " sudo hermes gateway install --system",
- "Otherwise run the gateway under the container runtime's supervisor.",
- )
+ if refuses_container_user_scope_install(system):
sys.exit(1)
_install_systemd_from_cli(args, force=force, system=system, run_as_user=run_as_user)
elif backend == "launchd":
diff --git a/tests/hermes_cli/test_gateway_service.py b/tests/hermes_cli/test_gateway_service.py
index f7412fab82b..9f6564047db 100644
--- a/tests/hermes_cli/test_gateway_service.py
+++ b/tests/hermes_cli/test_gateway_service.py
@@ -1904,6 +1904,19 @@ class TestDockerAwareGateway:
assert calls == [{"force": False, "system": True, "run_as_user": None}]
+ def test_setup_wizard_user_scope_in_container_skips_install(self, monkeypatch, capsys):
+ """The wizard's default "user service" choice is the same host-visible unit (#112323):
+ inside a container it prints the guidance and reports no install instead of writing it."""
+ monkeypatch.setattr(gateway_cli, "is_container", lambda: True)
+ monkeypatch.setattr(gateway_cli, "prompt_linux_gateway_install_scope", lambda: "user")
+ monkeypatch.setattr(
+ gateway_cli, "systemd_install",
+ lambda **kwargs: pytest.fail("must not install a user unit in a container"),
+ )
+
+ assert gateway_cli.install_linux_gateway_from_setup(force=False, enable_on_startup=True) == ("user", False)
+ assert "--system" in capsys.readouterr().out
+
class TestLegacyHermesUnitDetection:
"""Tests for _find_legacy_hermes_units / has_legacy_hermes_units.
diff --git a/website/docs/user-guide/messaging/index.md b/website/docs/user-guide/messaging/index.md
index f6a932a2ae9..d65eae3ae7a 100644
--- a/website/docs/user-guide/messaging/index.md
+++ b/website/docs/user-guide/messaging/index.md
@@ -597,6 +597,10 @@ hermes ALL=(root) NOPASSWD: /usr/bin/systemctl --no-ask-password reset-failed he
Avoid keeping both the user and system gateway units installed at once unless you really mean to. Hermes will warn if it detects both because start/stop/status behavior gets ambiguous.
+:::note Inside a container, only the system scope is offered
+`hermes gateway install` (and the `hermes gateway setup` wizard) refuse to install a **user** service when Hermes detects it is running inside a container. A user unit lands in `~/.config/systemd/user`, and when that home is bind-mounted from the host (podman/distrobox), the host's own `systemd --user` enables and starts the same unit — a second gateway polling the same bot token. Run the gateway as the container's main process (`hermes gateway run`, with a container restart policy), or in a systemd container (systemd as PID 1) install the isolated system scope: `sudo hermes gateway install --system --run-as-user <user>`.
+:::
+
:::info Multiple installations
If you run multiple Hermes installations on the same machine (with different `HERMES_HOME` directories), each gets its own systemd service name. The default `~/.hermes` uses `hermes-gateway`; other installations use `hermes-gateway-<hash>`. The `hermes gateway` commands automatically target the correct service for your current `HERMES_HOME`.
:::