Keycloak 26.3.3 β€” Resetting the Admin Password (Step-by-Step Guide)

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

πŸ‘‰ Now, the only way to create/reset the admin user is by using environment variables KEYCLOAK_ADMIN and KEYCLOAK_ADMIN_PASSWORD before starting the server.

If an admin account already exists in the database, these environment variables will be ignored β€” you must clear the data or reset the DB.


2. Verify Environment Variables

Check if the variables are set:

echo $KEYCLOAK_ADMIN
echo $KEYCLOAK_ADMIN_PASSWORD

Or:

printenv | grep KEYCLOAK

Expected:

admin
StrongP@ss!123

3. Start Keycloak with New Admin Credentials

Development mode (file-based / H2 database):

export KEYCLOAK_ADMIN=admin
export KEYCLOAK_ADMIN_PASSWORD='StrongP@ss!123'

cd /opt/keycloak
./bin/kc.sh start-dev

On success, logs should show:

Creating initial admin user: admin

4. Fix: Admin Password Still Invalid

If you get β€œInvalid username or password”:

Case A β€” Using dev mode (H2/file store)

Reset by removing local data:

/opt/keycloak/bin/kc.sh stop 2>/dev/null || true
rm -rf /opt/keycloak/data

export KEYCLOAK_ADMIN=admin
export KEYCLOAK_ADMIN_PASSWORD='StrongP@ss!123'
/opt/keycloak/bin/kc.sh start-dev

This forces Keycloak to recreate the admin user.


Case B β€” Using MariaDB (LAMPP integration)

If Keycloak is connected to MariaDB, you need to reset the DB schema:

# Stop Keycloak
/opt/keycloak/bin/kc.sh stop 2>/dev/null || true

# Login to MariaDB
/opt/lampp/bin/mysql -u root -p

# Inside MariaDB shell
DROP DATABASE keycloak;
CREATE DATABASE keycloak CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
GRANT ALL PRIVILEGES ON keycloak.* TO 'keycloak'@'localhost' IDENTIFIED BY 'your_db_password';
FLUSH PRIVILEGES;
EXIT;

Now restart Keycloak with DB config:

export KEYCLOAK_ADMIN=admin
export KEYCLOAK_ADMIN_PASSWORD='StrongP@ss!123'

/opt/keycloak/bin/kc.sh start \
  --db=mysql \
  --db-url=jdbc:mariadb://127.0.0.1:3306/keycloak \
  --db-username=keycloak \
  --db-password=your_db_password

5. Common Errors & Fixes

  • invalid_user_credentials β†’ Admin exists in DB, env vars ignored β†’ wipe data/reset schema.
  • expired_code β†’ Old login token cached β†’ open an Incognito tab or clear cookies for localhost.
  • No β€œCreating initial admin user” line β†’ Admin was not recreated β†’ confirm env vars are set in the same shell that starts Keycloak.
  • Using sudo β†’ sudo clears env vars β†’ either sudo -E or start without sudo.

6. Persistence of Env Vars

To avoid retyping exports every time, add them to:

~/.bashrc

Example:

export KEYCLOAK_ADMIN=admin
export KEYCLOAK_ADMIN_PASSWORD='StrongP@ss!123'

Reload:

source ~/.bashrc

If using systemd service:

[Service]
Environment=KEYCLOAK_ADMIN=admin
Environment=KEYCLOAK_ADMIN_PASSWORD=StrongP@ss!123

7. Final Verification

Log in at:

http://localhost:8080

with:

  • Username: admin
  • Password: StrongP@ss!123

βœ… With this tutorial, anyone on your team can reset the Keycloak admin password for both dev mode and MariaDB setup safely

Senior Software Development Engineer at Cotocus

Related Posts

Complete Tutorial: Fixing Keycloak UDP Socket Exhaustion, DNS Failure, GitHub Resolution Error, and Laravel Guzzle ConnectException on a Single Server

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now This…

Read More

Keycloak Multi-Client Architecture with Project-Based Email Validation (Student, Trainer, Company, Consulting)

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now When…

Read More

Complete Step-by-Step Guide to Running and Managing Keycloak 26.3.3 on Linux (Production Ready)

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now 1….

Read More

Complete Tutorial: Running Keycloak 26.x on Ubuntu with LAMPP (MariaDB) in Production

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now 1….

Read More

Complete Guide: Migrating Legacy Wizbrand Users to Keycloak & Customizing Email Templates

Limited Time Offer! For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly. Master DevOps, SRE, DevSecOps Skills! Enroll Now When…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments