Skip to content

fix(scripts): regenerate machine-id and SSH host keys per board on first boot - #424

Merged
gmmcosta15 merged 5 commits into
devfrom
fix/per-board-identity
Nov 20, 2026
Merged

gmmcosta15 merged 5 commits into
devfrom
fix/per-board-identity

Conversation

@gmmcosta15

@gmmcosta15 gmmcosta15 commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Description

  • Bug Fix

RF50 SD images are cloned from a booted box, so every printer shares /etc/machine-id and SSH host keys. NetworkManager derives stable-privacy IPv6 addresses from the machine-id, so boxes on one network get the same IPv6 addresses. avahi then reports a host name conflict and renames the box to <hostname>-N.local. This change gives each board its own identity on its first boot.

Changes

  • scripts/hostname-helper.sh: regenerates the identity once per board, while the boot unit runs before basic.target (systemctl is-system-running reports initializing). Each step has its own marker holding the CPU serial, so a failure retries only that step and never re-rolls an earlier one:

    1. machine-id (/var/lib/blocksscreen/identity-serial): replace /etc/machine-id atomically, keep /var/lib/dbus/machine-id a symlink to it, and delete a v1 NetworkManager secret_key. v2 keys are hashed with the machine-id; v1 keys are used raw and would keep the shared IPv6 seed. NM writes a fresh v2 key at start.
    2. SSH host keys (/var/lib/blocksscreen/hostkeys-serial): generate into a staging dir, rename over the old keys, then delete key types and host certificates the new set does not replace. If ssh-keygen fails, the current keys stay.

    Each marker is written after a sync.

    The gate is the boot state, not a unit flag. Release-2.0 boxes run an older unit, and only this script rewrites it at boot, because updates hard-reset the tree and post-merge never runs. Those boxes regenerate on their first reboot after updating.

  • tests/scripts/test_hostname_helper.py: covers:

    • regeneration once per serial, again on a new serial, and removal of stale key types;
    • a failing ssh-keygen keeping the current keys and retrying only them, with the machine-id stable across retries;
    • deleting only a v1 secret_key, including a binary one;
    • regeneration only before basic.target.

Motivation

Each printer needs a unique machine-id for unique IPv6 addresses and a stable .local name. Shared SSH host keys also let one box impersonate another.

What changes with the machine-id on RF50:

  • IPv6 stable-privacy addresses: change once (intended).
  • SSH host key: changes once; clients drop the old known_hosts entry.
  • IPv4 DHCP lease: unchanged. No connection sets dhcp-client-id/dhcp-duid and no NetworkManager.conf override exists, so the client ID stays MAC-based.
  • journald: Storage=volatile, so no per-machine-id log directory is lost.
  • Tailscale: own node state, address unchanged.
  • udev MACAddressPolicy=persistent: not applicable, every interface has a permanent MAC.
  • Klipper, Moonraker, KlipperScreen, Spoolman, Happy-Hare: nothing reads the machine-id or host keys.

Notes:

  • systemd, journald and udev have already read the old id when the unit runs; they pick up the new one on the next boot. dbus, NetworkManager and sshd start after basic.target and see the new identity.
  • The kernel CRNG is seeded by the bootloader before userspace, so early ssh-keygen has full entropy. Regeneration adds 1 to 5 s to that one boot, mostly RSA key generation.
  • Long term, factory images should ship with an empty /etc/machine-id and no host keys so systemd and sshd create them natively; this change repairs boards already in the field.

Tests

  • pytest tests/scripts/test_hostname_helper.py: 12 passed. shellcheck is clean.

@gmmcosta15 gmmcosta15 self-assigned this Nov 20, 2026
@gmmcosta15
gmmcosta15 requested a review from a team November 20, 2026 10:20
@gmmcosta15 gmmcosta15 added the bug Something is broken, or a fix for it label Nov 20, 2026
@gmmcosta15
gmmcosta15 merged commit e462a78 into dev Nov 20, 2026
10 checks passed
@gmmcosta15
gmmcosta15 deleted the fix/per-board-identity branch November 20, 2026 11:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something is broken, or a fix for it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants