Managing Secrets on Linux Hosts
Why Secrets on Host Are a High-Risk Area
API keys, database passwords, cloud credentials, TLS private keys — once a host is compromised, attackers look for secrets first. Poor secret handling turns a single host breach into full infrastructure takeover.
Symptom
- Plaintext passwords inside config files
- .env files readable by all users
- Credentials committed to backup archives
- Environment variables leaking through process inspection
- Application running as root exposing private keys
Root Cause
- No centralized secret management
- Weak file permission discipline
- Service accounts over-privileged
- Secrets stored in home directories
- No separation between runtime user and admin user
Investigation
Find world-readable sensitive files:
sudo find / -type f -perm -004 -name "*.env" 2>/dev/null
Check permissions of critical directories:
ls -ld /etc /var/www /home
Inspect application config permissions:
ls -l /var/www/app/.env
Check process environment variables (current user):
cat /proc/$(pgrep -f app_process_name)/environ | tr " " " "
List private keys on system:
sudo find / -type f -name "id_rsa" 2>/dev/null
Mitigation
1) Restrict File Permissions
Secrets must not be world-readable.
chmod 600 /var/www/app/.env chown appuser:appuser /var/www/app/.env
Ensure .ssh directory is restricted:
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
2) Run Applications as Dedicated Users
Never run application processes as root.
ps aux | grep app_process_name
If running as root, fix service configuration (systemd example):
User=appuser Group=appuser
3) Avoid Storing Secrets in User Home Directories
Use restricted system paths such as:
- /etc/appname/
- /run/secrets/
4) Protect TLS Private Keys
ls -l /etc/ssl/private
Keys should be owned by root and readable only by required services.
5) Reduce Environment Variable Exposure
Environment variables are visible to processes with sufficient privileges.
Avoid exporting secrets globally in:
- /etc/profile
- ~/.bashrc
- system-wide environment files
Hardening Strategy
- Principle of minimum exposure: only the running service should read its secrets
- Separate runtime identity: isolate service accounts
- Rotate credentials regularly
- Use external secret managers (cloud-native or vault systems)
- Audit file permissions regularly
- Prevent backups from including plaintext secrets unintentionally
Common Escalation Pattern
- Attacker gains low-privilege shell
- Searches for readable .env files
- Extracts database password
- Connects to database and dumps user data
- Uses API keys to access cloud infrastructure
Secret hygiene determines whether compromise stays local or spreads.
Verification Checklist
sudo find /etc /var/www -type f -perm -004 2>/dev/null ps aux ls -l /etc/ssl/private
- No world-readable secret files
- No services running as root unnecessarily
- Secret files owned by correct service account
- No plaintext secrets in home directories
Why This Matters in Real Infrastructure
Secrets are the bridge between one machine and the rest of your infrastructure. A single exposed credential often leads to database compromise, cloud account takeover, or lateral movement. Production systems assume compromise can happen — secret management ensures the damage stops at one boundary.