# Enroll a device again

> Enroll a device again before its certificate's year is up, move it to its new record, and remove the old one.

Maya's laptop was enrolled almost a year ago, and the date `enroll` printed then is close. Past it, the device client
stops connecting, and the laptop's security events and AI tools agent go quiet. You want it enrolled again before
then, with no gap and no stale record left behind.

Enrolling again makes a **new** record, with a new device id and sensor id. The old record stays, and counts toward
the realm's device cap, until you remove it. Renewal over the device's own connection, and re-enrollment into the same
record, are [planned](../../enrollment/whats-next/#certificate-renewal).

## 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/)).
- Someone with administrator rights on the laptop, and the device address and sign-in address it was enrolled with.
- The realm has room for one more device while both records exist ([Device limits](../../enrollment/device-limits/)).
- The date to enroll again before: `enroll` printed it at the last enrollment, and the realm doesn't show it.

## Enroll it again

<Steps>

1. **Find the old record.** List the realm's devices and pick the one whose `uid` is `maya-laptop`:

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

   Keep its `id` as the old device id, and note any `agents` recorded on it.

2. **Make a token**:

   ```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": 4}'
   ```

3. **Enroll the laptop again, under a new name.** While the old record exists, its sensor still holds the old name,
   and an enrollment under a name that's held is refused:

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

   ```sh
   sudo /var/cvyuh/bin/cvyuh-uem enroll --name maya-laptop-2 \
     --server <device address> --token <enrollment token> \
     --issuer https://foobar.cvyuh.com/oauth2
   ```

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

   ```sh
   sudo /Library/Cvyuh/bin/cvyuh-uem enroll --name maya-laptop-2 \
     --server <device address> --token <enrollment token> \
     --issuer https://foobar.cvyuh.com/oauth2
   ```

   </TabItem>
   </Tabs>

   It prints the new name and the next date to enroll again before. Note the date.

4. **Restart the device client**, so it connects with its new certificate:

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

   ```sh
   sudo systemctl restart cvyuh-uem
   ```

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

   ```sh
   sudo launchctl kickstart -k system/com.cvyuh.endpoint
   ```

   </TabItem>
   </Tabs>

5. **Check the new record.** List the realm's devices again: `maya-laptop-2` is there, `active` and connected. Keep
   its `id`. If agents were recorded on the old record, record them on the new one
   ([Device records](../../devices/device-records/#set-it-up)).

6. **Remove the old record**, by the old device id:

   ```sh
   curl -X DELETE https://foobar.cvyuh.com/api/v0/endpoint/ppm/v1/<old device id> \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
   ```

   The realm answers `202`, and removes the old sensor registration with it.

</Steps>

<Aside type="note" title="New ids">
  The laptop's device id and sensor id are new. Anything you keyed on the old ones, such as a saved Detection &
  Response filter on its sensor id, needs the new ones.
</Aside>

## Check it worked

Reading the old record answers `404` once its removal has finished, and the list holds one record for the laptop,
`maya-laptop-2`, `active` and connected.

## If it didn't work

| Symptom | Cause | Fix |
|---|---|---|
| `enroll` is refused with `409`, the name already held | The name is in use, by this laptop's old enrollment or another device | Pick another name |
| `enroll` is refused with `409`, the realm at its cap | Both records need a place for a moment | Remove a device you no longer need, or have the cap raised ([Device limits](../../enrollment/device-limits/)) |
| The new record never shows `connected` | The client wasn't restarted, or can't reach the device address | Restart it (step 4), then see the device client's [Troubleshooting](../../enrollment/device-client/#troubleshooting) |
| The date passed before you got to it | The client stopped connecting at the date | Do the same steps: enrolling again works on an expired device too |

## Undo it

Until step 6 the old record is still there, but the laptop now holds only its new certificate, so it can't go back to
the old record. If the new enrollment can't connect, fix that first (above) and remove the old record after.

## Related

- [Device enrollment](../../enrollment/device-enrollment/): every `enroll` option, and what each refusal means.
- [Device removal](../../revoke-and-remove/removal/): what removing the old record does.
- [What's next for enrollment](../../enrollment/whats-next/): renewal with no new token, and re-enrollment into the
  same record.
