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.
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 editorSetting 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
- Enter the server address people use to reach Keycloak.
- Choose Admin user or Client secret, and the realm to sign in to.
- Enter the realm to work in and pick the task.
- Fill in the username, client id, role or group.
- Run the sign-in command once per session, then the task command. Read secrets with
read -rs NEW_PASSWORDorread -rs CLIENT_SECRETfirst.
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.
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,enabledOn 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
| Message | Usual cause | Fix |
|---|---|---|
| Session has expired or no credentials | You have not signed in, or the saved session ran out. | Run config credentials again. |
invalid_grant on sign-in | Wrong username or password, or the user is in another realm. | Check the user and the --realm you sign in to. |
| HTTP 403 | The account lacks the role for the command. | Assign the needed realm-management role, or use an account that has it. |
| HTTP 409 Conflict | A user, client, role or group with that name exists. | Use get to find it and update instead of create. |
| SSL or certificate errors | The 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.shprompt 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.configlike 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
--fieldsto keep output short, for example--fields id,username,enabled. - Use
-qqueries 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
getbefore youcreate. - 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.