Skip to content

kcadm.sh command builder

Build kcadm.sh commands to create realms, users, clients, roles and groups from the command line, with the sign-in step first.

Runs in your browser. Nothing you paste is sent to us.

Command

A made-up server is filled in.

Sign in with

kcadm.sh

1. Sign in (once per session)
kcadm.sh config credentials --server https://auth.example.com --realm master --user admin
2. Run
kcadm.sh create users -r acme -s username=alice -s enabled=true
kcadm.sh asks for the password, so it stays out of your shell history.
In the Keycloak container the tool is /opt/keycloak/bin/kcadm.sh. It saves its session in ~/.keycloak/kcadm.config.

How to use it

  1. Enter the server and how you sign in.
  2. Pick the realm to work in and the task.
  3. Fill in the names the task needs.
  4. Run the sign-in command once, then the task.

What to know

kcadm.sh is Keycloak’s admin command-line tool, in the bin folder of every Keycloak install. It wraps the Admin REST API and keeps its session in ~/.keycloak/kcadm.config.

With -s it sets single fields; with -f it reads a whole JSON object, which is easier for clients and for keys that contain dots.

Signing in with a user asks for the password, so it stays out of your shell history. For automation, sign in with a client and a secret instead.

What kcadm.sh is

kcadm.sh is the admin command-line tool that ships with every Keycloak distribution. It sits in the bin folder next to kc.sh, and in the official container image at /opt/keycloak/bin/kcadm.sh. On Windows the same tool is kcadm.bat.

It is a thin client for the Admin REST API. Each command turns into one or more HTTP calls, and the answers come back as JSON. Anything the admin console can change, kcadm.sh can change too, from a terminal or a script.

Teams reach for it when a change must be repeatable: setting up a new realm the same way in every environment, adding users in bulk, or fixing something on a server where the console is not reachable from outside.

How kcadm.sh works in Keycloak 26

Using kcadm.sh is always two parts. First you sign in once with config credentials. The tool saves the session in ~/.keycloak/kcadm.config and refreshes it as needed. Then you run commands such as create, get, update, delete, set-password and add-roles.

Sign in, then create a user in the sample realm acme
kcadm.sh config credentials --server https://auth.example.com --realm master --user admin
kcadm.sh create users -r acme -s username=alice -s enabled=true
kcadm.sh set-password -r acme --username alice --new-password "$NEW_PASSWORD" --temporary
kcadm.sh add-roles -r acme --uusername alice --rolename editor

Setting fields with -s and -f

With -s name=value you set one field of the JSON the command sends. Values that are lists or objects are written as JSON, for example -s 'redirectUris=["https://app.example.com/*"]'. With -f file.json, or -f - to read from standard input, you send a whole object at once.

For clients, -f is easier. Client attributes have dots in their names, such as pkce.code.challenge.method, and a whole JSON object avoids any doubt about how those keys are read.

Choosing the realm

The realm you sign in to and the realm you work in can differ. An admin in the master realm signs in with --realm master and then passes -r acme to each command. A service account in realm acme signs in to acme directly.

What this tool does

The builder writes the sign-in command and the command for one task. You choose how to sign in, the realm to work in and the task, and fill in names. It quotes values for the shell and shows hints for the ids some commands need.

  • Create a realm.
  • Create a user, set a password (optionally temporary), and find a user by username.
  • Create an OpenID Connect client from a JSON object with PKCE required, and find a client by client id.
  • Create a realm role, give a user a realm role or a client role.
  • Create a group and add a user to a group.

Everything happens in your browser. The tool never connects to your server and never asks for a password: sign-in with a user prompts for it, and the other secrets are read from shell variables.

What it does not do

It cannot look up ids, so adding a user to a group shows placeholders for the user and group ids. It covers common tasks, not every kcadm.sh command, and it does not run anything for you.

How to use it

  1. Enter the server address people use to reach Keycloak.
  2. Choose Admin user or Client secret, and the realm to sign in to.
  3. Enter the realm to work in and pick the task.
  4. Fill in the username, client id, role or group.
  5. Run the sign-in command once per session, then the task command. Read secrets with read -rs NEW_PASSWORD or read -rs CLIENT_SECRET first.

Running kcadm.sh in a container

With the official image you can run the tool inside the running container. When HTTP is enabled, as it usually is behind a proxy, the address inside the container is http://localhost:8080. That skips the proxy and works even when the console is not public.

From the host, against a Compose service named keycloak
docker compose exec keycloak /opt/keycloak/bin/kcadm.sh config credentials \
  --server http://localhost:8080 --realm master --user admin
docker compose exec keycloak /opt/keycloak/bin/kcadm.sh get realms --fields realm,enabled

On Kubernetes the same idea works with kubectl exec. The session file is written inside the container, so it is gone when the pod restarts.

Where it pays off

A few situations where a short kcadm.sh script beats clicking through the console.

First production deploy

Write the realm, its roles, groups and first clients as a script. Run it on staging, check the result, then run the same script on production. The two environments start identical.

Break-glass access

When the admin console is not exposed publicly, kcadm.sh inside the container still works. You can unlock an account, reset a password or fix a client without opening the console to the internet.

Automation and CI

Pipelines can sign in with a client and secret instead of a person’s password. Give that client only the realm-management roles the job needs, and keep its secret in the pipeline’s secret store.

Upgrades and migrations

After moving a realm to a new server, a few commands confirm that clients, roles and users arrived. kcadm.sh get clients -r acme --fields clientId is a quick list to compare against the old server.

Common errors and fixes

MessageUsual causeFix
Session has expired or no credentialsYou have not signed in, or the saved session ran out.Run config credentials again.
invalid_grant on sign-inWrong username or password, or the user is in another realm.Check the user and the --realm you sign in to.
HTTP 403The account lacks the role for the command.Assign the needed realm-management role, or use an account that has it.
HTTP 409 ConflictA user, client, role or group with that name exists.Use get to find it and update instead of create.
SSL or certificate errorsThe server uses a certificate the tool does not trust.Point the tool at a trust store that holds the CA with kcadm.sh config truststore, or call the server from inside its container.

If commands fail with connection errors, check the --server address. It must be the base address of Keycloak, without /realms or /admin at the end.

Security notes

  • Sign in with a user and let kcadm.sh prompt for the password. Typing it on the command line leaves it in your shell history and process list.
  • The session file holds tokens. Protect ~/.keycloak/kcadm.config like a password file, and delete it on shared machines.
  • For automation, use a dedicated client with only the roles it needs. Revoking it does not affect anyone else.
  • Temporary passwords force a change at the next sign-in. Use them whenever you set a password for someone else.

Best practices

  • Use --fields to keep output short, for example --fields id,username,enabled.
  • Use -q queries to find things by name before you change them.
  • Keep larger objects, such as clients, as JSON files in version control and create them with -f.
  • Make scripts safe to rerun: check with get before you create.
  • Test every script on a staging copy before production.

Commands in this tool use made-up names such as realm acme and user alice. Replace them with your own.

kcadm.sh or the REST API

Both reach the same endpoints. kcadm.sh handles tokens for you and has shortcuts such as add-roles that look up role ids by name. Plain HTTP calls fit better in languages and tools that already speak HTTP. The Admin API request builder writes the curl version of the same tasks.

Questions

Frequently asked questions

Where is kcadm.sh in the Keycloak Docker image?

At `/opt/keycloak/bin/kcadm.sh`. Run it with `docker exec` or `docker compose exec`, or from any machine that has the same Keycloak distribution unpacked.

How do I log in with kcadm.sh?

Run `kcadm.sh config credentials --server <url> --realm master --user <admin>` and type the password when asked. For a service account, use `--client <id> --secret <secret>` instead of `--user`.

How do I create a user with kcadm.sh?

Run `kcadm.sh create users -r <realm> -s username=<name> -s enabled=true`. Then set a password with `kcadm.sh set-password -r <realm> --username <name> --new-password <password> --temporary`.

How do I assign a role with kcadm.sh?

Use `kcadm.sh add-roles -r <realm> --uusername <user> --rolename <role>`. For a client role, add `--cclientid <client>`. The tool looks up the role ids for you.

How do I add a user to a group with kcadm.sh?

Find both ids with `kcadm.sh get users -r <realm> -q username=<name>` and `kcadm.sh get groups -r <realm>`. Then run `kcadm.sh update users/<user-id>/groups/<group-id> -r <realm> -s realm=<realm> -s userId=<user-id> -s groupId=<group-id> -n`.

Why does kcadm.sh say the session has expired?

The saved tokens in `~/.keycloak/kcadm.config` ran out or were removed. Run `config credentials` again to start a new session.

Can I use kcadm.sh in a CI pipeline?

Yes. Sign in with a confidential client and its secret, read from the pipeline’s secret store. Give the client only the `realm-management` roles the job needs.

How do I list users or clients with kcadm.sh?

Run `kcadm.sh get users -r <realm>` or `kcadm.sh get clients -r <realm>`. Add `--fields` to choose the columns you need, and `-q` to filter, for example `-q username=alice`. In a large realm, page through the results with `--offset` and `--limit`.

Is kcadm.sh the same as kc.sh?

No. `kc.sh` starts, builds and exports the server. `kcadm.sh` talks to a running server through the Admin REST API to change realms, clients and users.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.