Deploy mlab-agent on a fleet
Install, enrol and run mlab-agent on many Linux hosts without a person at each one: unattended enrolment, the systemd service, an Ansible playbook, policy distribution, upgrades and removal.
This page is for installing mlab-agent on many hosts at once, with configuration management or an image pipeline. It assumes you have read The agent, which walks through one host by hand. For Kubernetes nodes, use the k3sec DaemonSet instead: k3sec on Kubernetes.
Linux only. The packages are built for Linux x86_64 (amd64) with glibc 2.36 or later, and the only service integration is systemd. mlab-agent install refuses to run on any other OS ("sets up a systemd service: Linux only"). There is no Windows service, MSI or macOS package, so this page has no GPO or Intune section.
What an unattended install does
Each step is safe to run again, which is what configuration management needs:
| Step | Why running it again is harmless |
|---|---|
| Package install | The postinst script creates the mlab-agent user and /etc/mlab-agent only if missing, and restarts a running agent on upgrade (systemctl try-restart). |
enroll --if-unenrolled | Does nothing if the host is already enrolled: it prints already enrolled (<dir>), nothing to do and exits 0. |
| Policy file | A plain file you own. The agent reads it again on every request. |
systemctl enable --now | Already enabled and running stays that way. The unit only starts once /etc/mlab-agent/agent.toml exists (ConditionPathExists). |
Enrolment codes for many hosts
Every agent joins a workspace with a one-time code from Agents › Enrol an agent:
| Code | Agents it enrols | Valid for |
|---|---|---|
| Host code | 1 | 15 minutes |
| Code for a number of nodes (the A Kubernetes cluster option) | up to that number, at most 200 | 24 hours |
The server records how many times each code was used and refuses it once the count is reached or it has expired. The agent sends the code in the same way whatever kind it is. For a large fleet, make one code per batch of up to 200 hosts and roll the batch out within 24 hours.
Keep the code off the command line, where it is visible in ps and shell history. --code-file reads it from a file and trims surrounding whitespace:
sudo -u mlab-agent mlab-agent enroll \
--server https://mlab.sh \
--code-file /run/mlab-enroll-code \
--expect-fingerprint "<fingerprint from the dialog>" \
--label "$(hostname)" \
--if-unenrolled| Flag | Effect |
|---|---|
--server <url> | The mlab.sh instance (required). |
--code <code> / --code-file <path> | The enrolment code, one or the other (one is required). |
--expect-fingerprint <fp> | mlab.sh's key fingerprint from the dialog. Enrolment stops if it does not match. Always pass it in automation. |
--label <name> | The name shown in the Agents tab. |
--if-unenrolled | Do nothing if this host is already enrolled. Cannot be combined with --force. |
--force | Replace an existing enrolment (a host that was revoked, or moved to another workspace). |
Enrol as the mlab-agent user: agent.key must be owner-only and owned by the account that runs the service. The code is only needed once, so delete the code file after enrolment.
The service
The package installs /usr/lib/systemd/system/mlab-agent.service:
| Setting | Value |
|---|---|
| Runs | /usr/bin/mlab-agent run as mlab-agent:mlab-agent, UMask=0077 |
| Starts | After network-online.target, and only if /etc/mlab-agent/agent.toml exists |
| Restarts | Restart=on-failure, RestartSec=10 |
| Writes | Only /etc/mlab-agent (ProtectSystem=strict, ReadWritePaths=/etc/mlab-agent), with home directories read-only and a private /tmp |
| Privileges | No capabilities (CapabilityBoundingSet= empty), NoNewPrivileges, kernel tunables, modules, logs and control groups protected, and only AF_INET, AF_INET6 and AF_UNIX sockets |
| Not set | MemoryDenyWriteExecute, because tools may JIT (engram's YARA engine does) |
sudo systemctl enable --now mlab-agent
systemctl status mlab-agent
journalctl -u mlab-agent -fLogs go to stderr, so they end up in the journal. Their level is set with RUST_LOG (default info), for example through a drop-in:
# /etc/systemd/system/mlab-agent.service.d/log.conf
[Service]
Environment=RUST_LOG=debugmlab-agent exits 0 on success and 1 on any error, with the reason in the log.
Hosts without the package
On a host where you copied the binary yourself (an image without access to apt.mlab.sh, for example), mlab-agent install writes svc.sh, which installs the same hardened unit pointed at that binary, config directory and user:
mlab-agent --config /opt/mlab-agent/conf install --user svc-mlab --out /opt/mlab-agent
sudo /opt/mlab-agent/svc.sh install # also: uninstall, start, stop, restart, statussvc.sh install refuses to continue if systemd is missing, the user does not exist, the host is not enrolled, policy.toml is missing, or agent.key is owned by someone other than the service user. Paths and user names may only contain letters, digits and / . _ - + @, and neither the binary nor the config directory may live under /tmp or /var/tmp (the service gets a private /tmp). Without --user, the service runs as the user who called sudo.
Ansible example
This playbook is an example built only from the commands and paths on this page. Adapt it to your inventory and test it on one host first. It targets Debian and Ubuntu. For the RPM family, replace the repository tasks with the pkg.mlab.sh/rpm/mlab.repo download shown on mlab-agent.
# deploy-mlab-agent.yml
# Variables (keep the code in Ansible Vault):
# mlab_server: https://mlab.sh
# mlab_enroll_code: "<code for N agents>"
# mlab_fingerprint: "<mlab.sh key fingerprint>"
# mlab_policy_template: files/policy.toml.j2
- hosts: mlab_agents
become: true
tasks:
- name: mlab apt key
ansible.builtin.get_url:
url: https://apt.mlab.sh/mlab.asc
dest: /etc/apt/keyrings/mlab.asc
mode: "0644"
- name: mlab apt repository
ansible.builtin.copy:
dest: /etc/apt/sources.list.d/mlab.sources
mode: "0644"
content: |
Types: deb
URIs: https://apt.mlab.sh
Suites: stable
Components: main
Signed-By: /etc/apt/keyrings/mlab.asc
- name: Install the agent and engram
ansible.builtin.apt:
name: [mlab-agent, engram]
state: present
update_cache: true
- name: Check whether the host is enrolled
ansible.builtin.stat:
path: /etc/mlab-agent/agent.toml
register: enrolled
- name: Enrol
when: not enrolled.stat.exists
block:
- name: Write the code for the mlab-agent user only
ansible.builtin.copy:
dest: /run/mlab-enroll-code
content: "{{ mlab_enroll_code }}"
owner: mlab-agent
mode: "0400"
no_log: true
- name: mlab-agent enroll
become_user: mlab-agent
ansible.builtin.command: >
mlab-agent enroll --server {{ mlab_server }}
--code-file /run/mlab-enroll-code
--expect-fingerprint {{ mlab_fingerprint }}
--label {{ inventory_hostname }}
--if-unenrolled
always:
- name: Remove the code
ansible.builtin.file:
path: /run/mlab-enroll-code
state: absent
- name: Hash of engram-tool, to pin in the policy
ansible.builtin.command: mlab-agent tools hash /usr/bin/engram-tool
register: engram_sha
changed_when: false
- name: Install the policy (root-owned, readable by the agent)
ansible.builtin.template:
src: "{{ mlab_policy_template }}"
dest: /etc/mlab-agent/policy.toml
owner: root
group: mlab-agent
mode: "0640"
notify: restart mlab-agent
- name: Check that every declared tool gets ready
become_user: mlab-agent
ansible.builtin.command: mlab-agent tools check
register: tools_check
changed_when: false
failed_when: "'Failed' in tools_check.stdout"
- name: Run the agent
ansible.builtin.systemd:
name: mlab-agent
enabled: true
state: started
handlers:
- name: restart mlab-agent
ansible.builtin.systemd:
name: mlab-agent
state: restartedmlab-agent tools check exits 0 as soon as the policy parses. It prints one line per tool (name version state ops: …) with a state of Running, Disabled or Failed, so the playbook checks the output, not the exit code. Run it as mlab-agent: run as root, it would create root-owned work directories under /etc/mlab-agent/tools/ that the service cannot write to.
Distributing policy.toml
mlab.sh never pushes a policy. The file is yours, and the agent can read it but not replace it: /etc/mlab-agent is root:mlab-agent with mode 1770 (sticky), and the package resets an existing policy.toml to root:mlab-agent 0640.
A template for the Ansible example above, built from the policy reference and the engram settings:
# files/policy.toml.j2
enabled = true
upload_hosts = ["<storage host from the Policy editor>"]
[tools.engram]
path = "/usr/bin/engram-tool"
sha256 = "{{ engram_sha.stdout }}"
enabled = true
allowed_ops = ["list_dumps", "get_index", "mem_read", "phys_read", "extract_pe"]
[tools.engram.settings]
dump_dirs = ["/srv/dumps"]
max_read_len = 65536
collect_environment = true
redact_secrets = trueBuild the first version in the workspace's Policy editor, which fills in upload_hosts and checks your draft against what agents report, then turn it into a template. When a policy change applies:
| Change | Applies |
|---|---|
enabled (kill switch), allowed_ops, upload_hosts, the policy file itself | On the next request: the agent reads the policy on every request |
A tool's path or sha256, a new tool | After systemctl restart mlab-agent: tools are started once, when the agent starts |
Unknown keys in a tool's settings are refused: the tool does not start and shows Failed.
enabled = false is the fleet-wide kill switch. Pushing it to every host refuses every request, ping included, without stopping the service or losing the enrolment.
Upgrades
Upgrade the packages. apt / dnf upgrades restart a running agent on the new binary (postinst runs systemctl try-restart):
sudo apt update && sudo apt install --only-upgrade mlab-agent engramPin the new tool hashes. A new engram-tool (or any tool) has a new SHA-256, and the agent refuses it until the policy carries the new hash. In the Ansible example, the tools hash task and the template handle this, and the handler restarts the agent. By hand:
mlab-agent tools hash /usr/bin/engram-tool
# update sha256 in [tools.engram], then:
sudo systemctl restart mlab-agentCheck. sudo -u mlab-agent mlab-agent tools check on the host, or the Tools tab of the workspace, which shows the version each agent runs and whether a newer one is published.
Upgrade the tool package and the policy together. Between the two, the tool shows Failed ("does not match the SHA-256 pinned in the policy").
Removal
sudo apt remove mlab-agent # or: sudo dnf remove mlab-agentRemoval stops and disables the service (prerm). It keeps /etc/mlab-agent (identity, keys, audit log) and the mlab-agent user, so reinstalling resumes the same enrolment. To forget the host entirely:
sudo rm -rf /etc/mlab-agent
sudo userdel mlab-agentThen revoke the agent in the workspace's Agents tab. For an agent installed with svc.sh, sudo ./svc.sh uninstall removes the unit and keeps the config directory.
See also
- mlab-agent: commands, files and the policy.
- The agent: the single-host walkthrough and troubleshooting.
- engram: the memory-forensics tool the agent runs.
assay
Offline-first, single-binary scanner for ML model artifacts - safetensors, GGUF and PyTorch pickle. Assay the weights before you trust them.
k3sec on Kubernetes
Run k3sec on every node of a cluster from the official DaemonSet: what it is allowed to do, how to configure it, how to write and test detection rules, and how to upgrade it.