What is kcadm (Keycloak Admin CLI)?

What is kcadm (Keycloak Admin CLI)?

Every click an admin makes in Keycloak's web console is really just a request to an API underneath — kcadm is the tool that lets you skip the clicking and talk to that API directly, by typing.

Picture a restaurant kitchen with two ways to place an order: a waiter walks up to a touchscreen and taps through several menus, or a chef radios the order straight into the kitchen’s ticket system in one line. Both end up creating the exact same ticket — one is just faster and more repeatable once you know exactly what you want. Keycloak’s admin console is the touchscreen: friendly, visual, great for exploring. kcadm, the Keycloak Admin Command-Line Interface, is the radio — a text-based tool that talks to the same underlying Admin REST API, letting administrators create users, configure realms, and manage clients by typing commands instead of clicking through screens. This guide explains what kcadm actually is, how it fits into Keycloak’s overall architecture, what happens internally when a command runs, and how real teams use it safely and effectively for day-to-day and automated administration. No prior command-line experience is assumed.

1Core Concepts

Before kcadm makes sense as a tool, it helps to understand what it’s actually a shortcut for: Keycloak’s Admin REST API.

What is the Admin REST API?

Every action an administrator performs — creating a user, changing a client’s settings, adding a role — is, underneath the visual interface, a request sent to Keycloak’s Admin REST API, a structured set of web addresses (endpoints) that accept and return data in JSON format. The admin console you see in a browser is essentially a polished frontend built entirely on top of this same API. Nothing the console can do is impossible to do through the API directly; the console just makes it visual and clickable.

What is kcadm, then?

kcadm (short for “Keycloak Admin CLI,” and run as a script often called kcadm.sh on Linux/macOS or kcadm.bat on Windows) is a command-line tool, shipped alongside Keycloak, that wraps the Admin REST API in a much friendlier, typed-command form. Instead of manually constructing raw web requests, an administrator types a readable command like “get all users” or “create a new client,” and kcadm handles turning that into the correct underlying API call, sending it, and displaying the result.

Analogy

Think of ordering food through a restaurant’s official phone line versus trying to directly wire into their kitchen’s internal ticket-printing system yourself. Both eventually produce the same ticket, but the phone line (kcadm) gives you a simple, well-defined way to place that order correctly, without needing to know exactly how the kitchen’s internal wiring and protocols work. The Admin REST API is the kitchen’s internal system; kcadm is the well-designed phone line built specifically to talk to it correctly.

Why does kcadm exist, when the admin console already works?

The admin console is excellent for one-off, exploratory tasks — looking around, making a single configuration change, checking on a specific user. But it doesn’t scale well to repetitive, bulk, or automated work. Creating five hundred test users one by one by clicking through forms is painfully slow; doing it with a short script calling kcadm in a loop takes seconds. kcadm exists specifically to make Keycloak administration scriptable — usable inside automation pipelines, setup scripts, and repeatable processes, not just interactive point-and-click sessions.

i
Where you’d meet this

Any deployment script that sets up a fresh Keycloak realm with predefined clients and roles, any continuous-integration pipeline that provisions test users automatically, or any administrator who prefers typing a quick command over navigating several console screens relies on kcadm.

Key vocabulary, explained simply

Term

Admin REST API

Keycloak’s underlying set of structured web endpoints that both the admin console and kcadm ultimately talk to for every administrative action.

Term

kcadm Session

A locally stored, temporary authentication state kcadm keeps after you log in, so you don’t need to re-authenticate before every single command.

Term

Realm

An isolated space in Keycloak containing its own users, clients, and settings — most kcadm commands operate against one specific, currently targeted realm.

Term

Service Account

A non-human client identity, often used specifically so automation scripts using kcadm don’t need to authenticate as a real human administrator’s personal account.

Term

Endpoint Resource

The specific type of object a kcadm command targets — users, clients, roles, realms — mirroring the structure of the underlying REST API itself.

Term

JSON Payload

The structured data format kcadm commands read from or write to when creating or updating objects, matching exactly what the REST API itself expects.

2Architecture & Components

kcadm is a thin, purpose-built layer sitting directly on top of the Admin REST API, with a small number of moving parts that work together every time a command runs.
graph TB
    U[Administrator] -->|types command| KC[kcadm CLI Tool]
    KC --> CFG[Local Config File
stores session/token] KC -->|constructs request| API[Admin REST API] API --> KCS[Keycloak Server] KCS -->|JSON response| API API -->|response| KC KC -->|formatted output| U
FIG 1 — kcadm translating typed commands into Admin REST API calls and back

Component breakdown

Component

kcadm Script

The actual executable file (kcadm.sh or kcadm.bat) an administrator runs from a terminal, bundled with every Keycloak distribution.

Component

Local Configuration File

A small file kcadm maintains locally, storing the current authenticated session so subsequent commands don’t require re-entering credentials each time.

Component

Command Structure

kcadm commands generally follow a consistent shape — an action (get, create, update, delete) plus a resource path (like users or clients) plus optional data or filters.

Component

Output Formatting

kcadm returns results as JSON by default, matching the API’s own native format, making it straightforward to pipe results into other scripts or tools for further processing.

How a typical session begins

Before running any administrative commands, an administrator first authenticates kcadm against a specific Keycloak server and realm, providing credentials once. kcadm exchanges these credentials for an access token behind the scenes and stores the resulting session information in its local configuration file. Every subsequent command in that session reuses this stored token automatically, refreshing it as needed, without requiring the administrator to log in again for each individual action.

A common real setup

A deployment script authenticates kcadm using a dedicated service account created specifically for automation, then runs a sequence of commands to create a new realm, add several clients with predefined settings, and create an initial set of roles — all as part of an automated environment setup process that runs identically every time a new environment is provisioned.

Why does kcadm need its own configuration file?

Without some form of session persistence, every single kcadm command would need to include full authentication credentials, which would be both cumbersome to type repeatedly and a security risk if credentials ended up scattered across many command lines or scripts. The local configuration file’s session-based approach mirrors how the admin console itself keeps you logged in across multiple actions, just adapted to a command-line, scriptable context.

3Internal Working

Two moments matter for understanding kcadm’s internals: how it authenticates in the first place, and what actually happens between typing a command and seeing a result.

Authentication under the hood

When an administrator runs kcadm’s login command, providing a username, password, server URL, and realm, kcadm sends these credentials to Keycloak’s token endpoint, following the exact same OpenID Connect flow any other client application would use to obtain an access token. The resulting token, along with a refresh token, is saved into the local configuration file. From that point forward, every kcadm command attaches this access token to its outgoing API requests, and transparently uses the refresh token to obtain a new access token once the current one expires.

What happens when you run a command

When an administrator types a kcadm command — say, creating a new user — kcadm first parses the command’s structure: the action (create), the resource type (users), and any provided data (a JSON payload describing the new user, or individual field flags). It then constructs the exact corresponding HTTP request the Admin REST API expects, attaches the current access token for authentication, and sends the request to the Keycloak server. The server processes this exactly as it would any other API call — validating the token, checking the requester’s permissions, and performing the requested action — before returning a JSON response that kcadm then displays back to the administrator.

“kcadm doesn’t have any special, hidden access — it makes the exact same API calls the admin console makes, just from a typed command instead of a mouse click.”

Permissions work exactly the same as anywhere else

Because kcadm ultimately authenticates as a real Keycloak user or service account and calls the same Admin REST API, it is bound by exactly the same role-based permission checks as any other caller. An account without sufficient administrative privileges will be rejected by the API in precisely the same way whether the request came from kcadm, the admin console, or a custom script calling the API directly. kcadm carries no special elevated access of its own.

!
Gotcha

A kcadm session’s stored access token is only valid for a limited time, and while kcadm automatically refreshes it using the stored refresh token, a very long-running script or a session left idle far beyond the refresh token’s own lifetime can eventually require a fresh login again.

Step-by-step: from typed command to completed action

1

Administrator types a command

A command specifying the action, resource, and any needed data or flags is entered in the terminal.

2

kcadm parses and validates syntax

The tool checks that the command is well-formed before attempting to send anything to the server.

3

Stored session token attached

kcadm reads the current access token from its local configuration file and attaches it to the outgoing request.

4

Request sent to Admin REST API

The correctly formatted HTTP request reaches the Keycloak server’s Admin REST API endpoint.

5

Server validates and processes

Keycloak checks the token’s validity and the caller’s permissions, then performs the requested action if authorized.

6

Response returned and displayed

The resulting JSON response is sent back and printed to the terminal, ready to be read or piped into another tool.

4Data Flow & Lifecycle

Seeing kcadm’s full request-response journey alongside the admin console’s own path makes clear just how directly parallel the two really are.
sequenceDiagram
    participant Admin
    participant kcadm
    participant Config as Local Config File
    participant API as Admin REST API
    participant Server as Keycloak Server

    Admin->>kcadm: kcadm config credentials (login)
    kcadm->>Server: Requests access token
    Server-->>kcadm: Returns access + refresh token
    kcadm->>Config: Stores tokens locally
    Admin->>kcadm: kcadm create users -r myrealm -s username=jane
    kcadm->>Config: Reads stored access token
    kcadm->>API: POST /admin/realms/myrealm/users
    API->>Server: Validates token, checks permissions
    Server-->>API: Creates user, returns result
    API-->>kcadm: JSON response
    kcadm-->>Admin: Prints result to terminal
    
FIG 2 — A full login-then-command sequence, showing token reuse across multiple commands

Notice that after the initial login, every subsequent command reuses the same stored token rather than re-authenticating from scratch — this is exactly what makes kcadm practical for scripting a whole sequence of administrative actions in one automated run, without needing to embed a password in every individual step.

The lifecycle of a kcadm session

A

Login and configuration

An administrator or script authenticates once, and kcadm stores the resulting session details locally.

B

Active use

Multiple commands run in sequence, each reusing and, when needed, automatically refreshing the stored token.

C

Session expiry or explicit logout

Eventually the refresh token itself expires, or the administrator explicitly logs out, ending the ability to run further commands without re-authenticating.

D

Re-authentication for future use

A fresh login is required to start a new working session, repeating the cycle.

i
Worth noting

Because the local configuration file contains live session credentials, its location and file permissions matter operationally — leaving it readable by other users on a shared machine, or committing it accidentally into a script repository, effectively exposes an active administrative session.

5Advantages, Disadvantages & Trade-offs

kcadm trades the visual friendliness of the admin console for speed, repeatability, and scriptability — a trade that pays off enormously for some tasks and adds unnecessary friction for others.

Advantages

  • Makes bulk and repetitive administrative tasks fast and scriptable, rather than requiring manual clicking through many screens
  • Fits naturally into automated deployment pipelines and infrastructure-as-code style workflows
  • Produces JSON output that’s easy to feed into other tools, scripts, or automated checks
  • Requires no special installation beyond what already ships with a standard Keycloak distribution

Disadvantages & Trade-offs

  • Has a steeper learning curve than the visual admin console for administrators unfamiliar with command-line tools
  • Lacks the visual context and guided forms the console provides, making it easier to make a typo-driven mistake without immediate visual feedback
  • Requires careful handling of stored session credentials, especially in shared or automated environments
  • Complex, multi-step configuration changes can require longer, harder-to-read command sequences compared to a few clicks in the console
Analogy

Using kcadm for a single, one-off change is a bit like typing a detailed shell command to rename one file when you could just as easily drag and drop it in a file browser — technically fine, but the console’s visual approach is often simpler for that single task. But the moment you need to rename ten thousand files following a consistent pattern, the command-line approach suddenly becomes overwhelmingly more practical than clicking through each one individually.

6Security

Because kcadm can perform genuinely powerful administrative actions — creating users, changing client secrets, modifying roles — its credentials and usage patterns deserve the same security discipline as any other privileged access tool.

Credential handling in scripts and automation

A common and risky pattern is embedding an administrator’s actual username and password directly inside an automation script that calls kcadm. If that script is ever shared, committed to version control, or exposed, those credentials are compromised along with it. A safer approach uses a dedicated service account with only the specific permissions the automation actually needs, rather than a full administrator’s personal credentials.

SEC-07Recommended
Context

Automation scripts using kcadm often default to whatever administrative credentials are convenient at the time, sometimes a real human administrator’s own account, sometimes a broadly privileged service account far more powerful than the script actually needs.

Decision

Create dedicated service accounts scoped to exactly the roles and permissions each specific automation task requires, and use those, rather than a human administrator’s personal credentials or an overly broad service account, for any scripted kcadm usage.

Consequence

A compromised script or leaked credential exposes only the narrow set of permissions that specific automation genuinely needed, rather than full administrative control over the realm.

Protecting the local configuration file

Since kcadm’s local configuration file holds live session tokens, treating it with the same care as any other credential file matters — restricting its file permissions, avoiding storing it in shared or version-controlled locations, and ensuring automated systems clean up or isolate their session files appropriately between runs.

!
Common mistake

Running kcadm interactively as a full realm administrator for routine, low-risk tasks that a much more narrowly scoped account could perform just as well, unnecessarily expanding what’s exposed if that session or its stored token were somehow compromised.

Auditability of kcadm actions

Because kcadm calls the same Admin REST API as the console, actions performed through it are captured by Keycloak’s admin event logging in exactly the same way — meaning a well-configured event listener or audit log will show kcadm-driven changes with the same detail as console-driven ones, an important property for maintaining a complete audit trail regardless of which interface an administrator used.

7Monitoring, Logging & Metrics

Because kcadm is often the engine behind automated changes, keeping visibility into what it’s actually doing — and how often — matters just as much as monitoring changes made through the console.
1
underlying Admin REST API shared by both kcadm and the visual admin console
2
token types kcadm manages automatically: access and refresh
0
special or elevated access kcadm has beyond what the authenticated account is granted
Metric

Automation script failure rate

Tracking how often kcadm commands inside a deployment or provisioning script fail, since a rising failure rate often signals a Keycloak version change or an API contract shift the script hasn’t adapted to yet.

Metric

Service account command volume

Monitoring how frequently a given automation service account is used, helping distinguish routine scheduled activity from an unexpected, potentially unauthorized usage pattern.

Metric

Admin event correlation

Cross-referencing Keycloak’s own admin event log against known scheduled kcadm-driven automation runs, to confirm that changes match what was actually intended and scheduled.

Metric

Session file access patterns

On shared automation infrastructure, monitoring who or what reads or modifies kcadm’s local configuration file, given the sensitivity of the session tokens it holds.

A practical monitoring habit

Teams that lean heavily on kcadm for environment provisioning often wrap every automation script with basic logging of each command’s outcome and timestamp, feeding this into the same monitoring system used for the rest of their infrastructure automation, so a failed provisioning step is caught and alerted on immediately rather than discovered later when a new environment doesn’t behave as expected.

8Design Patterns & Anti-patterns

Teams that rely on kcadm heavily for real infrastructure work tend to settle on the same handful of dependable usage patterns, and repeatedly run into the same avoidable operational headaches.

Proven patterns

Pattern

Idempotent provisioning scripts

Write kcadm-based setup scripts that check whether a resource already exists before attempting to create it, so the same script can safely run multiple times without producing duplicate or conflicting errors.

Pattern

Dedicated automation service accounts

Use narrowly scoped service accounts specifically for scripted kcadm usage, kept entirely separate from any real administrator’s personal login.

Pattern

Version-pinned automation

Tie automation scripts to a known, tested Keycloak version, and deliberately re-validate them whenever upgrading, since Admin REST API details can shift between major versions.

Pattern

Combining kcadm with configuration-as-code

Store the desired realm, client, and role configuration as version-controlled files, using kcadm scripts to apply that configuration consistently across environments, rather than manually replicating console clicks.

Anti-patterns to avoid

Anti-pattern

  • Hard-coding a real administrator’s personal password directly inside an automation script
  • Writing non-idempotent scripts that fail or duplicate resources if accidentally run more than once
  • Sharing a single, broadly privileged kcadm session across many unrelated automation tasks
  • Ignoring Keycloak version compatibility when upgrading, only discovering broken automation after the fact

Instead, do this

  • Authenticate automation with a scoped service account, never a personal administrator login
  • Design scripts to check for existing resources before creating them
  • Scope each automation task’s service account permissions to exactly what that task needs
  • Test kcadm-based automation against new Keycloak versions before rolling out an upgrade broadly

9Best Practices & Common Mistakes

Long-term, reliable use of kcadm comes down to treating it as a first-class piece of infrastructure tooling, not an ad hoc convenience script.

Best practices

1

Keep automation scripts under version control

Treat kcadm-based provisioning scripts the same as any other important infrastructure code, with proper history and review.

2

Separate interactive and automated usage

Use personal administrator logins for exploratory, interactive kcadm sessions, and dedicated service accounts strictly for scripted automation.

3

Validate output, not just exit codes

Check that a kcadm command’s actual returned data matches expectations, rather than assuming success just because the command didn’t produce an obvious error.

4

Document the exact commands used for critical setup steps

Keep a clear record of which kcadm commands were used to configure important realm or client settings, making it easier to reproduce or audit that configuration later.

Common mistakes

!
Mistake

Assuming kcadm bypasses normal Keycloak permission checks because it’s a command-line tool, when in fact it’s bound by exactly the same role-based access control as any other API caller.

!
Mistake

Leaving a kcadm session’s stored configuration file behind on a shared build server without cleaning it up afterward, potentially leaving an active session accessible to anyone else with access to that machine.

!
Mistake

Writing large, monolithic setup scripts with no error handling, where a single failed command midway through leaves the realm in a half-configured, inconsistent state that’s hard to diagnose or safely re-run.

10Real-world & Industry Examples

kcadm shows up wherever Keycloak administration needs to happen repeatably, at scale, or as part of a larger automated system — which describes most serious production deployments sooner or later.
Industry

DevOps and platform engineering teams

Teams responsible for provisioning new environments use kcadm inside infrastructure automation to consistently set up realms, clients, and initial roles every time a new environment (development, staging, testing) is created.

Industry

Continuous integration pipelines

Automated test suites use kcadm to create and tear down test users and clients as part of each pipeline run, ensuring a clean, consistent identity setup for every automated test cycle.

Industry

Managed service providers

Organizations managing Keycloak on behalf of many customers use kcadm scripts to apply consistent baseline configuration across every customer realm, rather than manually repeating console clicks for each one.

Industry

Disaster recovery and environment replication

Teams preparing for disaster recovery scenarios use kcadm-based scripts to quickly recreate a known-good realm configuration on a new Keycloak instance, rather than relying on manual reconstruction from memory or scattered documentation.

A representative scenario

A platform team responsible for dozens of internal development environments needed each new environment’s Keycloak instance configured identically — the same realm settings, the same set of test clients, the same baseline roles. Rather than manually repeating the same series of console clicks every time, they built a kcadm-based provisioning script, checked into version control alongside their other infrastructure code, that could reliably stand up a fully configured Keycloak realm in seconds, consistently, for every new environment created.

11FAQ

Q1Is kcadm a separate product from Keycloak?
No. It’s a command-line tool bundled with standard Keycloak distributions, built specifically to interact with the same Keycloak server’s Admin REST API.
Q2Can kcadm do anything the admin console can’t?
Generally not — both ultimately call the same Admin REST API. kcadm’s main advantage is scriptability and speed for repetitive tasks, not access to hidden functionality.
Q3Do I need special permissions to use kcadm?
You need whatever administrative permissions the specific actions you’re performing require — kcadm itself grants no special access beyond what the account you authenticate with already has.
Q4Is it safe to use kcadm in automated scripts?
Yes, when done carefully — using a dedicated, narrowly scoped service account rather than a personal administrator login, and handling the local session configuration file with appropriate care.
Q5What happens if my kcadm session expires mid-script?
kcadm automatically attempts to refresh an expired access token using its stored refresh token; if the refresh token itself has also expired, a fresh login is required before further commands will succeed.
Q6Does using kcadm show up in Keycloak’s audit logs?
Yes. Because kcadm calls the same Admin REST API as the console, its actions are captured by Keycloak’s admin event logging in the same way as any other administrative change.

12Summary and Key Takeaways

Key Takeaways

  • kcadm is Keycloak’s official command-line tool for administration, wrapping the same Admin REST API the visual admin console itself uses.
  • It exists mainly to make Keycloak administration scriptable and repeatable, especially for bulk operations and automated deployment pipelines.
  • kcadm authenticates like any other client, storing an access and refresh token locally so a whole sequence of commands can run without repeated logins.
  • It carries no special elevated access — every command is subject to exactly the same role-based permission checks as any other Admin REST API caller.
  • Security best practice favors dedicated, narrowly scoped service accounts for scripted kcadm usage, rather than a human administrator’s personal credentials.
  • The local configuration file holding session tokens deserves the same protective handling as any other credential file, especially on shared automation infrastructure.
  • Well-designed kcadm automation is idempotent, version-controlled, and validated against real command output, not just the absence of an obvious error.
  • Because it shares the same underlying API, kcadm-driven changes appear in Keycloak’s admin event log just like console-driven ones, preserving a complete audit trail regardless of which interface was used.