Secrets Management — Protecting Sensitive Information in Linux¶
Secrets Management is the practice of securely storing, accessing, rotating, and protecting sensitive information such as passwords, API keys, SSH keys, database credentials, encryption keys, and certificates. Poor secrets management is one of the most common causes of security breaches. Hardcoding secrets into scripts, configuration files, or source code can expose critical infrastructure to attackers. Every Linux administrator, DevOps engineer, Cloud Architect, Platform Engineer, and Site Reliability Engineer (SRE) should understand how to manage secrets securely in production environments.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand secrets management
- Identify different types of secrets
- Avoid common security mistakes
- Secure secrets in Linux
- Use environment variables safely
- Learn enterprise secret management solutions
- Rotate secrets securely
- Apply production best practices
Prerequisites¶
Complete:
- Modules 1–10
- Module 11 Lessons 1–8
Why Learn Secrets Management?¶
Imagine a deployment script.
Poor practice:
Anyone who can read the script now has the password.
Secure approach:
Secrets remain protected throughout their lifecycle.
What is a Secret?¶
A secret is any sensitive information used for authentication, authorization, or encryption.
Examples include:
- Passwords
- API keys
- SSH private keys
- Database credentials
- Cloud access keys
- OAuth tokens
- TLS certificates
- Encryption keys
Why Secrets Must Be Protected¶
If secrets are exposed, attackers may gain:
- Server access
- Database access
- Cloud account access
- Application control
- Sensitive customer data
- Internal infrastructure access
Common Secrets¶
| Secret | Example |
|---|---|
| Password | Linux login |
| API Key | Cloud API |
| SSH Key | Remote access |
| Token | GitHub Personal Access Token |
| Certificate | TLS/SSL |
| Database Password | PostgreSQL login |
Common Security Mistakes¶
Avoid:
Hardcoding passwords.
Storing secrets in:
- Git repositories
- Source code
- Public cloud storage
- Shared documents
- Chat messages
Environment Variables¶
A safer alternative for many applications.
Example:
Read:
Applications can access the variable without embedding it directly in source code.
Note
Environment variables are more secure than hardcoding credentials, but they are not a complete secrets management solution because privileged users or processes may still be able to access them.
Using a .env File¶
Example:
Load:
Best practice:
Never commit .env files containing secrets to version control.
File Permissions for Secrets¶
Restrict access.
Example:
Owner only.
SSH Private Keys¶
Secure permissions.
Directory:
Secret Rotation¶
Secrets should be changed regularly.
Example:
Benefits:
- Limits exposure
- Reduces long-term risk
- Supports compliance
Enterprise Secret Managers¶
Common solutions:
- HashiCorp Vault
- AWS Secrets Manager
- Azure Key Vault
- Google Secret Manager
- Kubernetes Secrets (when combined with encryption and RBAC)
These systems provide:
- Encryption
- Access control
- Auditing
- Automatic rotation
- Centralized management
Access Control¶
Apply the principle of least privilege.
Only authorized users and applications should access secrets.
Example:
Encryption¶
Secrets should be encrypted:
- At rest
- In transit
Use:
- TLS
- Disk encryption
- Secret management platforms
- Encrypted backups
Auditing Secret Access¶
Monitor:
- Who accessed a secret
- When it was accessed
- Which application requested it
- Failed access attempts
Audit logs help detect misuse and support compliance.
Kubernetes Secrets¶
Kubernetes provides:
resource objects.
Example:
Remember:
- Base64 encoding is not encryption.
- Enable encryption at rest.
- Use RBAC to restrict access.
- Consider integrating with an external secrets manager for production workloads.
Common Commands¶
Create environment variable.
Display variable.
Secure file.
List secrets.
Real Production Examples¶
Secure SSH key.
Secure configuration.
Read environment variable.
List Kubernetes secrets.
Production Perspective¶
Secrets management is critical for:
- Cloud platforms
- Kubernetes
- CI/CD pipelines
- Databases
- Linux servers
- Web applications
- Enterprise infrastructure
- DevOps automation
Poor secrets management is one of the leading causes of cloud security incidents.
Hands-on Lab¶
Task 1¶
Create an environment variable.
Task 2¶
Display the variable.
Task 3¶
Create a .env file.
Task 4¶
Protect the file.
Task 5¶
Load the file.
Task 6¶
Check SSH key permissions.
Task 7¶
List Kubernetes secrets.
(Requires access to a Kubernetes cluster.)
Task 8¶
Review your scripts and ensure that no passwords or API keys are hardcoded.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
export | Create environment variable | Application configuration |
source | Load environment variables | Deployment scripts |
chmod 600 | Protect secret files | Configuration security |
kubectl get secrets | List Kubernetes secrets | Cluster administration |
ls -l ~/.ssh | Verify SSH key permissions | SSH security |
printenv | View environment variables | Troubleshooting |
Common Secrets Management Mistakes¶
| Mistake | Solution |
|---|---|
| Hardcoding passwords | Use a secrets manager or secure configuration |
| Committing secrets to Git | Use .gitignore and secret scanning |
| Weak file permissions | Restrict access with chmod 600 |
| Never rotating secrets | Rotate credentials regularly |
| Sharing secrets through email or chat | Use secure secret-sharing methods |
Production Troubleshooting Scenario¶
Scenario
A developer accidentally commits AWS credentials to a Git repository.
Result:
Correct response:
- Revoke the exposed credentials immediately.
- Generate new credentials.
- Update applications with the new secrets.
- Remove the secret from the repository history if appropriate.
- Enable secret scanning and review audit logs.
- Move the credentials to a dedicated secrets management solution.
Best Practices¶
- Never hardcode secrets in scripts or source code.
- Use dedicated secrets management platforms for production.
- Encrypt secrets at rest and in transit.
- Rotate credentials regularly.
- Apply the principle of least privilege.
- Audit access to sensitive credentials.
- Protect secret files with restrictive permissions.
- Enable secret scanning in CI/CD pipelines and version control workflows.
Common Mistakes¶
❌ Hardcoding passwords in scripts.
✅ Avoid this mistake: hardcoding passwords in scripts.
❌ Uploading secrets to Git repositories.
✅ Avoid this mistake: uploading secrets to Git repositories.
❌ Sharing credentials through insecure communication channels.
✅ Avoid this mistake: sharing credentials through insecure communication channels.
❌ Never rotating API keys or passwords.
✅ Always rotating API keys or passwords.
❌ Granting unnecessary access to sensitive credentials.
✅ Avoid this mistake: granting unnecessary access to sensitive credentials.
Interview Questions¶
Beginner¶
- What is a secret?
- Why should secrets never be hardcoded?
- What file permissions should a
.envfile have? - What is an environment variable?
Intermediate¶
- Why are environment variables not a complete secrets management solution?
- What are the benefits of secret rotation?
- How do enterprise secrets managers improve security?
- Why should SSH private keys have
600permissions?
Architect Level¶
- How would you design secrets management for a Kubernetes platform?
- How would you securely manage secrets across multiple cloud providers?
- How would you prevent secret leakage in CI/CD pipelines?
Summary¶
In this lesson, you learned:
- Secrets management fundamentals
- Types of secrets
- Environment variables
.envfiles- Secret rotation
- Enterprise secrets managers
- Kubernetes secrets
- Production security best practices
Secrets management is a critical part of modern infrastructure security. Properly storing, protecting, rotating, and auditing secrets reduces the risk of credential compromise and strengthens the overall security posture of Linux systems, cloud platforms, and enterprise applications.
Key Takeaways¶
- Never hardcode secrets in source code or scripts.
- Restrict access to secret files using appropriate permissions.
- Rotate credentials regularly.
- Use dedicated secrets management platforms in production.
- Encrypt secrets at rest and in transit.
- Audit access to sensitive credentials and follow the principle of least privilege.
What's Next?¶
CIS Benchmark Basics — Hardening Linux Systems Using Security Standards
You'll explore:
- What CIS Benchmarks are
- Why secure configuration baselines matter
- CIS benchmark categories
- Performing basic compliance checks
- Hardening Linux systems
- Security scoring
- Production compliance best practices
By the end of the lesson, you'll understand how CIS Benchmarks help standardize Linux system hardening, improve security posture, and support regulatory and organizational compliance.