The Problem: The Nightmare of Leaking Secrets in Git Commits
Most of us have accidentally committed a .env file to GitHub at some point. Inside lies all kinds of sensitive data: database passwords, AWS secret keys, or Stripe API tokens. According to statistics from GitGuardian, millions of secrets are leaked and flagged across public repositories every year. Even if you quickly delete the commit or force push, credentials remain exposed through commit history or scanner bot caches.
The issue gets even trickier as the team grows to 5–10 microservices scattered across multiple Ubuntu servers. Sharing secrets through decentralized .env files becomes a major operational burden. Who has access to production passwords? When an engineer leaves, how do you rotate passwords across dozens of services without downtime? HashiCorp Vault was designed to solve this exact headache.
4 Core Concepts to Understand Before Running Commands
To avoid getting overwhelmed by HashiCorp’s extensive documentation, you only need to master these four foundational concepts:
- Storage Backend: The encrypted data store for Vault. Vault supports multiple backends such as Raft (Integrated Storage), Consul, PostgreSQL, or simply the local File System.
- Sealed & Unsealed: Upon startup, Vault is always in a Sealed state (completely locked). All API requests are rejected except for unseal operations. To unlock it, administrators must supply a threshold of Unseal Keys based on Shamir’s Secret Sharing algorithm.
- Secrets Engine: Components that handle secrets dynamically or statically. The KV (Key-Value) engine stores static configurations; the Database engine generates dynamic PostgreSQL/MySQL credentials with a Time-to-Live (TTL); and the Transit engine provides centralized data encryption via API.
- Token & Policy: Every request sent to Vault must include a Token. Each Token is bound to a Policy (written in HCL) specifying fine-grained read/write permissions on specific paths.
Hands-on: Installing and Configuring HashiCorp Vault on Ubuntu
This procedure was tested on an Ubuntu 22.04 LTS VPS (2 vCPUs, 4GB RAM). The steps below are fully compatible with Ubuntu 20.04, 22.04, and the latest 24.04 release.
Step 1: Install Vault from the Official HashiCorp Repository
Avoid installing Vault via Ubuntu’s default apt repository, as packages are often outdated. Instead, add the official HashiCorp repository:
sudo apt update && sudo apt install -y gpg coreutils curl
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install -y vault
Verify the installed build:
vault --version
Step 2: Set Up the Vault Server Configuration File
In this lab, we use local file storage and listen on the default port 8200. First, create the storage directory and transfer ownership to the system user vault:
sudo mkdir -p /vault/data
sudo chown -R vault:vault /vault
Next, write the configuration to /etc/vault.d/vault.hcl:
sudo tee /etc/vault.d/vault.hcl << 'EOF'
storage "file" {
path = "/vault/data"
}
listener "tcp" {
address = "0.0.0.0:8200"
tls_disable = 1
}
ui = true
disable_mlock = true
api_addr = "http://127.0.0.1:8200"
cluster_addr = "https://127.0.0.1:8201"
EOF
Important Note: The tls_disable = 1 option is strictly for local testing in dev/staging environments. Never enable this flag on a production server without a reverse proxy (such as Nginx with Let’s Encrypt SSL) fronting the traffic.
Step 3: Run Vault via systemd
Enable the service so Vault starts automatically whenever the system boots:
sudo systemctl enable vault
sudo systemctl start vault
sudo systemctl status vault
Export the environment variable into your bash profile to use the Vault CLI conveniently without having to specify the -address flag every time:
export VAULT_ADDR='http://127.0.0.1:8200'
echo "export VAULT_ADDR='http://127.0.0.1:8200'" >> ~/.bashrc
Step 4: Initialize and Unseal Vault
At this stage, Vault is like an empty safe without a PIN. Run the initialization command to generate the master key:
vault operator init -key-shares=5 -key-threshold=3
This command splits the master key into 5 shares. You need any 3 of them (the threshold) to unseal the vault. The console output will display 5 Unseal Key strings and 1 Initial Root Token. Make sure to back them up immediately in a secure location.
Proceed to unseal Vault using 3 different keys:
vault operator unseal <Unseal_Key_1>
vault operator unseal <Unseal_Key_2>
vault operator unseal <Unseal_Key_3>
Check the server status:
vault status
If you see Sealed: false, congratulations: Vault is unlocked and ready for data.
Step 5: Store and Retrieve Secrets
Log in using the Root Token obtained during the initialization step:
vault login <Initial_Root_Token>
Enable the Key-Value version 2 (KV v2) Secrets Engine. This version supports secret versioning and rollbacks in case of accidental overwrites:
vault secrets enable -path=secret kv-v2
Write sample database connection credentials to the path secret/my-app/staging:
vault kv put secret/my-app/staging \
db_host="10.0.1.15" \
db_port="5432" \
db_user="postgres_app" \
db_password="MySuperStrongP@ss123"
If you need to quickly generate random passwords over 24 characters with special characters, check out the Password Generator on ToolCraft. This tool runs 100% client-side via in-browser JavaScript and never sends your passwords to any server, ensuring complete peace of mind.
Read back the stored secret:
vault kv get secret/my-app/staging
Step 6: Create Policies and Provision Dedicated Application Tokens
A vital security rule: Never use the Root Token inside application source code. Instead, create a policy that grants read-only permissions:
cat << 'EOF' > app-policy.hcl
path "secret/data/my-app/*" {
capabilities = ["read"]
}
EOF
vault policy write app-read-policy app-policy.hcl
Generate a new token attached to this policy with a 24-hour Time-to-Live (TTL):
vault token create -policy="app-read-policy" -ttl=24h
From backend applications (Node.js, Golang, Python), developers can simply query the REST API using this Token:
curl -H "X-Vault-Token: <app_token>" \
-X GET http://127.0.0.1:8200/v1/secret/data/my-app/staging
The payload returned by the Vault API nests significant metadata. If you want to quickly inspect the data object without writing a test script, just paste the JSON string into the JSON Formatter & Validator on ToolCraft to format and validate syntax visually.
Conclusion
Integrating HashiCorp Vault into your infrastructure fundamentally solves the problem of hardcoded credentials, providing transparent audit logging and granular access control for every service. For large-scale production deployments, consider migrating to a high-availability (HA) Raft backend and adopting Auto-unseal with AWS KMS or GCP KMS so you never have to manually unlock the vault after server restarts.

