Hardcoding credentials into deployment scripts, configuration files, or terminal histories is one of the fastest ways to compromise an infrastructure. When automating server setups and operational workflows, teams need a reliable, scriptable way to store and retrieve sensitive data without exposing it in plain text.
KeePassXC solves this problem by storing every secret inside a single encrypted database file protected by one master password. Because it ships with a robust command-line interface (keepassxc-cli), you can integrate vault lookups and updates directly into your terminal workflows while maintaining desktop compatibility with the exact same database file.
Two Ground Rules for Secret Management
Before installing any tools or writing automation scripts, establish two non-negotiable operational rules:
- Never write secrets in plain text: A secret should only ever be typed at an interactive prompt—never passed directly on a command line, hardcoded into a script, or saved in a document.
- Treat the database file as the vault itself: Anyone who possesses both the encrypted database file and the master password has access to every secret inside; consequently, backing up your secrets is as simple as keeping a secure copy of that file.
1. Installing KeePassXC and keepassxc-cli
On Ubuntu 24.04, you can install KeePassXC directly from the developers’ official package archive (PPA) using --no-install-recommends to keep the footprint lean on headless or automation hosts:
sudo add-apt-repository -y ppa:phoerious/keepassxc
sudo apt-get update
sudo apt-get install -y --no-install-recommends keepassxc
keepassxc-cli --version
This installs keepassxc-cli (such as version 2.8.0). On a workstation, the same package provides the graphical desktop application, allowing you to manage the identical .kdbx file either visually or from the shell.
2. Initializing the Vault Database
Store your vault in a dedicated directory with restricted permissions (700) so only your user account can access it. To create a new encrypted database—for example, ~/Secrets/yourdomain.kdbx—run:
mkdir -p ~/Secrets && chmod 700 ~/Secrets
keepassxc-cli db-create -p ~/Secrets/yourdomain.kdbx
The tool will prompt you to set a master password. To keep subsequent commands clean and reusable across your terminal session, assign the database path to a shell variable:
DB=~/Secrets/yourdomain.kdbx
Keep in mind that every keepassxc-cli command prompts for your master password first before executing.
3. Essential CLI Commands for Daily Operations
Inside a KeePassXC database, each entry can store a title, username, password, URL, notes, and file attachments. Entries are organized hierarchically into groups and referenced by their path (Group/Title).
Organizing Groups and Adding Entries
Start by creating a logical group to hold related credentials (such as Mail):
keepassxc-cli mkdir $DB "Mail"
When adding an entry, you can either type the secret interactively at the prompt (-p) or let KeePassXC generate a strong 24-character alphanumeric password automatically (-g -L 24 -l -U -n):
# Add an entry and type the password at the prompt
keepassxc-cli add -u "<user name>" --url "<url>" --notes "<notes>" -p $DB "Mail/<title>"
# Add an entry with an auto-generated 24-character password (letters and digits)
keepassxc-cli add -u "<user name>" --url "<url>" --notes "<notes>" -g -L 24 -l -U -n $DB "Mail/<title>"
If an entry needs to hold keys, certificates, or configuration files, you can attach them directly to the entry:
keepassxc-cli attachment-import $DB "Mail/<title>" <name in the entry> <file>
Listing, Searching, and Retrieving Secrets
To inspect your vault structure or locate a specific entry:
keepassxc-cli ls -R -f $DB # List every group and entry, one per line
keepassxc-cli ls $DB "Mail" # List entries inside a specific group
keepassxc-cli search $DB <word> # Search for entries whose title contains <word>
When reading an entry, keepassxc-cli hides the password by default. You can explicitly reveal attachment names or print the raw password when needed for a deployment step:
keepassxc-cli show $DB "Mail/<title>" # Show metadata with password hidden
keepassxc-cli show --show-attachments $DB "Mail/<title>" # Include attached file names
keepassxc-cli show -s -a password $DB "Mail/<title>" # Print the password to the terminal
To extract an attached file back out of the vault:
keepassxc-cli attachment-export $DB "Mail/<title>" <name in the entry> <file>
Rotating Passwords and Updating Entries
Rotating a password in the vault follows the same pattern as creating one—either enter a new password manually or generate a fresh 24-character alphanumeric secret:
# Enter a new password at the prompt
keepassxc-cli edit -p $DB "Mail/<title>"
# Generate a new 24-character password (prompts only for the master password)
keepassxc-cli edit -g -L 24 -l -U -n $DB "Mail/<title>"
Both commands confirm the update with Successfully edited entry <title>, after which you can retrieve the new value using show -s -a password. Remember, this updates the vault only; any downstream server or web account will keep expecting the old credential until you apply the change there as well.
You can also update metadata, rename entries, or reorganize them across groups:
keepassxc-cli edit -u "<new user name>" $DB "Mail/<title>"
keepassxc-cli edit --url "<new url>" --notes "<new notes>" $DB "Mail/<title>"
keepassxc-cli edit -t "<new title>" $DB "Mail/<title>" # Rename an entry
keepassxc-cli mv $DB "<Group>/<title>" "Mail" # Move to another group
Deleting Entries Safely
Removing an entry is a two-stage safety process. The first rm command moves the entry into the Recycle Bin group; running rm on the entry inside Recycle Bin deletes it permanently:
keepassxc-cli rm $DB "Mail/<title>" # Move to Recycle Bin
keepassxc-cli rm $DB "Recycle Bin/<title>" # Permanently delete
With KeePassXC installed, your database initialized, and the core CLI commands at your fingertips, you have a portable, encrypted single source of truth for your infrastructure. From here, you can safely integrate automated secret retrieval into your provisioning workflows without ever leaking credentials into your codebase.