HashiCorp Vault is an enterprise-grade secrets management tool designed to securely store, access, and manage sensitive data such as API keys, passwords, certificates, and encryption keys. This guide intends to teach you a full setup for Secrets Management with HashiCorp Vault on VPS.
Unlike traditional methods, Vault provides centralized secrets management with strong encryption, access control, and audit capabilities.
Key features of HashiCorp Vault include:
Centralized Secret Storage: Encrypts secrets at rest and stores them in a backend of your choice.
Dynamic secrets: Generates short-lived credentials on demand and can rotate and revoke them automatically.
Encryption as a service: Apps encrypt and decrypt without handling encryption keys directly.
Access control policies: Use path-based rules to grant fine-grained permissions.
Audit logging: Records Vault activity for security reviews and compliance.
High availability: Runs in clusters with failover for production reliability.
You can proceed to the following steps to set up HashiCorp Vault on a VPS, including installation, configuration, and secrets management.
If you need a reliable VPS server, you can visit PerLod Hosting, which offers the best plans to deploy HashiCorp Vault secret management.
Prerequisites for Setting Up Secrets Management with HashiCorp Vault on VPS
To set up secrets management with HashiCorp Vault on VPS, you need some requirements. Be sure to meet these prerequisites:
A VPS running Ubuntu 22.04 or later.
Root or sudo access to the server.
At least 2GB RAM and 20GB disk space.
SSH access to your VPS.
A domain name or IP address for accessing Vault.
If you need a reliable Linux VPS with full sudo access and public networking, PerLod provides the best services for your needs.
Connect to your VPS via SSH and update the system packages with the commands below:
Bash
sudoaptupdate&&sudoaptupgrade-y
Install required dependencies for adding the HashiCorp repository:
Bash
sudoaptinstallgpgwgetcurl-y
Once you are done with these, proceed to the following steps to set up Secrets Management with HashiCorp Vault on VPS.
Step 1. Install HashiCorp Vault
To install Vault safely on your VPS, you can use HashiCorp’s official APT repository so the package can be installed and updated through your system’s package manager.
Add the HashiCorp GPG key to verify package authenticity with the command below:
Update the package index and install Vault with the following commands:
Bash
sudoaptupdatesudoaptinstallvault-y
You can verify the installation by checking its version:
Bash
vault--version
Step 2. Create HashiCorp Vault User and Directories
At this point, you need to create a dedicated Vault user and the standard directories Vault needs for data, TLS files, and configuration. This allows Vault to run as a non-root user, separates its files into predictable locations, and applies restrictive permissions.
Create a dedicated system user for Vault with the command below:
Step 3. Generate TLS Certificates for Vault Secrets Management
TLS is required to protect Vault traffic in transit, especially on a VPS exposed to the internet. In this step, we want to generate a private key and a self-signed certificate for testing.
For production, replace the self-signed cert with one issued by a trusted CA to avoid errors.
Generate a private key with the following command:
At this point, you must create the vault.hcl file, which is the main server config that tells Vault where to store data, how to accept client connections, and which addresses it should announce to clients and other Vault nodes.
Bash
sudotee/etc/vault.d/vault.hcl>/dev/null<< EOF# Storage Backend - Raft Integrated Storagestorage"raft"{path="/opt/vault/data"node_id="node1"}# TCP Listener with TLSlistener"tcp"{address="0.0.0.0:8200"tls_cert_file="/opt/vault/tls/vault-cert.pem"tls_key_file="/opt/vault/tls/vault-key.pem"}# API and Cluster Addressesapi_addr="https://YOUR_VPS_IP:8200"cluster_addr="https://YOUR_VPS_IP:8201"# Enable UIui=true# Disable memory locking (required for non-root users)disable_mlock=true# Log levellog_level="Info"EOF
Configuration Explanation:
path: Local directory where Raft stores data and snapshots.
node_id: Unique identifier for this Vault node in a cluster.
address = "0.0.0.0:8200": Binds to all network interfaces on port 8200.
tls_cert_file: Path to TLS certificate.
tls_key_file: Path to private key for TLS.
api_addr: External URL for Vault API, which is used by clients.
cluster_addr: Internal address for cluster communication on port 8201.
ui = true: Enables the web interface at https://YOUR_VPS_IP:8200/ui.
disable_mlock = true: Disables memory locking, required when running as a non-root user; prevents swapping secrets to disk.
log_level: Options are Trace, Debug, Info, Warn, Error. Info is recommended for production.
Also, set the correct permissions on the configuration file with the following commands:
To run Vault reliably in the background and start it automatically after reboots, you can set it up as a systemd service.
To create the systemd service file, run the command below:
Bash
sudotee/lib/systemd/system/vault.service>/dev/null<< 'EOF'[Unit]Description="HashiCorp Vault - A tool for managing secrets"Documentation=https://developer.hashicorp.com/vault/docsConditionFileNotEmpty=/etc/vault.d/vault.hclRequires=network-online.targetAfter=network-online.target[Service]Type=notifyUser=vaultGroup=vaultProtectSystem=fullProtectHome=read-onlyPrivateTmp=yesPrivateDevices=yesSecureBits=keep-capsAmbientCapabilities=CAP_IPC_LOCKCapabilityBoundingSet=CAP_SYSLOGCAP_IPC_LOCKNoNewPrivileges=yesExecStart=/usr/bin/vaultserver-config=/etc/vault.d/vault.hclExecReload=/bin/kill--signalHUP$MAINPIDKillMode=processKillSignal=SIGINTRestart=on-failureRestartSec=5TimeoutStopSec=30LimitNOFILE=65536LimitMEMLOCK=infinity[Install]WantedBy=multi-user.targetEOF
Set proper permissions on the service file with the following command:
Bash
sudochmod644/lib/systemd/system/vault.service
Reload systemd to recognize the new service:
Bash
sudosystemctldaemon-reload
Enable and start the Vault service with the commands below:
Bash
sudosystemctlenablevaultsudosystemctlstartvault
Check the service status that is active and running with the following command:
Bash
sudosystemctlstatusvault
To check logs and errors, you can use the command below:
Bash
sudojournalctl-uvault-n50--no-pager
Step 6. Set Vault CLI Environment Variables
To make the Vault CLI talk to your server without repeating flags on every command, you can set a few environment variables in your shell.
Set up environment variables for interacting with Vault with the commands below:
The VAULT_SKIP_VERIFY=1 skips TLS certificate verification. It is only for self-signed certs in development; do not use it in production with proper certificates.
To make these settings permanent, add them to your shell profile with the commands below:
You should see the following output that Vault is sealed and uninitialized:
Bash
Sealed:trueInitialized:false
Step 7. Initialize and Unseal Vault
Vault initialization will create the unseal key shares and the first (root) admin token, which must be captured immediately and stored securely, because Vault will not show them again and losing them can lock you out permanently.
Initialize Vault to generate unseal keys and root token with the command below:
Bash
vaultoperatorinit
This command generates 5 unseal keys by default and 1 root token.
After initialization, Vault starts in a sealed state. You must unseal it using at least 3 of the 5 unseal keys with the command below:
Bash
vaultoperatorunseal
When prompted, enter the first unseal key. Repeat this command two more times with different keys:
Bash
vaultoperatorunseal# Enter second keyvaultoperatorunseal# Enter third key
After the third key, check the status:
Bash
vaultstatus
Important note: Vault will seal automatically when:
The service is restarted.
The server reboots.
An explicit seal command is issued.
A storage error occurs.
You must unseal the Vault every time it seals.
For production environments, you can implement auto-unseal using cloud KMS or HSM.
Step 8. Vault Authentication with Root Token
At this point, you can log in to Vault using the initial root token created during initialization, so you can perform the first setup tasks like enabling auth methods, creating policies, and configuring secrets engines.
Log in to Vault using the root token with the command below:
Bash
vaultlogin
When prompted, enter your root token.
Alternatively, you can use this command:
Bash
vaultloginhvs.6j6aLuAq33GcdLQaHXRgztpM
Once authenticated, the token is stored in ~/.vault-token.
Root Token Warnings:
Root tokens have unrestricted access to all Vault operations and should only be used for initial configuration.
Revoke root token after creating administrative policies.
Use the principle of least privilege for all other operations.
Step 9. Enable and Configure Vault Secrets Engines
Secrets engines are how Vault actually stores and serves sensitive data or generates it. In this step, we want to enable the KV v2 secrets engine and then practice the core workflows.
Enable KV Version 2 Secrets Engine:
The KV (Key-Value) v2 engine provides versioned secret storage. To enable it, use the command below:
Bash
vaultsecretsenable-path=secretkv-v2
Check enabled secrets engines with the command below:
Bash
vaultsecretslist
Store Secrets:
Now you can store a secret with multiple key-value pairs with the command below:
This creates version 2. To read a specific version, you can run:
Bash
vaultkvget-version=1secret/myapp/database
View version history metadata with the command below:
Bash
vaultkvmetadatagetsecret/myapp/database
Rollback to the previous version with:
Bash
vaultkvrollback-version=1secret/myapp/database
This creates a new version (version 3) with the same data as version 1.
Delete and Destroy Secrets:
For a soft delete, you can use this command:
Bash
vaultkvdeletesecret/myapp/database
Undelete a soft-deleted secret with the command below:
Bash
vaultkvundelete-versions=2secret/myapp/database
Permanently destroy specific versions with this:
Bash
vaultkvdestroy-versions=2secret/myapp/database
Warning: Destroyed versions cannot be recovered.
Delete all versions and metadata with the command below:
Bash
vaultkvmetadatadeletesecret/myapp/database
Also, you can list all secrets at a path with this:
Bash
vaultkvlistsecret/myapp
Step 10. Configure Vault Access Policies
Access policies are Vault’s core authorization mechanism, which define exactly who can do what, on which paths, using clear capabilities.
Policy Capabilities include:
create: Create new data at a path.
read: Read data from a path.
update: Update existing data at a path.
delete: Delete data at a path.
list: List keys at a path.
sudo: Gain root-protected access to paths.
deny: Explicitly deny access.
Read-Only Policy:
To create a policy file for read-only access, you can use this command:
Bash
teeread-only-policy.hcl>/dev/null<< 'EOF'# Read-only access to all secrets under secret/myapp/path"secret/data/myapp/*"{capabilities=["read","list"]}# Allow reading metadatapath"secret/metadata/myapp/*"{capabilities=["read","list"]}# Allow token self-managementpath"auth/token/lookup-self"{capabilities=["read"]}path"auth/token/renew-self"{capabilities=["update"]}EOF
Then, write the policy to Vault:
Bash
vaultpolicywriteread-onlyread-only-policy.hcl
Admin Policy:
You can create a comprehensive admin policy file with:
Bash
teeadmin-policy.hcl>/dev/null<< 'EOF'# Read system health checkpath"sys/health"{capabilities=["read","sudo"]}# Manage ACL policiespath"sys/policies/acl/*"{capabilities=["create","read","update","delete","list","sudo"]}# Manage authentication methodspath"sys/auth/*"{capabilities=["create","read","update","delete","sudo"]}path"auth/*"{capabilities=["create","read","update","delete","list"]}# Manage secrets enginespath"sys/mounts/*"{capabilities=["create","read","update","delete","list","sudo"]}# List existing secrets enginespath"sys/mounts"{capabilities=["read"]}# Manage the Key-Value secrets enginepath"secret/*"{capabilities=["create","read","update","delete","list"]}# Manage audit devicespath"sys/audit/*"{capabilities=["create","read","update","delete","list","sudo"]}# Read audit device configurationpath"sys/audit"{capabilities=["read","sudo"]}EOF
Write the admin policy to the Vault:
Bash
vaultpolicywriteadminadmin-policy.hcl
Application Policy:
Create a policy for application access with the command below:
Bash
teeapp-policy.hcl>/dev/null<< 'EOF'# Application can create, read, update secrets under its pathpath"secret/data/myapp/*"{capabilities=["create","read","update"]}# Application can list secretspath"secret/metadata/myapp/*"{capabilities=["list","read"]}# Application can manage its own tokenpath"auth/token/renew-self"{capabilities=["update"]}path"auth/token/lookup-self"{capabilities=["read"]}EOF
Write the policy:
Bash
vaultpolicywriteapp-policyapp-policy.hcl
You can list all policies and read a specific policy with the commands below:
Bash
vaultpolicylistvaultpolicyreadadmin
Vault can show you what policy is needed for a specific operation. To do this, you can run:
Bash
vaultkvget-output-policysecret/myapp/database
Step 11. Configure Vault Authentication Methods
Authentication is how users and applications prove who they are to Vault before any policy rules are applied. In this step, we want to configure a few common auth methods, create credentials, and confirm everything is enabled and working.
Method1. Token Authentication: Tokens are the default authentication method.
To create a token with specific policies, you can run:
Bash
vaulttokencreate-policy=read-only-ttl=1h
To create a token with multiple policies, you can use:
To view all enabled authentication methods, you can use:
Bash
vaultauthlist
Step 12. Enable Vault Audit Logging
Audit logging records Vault activity so you can trace who accessed which secrets, when, and from where. It is an essential control for security monitoring and compliance.
File Audit Device: You can enable file-based audit logging by using the commands below:
Start and provide unseal keys with these commands:
Bash
sudosystemctlstartvaultvaultoperatorunseal
Also, you can create and use an automated backup script:
Bash
sudotee/usr/local/bin/vault-backup.sh>/dev/null<< 'EOF'#!/bin/bash# ConfigurationBACKUP_DIR="/opt/vault/backups"RETENTION_DAYS=30VAULT_ADDR="https://127.0.0.1:8200"VAULT_TOKEN_FILE="/root/.vault-token"# Create backup directory if it doesn't existmkdir-p"$BACKUP_DIR"# Set Vault addressexportVAULT_ADDR="$VAULT_ADDR"exportVAULT_TOKEN=$(cat"$VAULT_TOKEN_FILE")# Create snapshot with timestampTIMESTAMP=$(date+%Y%m%d-%H%M%S)SNAPSHOT_FILE="$BACKUP_DIR/vault-snapshot-$TIMESTAMP.snap"# Take snapshotvaultoperatorraftsnapshotsave"$SNAPSHOT_FILE"# Compress snapshotgzip"$SNAPSHOT_FILE"# Remove old backupsfind"$BACKUP_DIR"-name"vault-snapshot-*.snap.gz"-typef-mtime+$RETENTION_DAYS-deleteecho"Backup completed: $SNAPSHOT_FILE.gz"EOF
Make the script file executable:
Bash
sudochmod+x/usr/local/bin/vault-backup.sh
Schedule it with cron by using the commands below:
At this point, you have learned to set up Secrets Management with HashiCorp Vault on VPS, including secure installation, TLS-enabled access, persistent Raft storage, systemd service management, initialization, unsealing, authentication, policies, secrets engines, auditing, and a basic backup workflow.
We hope you enjoy this guide. Subscribe to our X and Facebook channels to get the latest updates and articles.
Vault is used to securely store and control access to sensitive data and to provide encryption services and dynamic, short-lived credentials.
Vault has an open-source community edition and paid editions with additional enterprise features.
Yes. TLS protects secrets in transit between clients and the Vault API/UI, and it’s considered a baseline requirement for real deployments exposed over networks.
We use cookies to run PerLod and to understand how the site is used. You can accept everything, or open Manage to turn individual categories off. Cookie policy.