Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

kcadm

The admin CLI of Keycloak, the open-source identity server: realms, users, groups, roles and OIDC/SAML clients from scripts.

Level
DevSecOps service
Group
Secrets
Needs root
No
Package
keycloak
Also called
kcadm.sh

What it does

Keycloak gives single sign-on (OIDC, OAuth 2.0, SAML) for apps, Grafana, Argo CD, Kubernetes dashboards and more, with MFA, user federation (LDAP/AD) and identity brokering (Entra ID, GitHub). kcadm.sh (in bin/ of the Keycloak folder or container) calls the admin REST API, so you can create realms, clients and users in automation.

A realm is a separate space of users and apps; a client is an app that uses Keycloak to log users in.

How to write it

kcadm.sh COMMAND ENDPOINT [-r REALM] [-s key=value ...] [ARGS]

Main parts

Commands

config credentials
Log in to the server.
create ENDPOINT
Create (realms, users, clients, roles).
get ENDPOINT
Read.
update ENDPOINT
Change.
delete ENDPOINT
Delete.
set-password
Set a user password.
add-roles / remove-roles
Give or take roles.
get-roles
Show roles.

Options 8

Most used

Flags

--server URL
Keycloak address (config credentials).
--realm master --user admin
Where and who to log in as.
-r REALM
Target realm.
-s key=value
Set a field.
-f FILE
JSON body from a file.
-q key=value
Query filter (get).
--fields a, b
Only some fields.
-i
Print only the new ID (create).

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1Run Keycloak in a container for the lab

    podman run -d --name keycloak -p 8080:8080 -e KC_BOOTSTRAP_ADMIN_USERNAME=admin -e KC_BOOTSTRAP_ADMIN_PASSWORD=ChangeMe! quay.io/keycloak/keycloak:26.0 start-dev
  2. 2Log in with the CLI

    podman exec -it keycloak bash
    /opt/keycloak/bin/kcadm.sh config credentials --server http://localhost:8080 --realm master --user admin
  3. 3Create a realm with brute-force protection

    kcadm.sh create realms -s realm=lab -s enabled=true -s bruteForceProtected=true -s 'passwordPolicy="length(12) and notUsername"'
  4. 4Create a user and set a password

    kcadm.sh create users -r lab -s username=hosam -s enabled=true -s email=hosam.test@example.com
    kcadm.sh set-password -r lab --username hosam --new-password 'Temp-Pass-2026!' --temporary
  5. 5Create an OIDC client for Grafana

    kcadm.sh create clients -r lab -s clientId=grafana -s protocol=openid-connect -s publicClient=false -s 'redirectUris=["https://grafana.lab.local/login/generic_oauth"]' -i
  6. 6Read the client secret

    kcadm.sh get clients -r lab -q clientId=grafana --fields id
    kcadm.sh get clients/3f2a91c0-1d2e-4f5a-9b8c-7d6e5f4a3b2c/client-secret -r lab
  7. 7Create a group and a role, give the role

    kcadm.sh create groups -r lab -s name=sre
    kcadm.sh create roles -r lab -s name=admin
    kcadm.sh add-roles -r lab --uusername hosam --rolename admin
  8. 8Require OTP for all users

    kcadm.sh update authentication/required-actions/CONFIGURE_TOTP -r lab -s defaultAction=true
  9. 9List users

    kcadm.sh get users -r lab --fields username,enabled,email
  10. 10Check the OIDC discovery document

    curl -s http://localhost:8080/realms/lab/.well-known/openid-configuration | jq '.issuer, .token_endpoint'
  11. 11Export a realm for Git (in the container)

    /opt/keycloak/bin/kc.sh export --dir /tmp/export --realm lab

Watch out

  • start-dev is only for labs (HTTP, no cache cluster). Production needs start with TLS and a database.
  • Change the bootstrap admin, enable MFA for admins, and do not expose /admin publicly.

Files

~/.keycloak/kcadm.config
CLI login token.

Tips to remember

  • Manage realms as code with the Keycloak Terraform provider or keycloak-config-cli.

Exit codes

0Success.
1Error.