هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
KC=/opt/keycloak/bin/kcadm.sh
$KC config credentials --server https://sso.example.com --realm master --user admin
$KC create realms -s realm=company -s enabled=true
$KC create clients -r company -s clientId=grafana -s publicClient=false -s standardFlowEnabled=true -s 'redirectUris=["https://grafana.example.com/login/generic_oauth"]'
$KC create users -r company -s username=alice -s email=alice@example.com -s enabled=true -s 'requiredActions=["CONFIGURE_TOTP"]'
$KC set-password -r company --username alice --new-password 'Temp-Password-123' --temporaryStep by step
- Own realm — keep
masterfor Keycloak admins only; users and apps go into a realm likecompany. - OIDC client per app (quick answer, line 4) — confidential (
publicClient=false), authorization code flow (standardFlowEnabled), and the exact redirect URI of the app. - Get the client secret for the app's config:
CID=$($KC get clients -r company -q clientId=grafana --fields id --format csv --noquotes)
$KC get clients/$CID/client-secret -r company- Users and groups — create users (line 5) or connect LDAP/Active Directory (User federation). Put people in groups (
grafana-admins,grafana-viewers) and add a groups mapper to the client so the token contains them. - MFA —
CONFIGURE_TOTPmakes the user set up an authenticator app at first login; make OTP required for the whole realm in the authentication flow. - Connect the app — every OIDC app needs the same facts, found at
https://sso.example.com/realms/company/.well-known/openid-configuration. For Grafana:
[auth.generic_oauth]
enabled = true
name = Company SSO
client_id = grafana
client_secret = $__file{/etc/grafana/keycloak-secret}
scopes = openid email profile
auth_url = https://sso.example.com/realms/company/protocol/openid-connect/auth
token_url = https://sso.example.com/realms/company/protocol/openid-connect/token
api_url = https://sso.example.com/realms/company/protocol/openid-connect/userinfo
role_attribute_path = contains(groups[*], 'grafana-admins') && 'Admin' || 'Viewer'- Offboarding — disable the user in Keycloak: all SSO apps refuse new logins at once (existing app sessions end at their next token refresh).
- Run Keycloak safely — HTTPS with the right
hostnamesetting, behind a proxy withproxy-headers xforwarded, a real database (PostgreSQL), backups, and the admin console not open to the internet.
Check that it worked
The discovery URL returns JSON with issuer https://sso.example.com/realms/company; Grafana shows "Sign in with Company SSO"; alice logs in, must set a new password and an OTP, and gets the role from her group; after disabling alice in Keycloak she cannot log in again.
Common problems
| Problem | Cause | Fix |
|---|---|---|
Invalid parameter: redirect_uri | Redirect URI in the client differs from the app's. | Copy the exact URL (scheme, host, path); no trailing slash differences. |
Login loops or invalid issuer | Keycloak builds URLs with the wrong host or http behind a proxy. | Set hostname; proxy-headers xforwarded; HTTPS end to end. |
| App gets no groups | No groups mapper on the client. | Add a "Group Membership" mapper (claim groups, no full path). |
invalid_client / unauthorized | Wrong client secret or a public client. | Copy the secret again; publicClient=false. |
| Token errors "not active yet / expired" | Clock difference between servers. | Time sync (chrony) on Keycloak and apps. |
| Admin console reachable from the internet | Same hostname for users and admins. | Separate admin hostname, or block /admin at the proxy. |
Go further
- Command pages: kcadm, curl.
- Next how-tos: Build a Grafana dashboard, Deploy an app with Argo CD from Git, Set the time zone and sync the clock, Lock or disable a user account.
- Learn it: Secrets services — Keycloak. Full guide: Keycloak.
1Why create your own realm instead of using master?
The master realm is for administering Keycloak itself; keeping users and apps in their own realm limits the damage of a mistake.
2What happens when you disable a user in Keycloak?
All apps that log in through Keycloak refuse new logins for that user, so one switch removes access everywhere.