# Enroll a device at install

> Hand the enrollment token to the package installer, so a device installs and enrolls in one step from your provisioning script.

Ben's new laptop is being set up by your provisioning script, and you want it to come out of setup already enrolled
into your realm, with no one typing an `enroll` command. The package's installer enrolls the device itself when it
finds the token and addresses it needs.

## Before you start

- You hold `MODIFY` on the realm's endpoint settings (`RealmOwner` has it; see
  [Who can call](../../api/#who-can-call)), with your session in `$CVYUH_SESSION`
  ([Call the API](../../../develop/api/)).
- The device runs Linux or macOS, the script installs as root, and the package isn't installed on it yet.
- You have the package for the device's platform and the device address (a host and port), both from whoever runs
  your deployment.

The variables are named for your deployment: these steps use `CVYUH_`, and the device client `cvyuh-uem`.

## Enroll it at install

<Steps>

1. **Make one token per device.** A token enrolls one device, so the script asks for one for each machine it sets
   up:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/endpoint/ppm/v1/enrolment-tokens \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{"ttlHours": 2}'
   ```

   Take `token` from the answer.

2. **Install with the values where the installer reads them.** Name the device with `CVYUH_NAME`, or leave it out
   for the host name:

   <Tabs syncKey="os">
   <TabItem label="Linux">

   The installer reads the values from its environment:

   ```sh
   sudo CVYUH_SERVER=<device address> CVYUH_TOKEN=<enrollment token> \
     CVYUH_ISSUER=https://foobar.cvyuh.com/oauth2 CVYUH_NAME=ben-laptop \
     apt install ./<package>.deb
   ```

   Use `dnf install ./<package>.rpm` for the rpm. Then start the device client:

   ```sh
   sudo systemctl enable --now cvyuh-uem
   ```

   </TabItem>
   <TabItem label="macOS">

   The installer reads the values from the file `/tmp/cvyuh_envs`, then deletes it. Because `/tmp` is shared by
   every user of the Mac, write the file so that only root can read or change it: refuse if the file is already
   there, create it empty, owned by root with mode `0600`, check that, and only then write the token into it. Fill
   in the values, then run:

   ```sh
   sudo sh -s <<'SCRIPT' && sudo installer -pkg ./<package>.pkg -target /
   set -eu
   f=/tmp/cvyuh_envs
   if [ -e "$f" ] || [ -L "$f" ]; then
     echo "$f already exists: not writing the token. Find out who made it." >&2
     exit 1
   fi
   umask 077
   set -C
   : > "$f"
   if [ ! -f "$f" ] || [ -L "$f" ] || [ "$(stat -f '%Su %Lp' "$f")" != "root 600" ]; then
     echo "$f isn't root's own with mode 600: not writing the token." >&2
     exit 1
   fi
   cat >> "$f" <<'EOF'
   export CVYUH_SERVER=<device address>
   export CVYUH_TOKEN=<enrollment token>
   export CVYUH_ISSUER=https://foobar.cvyuh.com/oauth2
   export CVYUH_NAME=ben-laptop
   EOF
   SCRIPT
   ```

   The package is installed only if the file was written. The installer starts the device client.

   </TabItem>
   </Tabs>

   The installer prints `Enrolling with the site...`, then the name the device was enrolled as and the date to enroll
   it again before. On macOS these lines go to the installer's log, `/var/log/install.log`.

</Steps>

<Aside type="caution" title="If /tmp/cvyuh_envs is already there">
  Don't delete it and carry on: someone on the Mac may have made it to read the token or to change what the installer
  runs. Check who owns it with `ls -l /tmp/cvyuh_envs`. Only if it's left over from your own earlier run, owned by
  root, remove it with `sudo rm /tmp/cvyuh_envs` and run the step again. Otherwise treat the Mac as compromised.
</Aside>

<Aside type="caution" title="Keep the token out of logs">
  Until it's spent or expires, anyone holding the token can enroll a machine into your realm, and it can't be
  withdrawn. Keep lifetimes short, and don't let your provisioning tool record the installer's environment or the
  script above.
</Aside>

## Check it worked

List the realm's devices:

```sh
curl https://foobar.cvyuh.com/api/v0/endpoint/ppm/v1 \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
```

`ben-laptop` is there with `status` `active` and `connected` `true`. On macOS, check that `/tmp/cvyuh_envs` is gone:
`ls /tmp/cvyuh_envs` should find nothing.

## If it didn't work

| Symptom | Cause | Fix |
|---|---|---|
| The installer prints `Error: enrolment failed; run ... enroll by hand.` | The realm refused the token, or the device couldn't reach it; the installer finishes installing anyway | Run `enroll` by hand, as in [Device enrollment](../../enrollment/device-enrollment/), and read its reason in [If enrollment fails](../../enrollment/device-enrollment/#if-enrollment-fails). The token is still good unless it was spent or expired |
| No `Enrolling` line at all | The values weren't found: on Linux, `sudo` dropped them; on macOS, the file wasn't there, or the package was already installed | Pass the variables on the same `sudo` line as the install. On a Mac that already has the package, the installer doesn't enroll: run `enroll` by hand |
| `/tmp/cvyuh_envs` is still there after install | The installer didn't run its enrollment, so it didn't delete the file | Remove it now, `sudo rm -f /tmp/cvyuh_envs`; it holds the token |

## Undo it

Remove the device from the realm, and uninstall the package on the device. See
[Device removal](../../revoke-and-remove/removal/).

## Related

- [Get started with Endpoint Management](../../get-started/): enroll one device by hand, and see it report in.
- [Device enrollment](../../enrollment/device-enrollment/): how a token and a certificate work, and every `enroll`
  option.
- [Device limits](../../enrollment/device-limits/): how many devices the realm may have.
