---
name: "capng_change_id"
section: "3"
platform: "linux"
package: "libcap-ng"
version: "0.9.6-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/capng_change_id"
---

# capng\_change\_id(3)

change the credentials retaining capabilities

## NAME

capng\_change\_id - change the credentials retaining capabilities

## SYNOPSIS

**#include \<cap-ng.h\>**

int capng\_change\_id(int uid, int gid, capng\_flags\_t flag);

## DESCRIPTION

This function will change uid and gid to the ones given while retaining the capabilities previously specified in capng\_update. It is also possible to specify -1 for either the uid or gid in which case the function will not change the uid or gid and leave it "as is". This is useful if you just want the flag options to be applied (assuming the option doesn't require more privileges that you currently have).

It is not necessary and perhaps better if capng\_apply has not been called prior to this function so that all necessary privileges are still intact. The caller may be required to have CAP\_SETPCAP capability still active before calling this function or capabilities cannot be changed. Do not specify CAP\_SETPCAP in the capability sets unless you intended to have it after the uid change. The library will silently add and remove it if it can. But if its specified, it will leave it alone.

This function also takes a flag parameter that helps to tailor the exact actions performed by the function to secure the environment. The option may be or'ed together. The legal values are:

- **CAPNG\_NO\_FLAG**

  Simply change uid and retain specified capabilities and that's all.

- **CAPNG\_DROP\_SUPP\_GRP**

  After changing the primary group ID, remove all supplementary groups. If no new primary group ID is supplied (gid == -1), this flag has no effect.

- **CAPNG\_INIT\_SUPP\_GRP**

  After changing id, initialize any supplemental groups that may come with the new account. If given with **CAPNG\_DROP\_SUPP\_GRP** it will have no effect.

- **CAPNG\_APPLY\_STAGED\_GROUPS**

  Available in libcap-ng 0.9.3 and later. Apply additional groups previously staged with [**capng\_stage\_additional\_groups**(3)](/capng_stage_additional_groups/3.md). If used by itself, the staged list is applied exactly. If combined with **CAPNG\_INIT\_SUPP\_GRP** the account's natural supplemental groups are initialized first and the staged gids are then merged in without duplicates. If combined with **CAPNG\_DROP\_SUPP\_GRP** the call fails. If this flag is set and no staged gids are present, the call fails. Staged gids are intended to be used once and are always cleared before **capng\_change\_id** returns, even when the flag is omitted or the call fails.

- **CAPNG\_CLEAR\_BOUNDING**

  Clear the bounding set regardless to the internal representation already setup prior to changing the uid/gid.

- **CAPNG\_APPLY\_BOUNDING**

  Available in libcap-ng 0.9.3 and later. Apply the internally prepared bounding set during **capng\_change\_id**. This only acts on bounding-set state explicitly prepared with [**capng\_clear**(3)](/capng_clear/3.md), [**capng\_fill**(3)](/capng_fill/3.md), or [**capng\_update**(3)](/capng_update/3.md) using **CAPNG\_BOUNDING\_SET**. If no prepared bounding-set state exists, this flag is a no-op. If combined with **CAPNG\_CLEAR\_BOUNDING** the call fails.

- **CAPNG\_CLEAR\_AMBIENT**

  Clear ambient capabilities regardless of the internal representation already setup prior to changing the uid/gid.

## RETURN VALUE

This returns 0 on success and a negative number on failure.

- \-1 means capng has not been initted properly

  - \-2 means a failure requesting to keep capabilities across the uid change

  - \-3 means that applying the intermediate capabilities failed

  - \-4 means changing gid failed

  - \-5 means initializing supplemental groups failed

  - \-6 means changing the uid failed

  - \-7 means dropping the ability to retain caps across a uid change failed

  - \-8 means clearing the bounding set failed

  - \-9 means dropping CAP\_SETPCAP or ambient capabilities failed

  - \-10 means resolving the target account for supplemental groups failed

  - \-11 means dropping supplemental groups failed

  - \-12 means CAPNG\_DROP\_SUPP\_GRP and CAPNG\_APPLY\_STAGED\_GROUPS were both set

  - \-13 means CAPNG\_APPLY\_STAGED\_GROUPS was requested with no staged gids

  - \-14 means applying staged additional groups failed

  - \-15 means looking up a group account in additional groups failed

  - \-16 means merging initialized and staged additional groups failed

  - \-17 means CAPNG\_APPLY\_BOUNDING and CAPNG\_CLEAR\_BOUNDING were both set

  Applications that need to support libcap-ng releases older than 0.9.3 should detect the availability of **CAPNG\_APPLY\_BOUNDING** and **CAPNG\_APPLY\_STAGED\_SUPP\_GRP** and [**capng\_stage\_additional\_groups**(3)](/capng_stage_additional_groups/3.md) before using this flow. Callers that need the prepared bounding-set behavior must request it explicitly with **CAPNG\_APPLY\_BOUNDING** so older callers continue to see the historical **capng\_change\_id** behavior.

Note: the only safe action to do upon failure of this function is to probably exit. This is because you are likely in a situation with partial permissions and not what you intended.

## SEE ALSO

[**capng\_update**(3)](/capng_update/3.md), [**capng\_apply**(3)](/capng_apply/3.md), [**capng\_stage\_additional\_groups**(3)](/capng_stage_additional_groups/3.md), [**prctl**(2)](/prctl/2.md), [**capabilities**(7)](/capabilities/7.md)

## AUTHOR

Steve Grubb
