Protect your AI assistant, API keys, and data with these essential security practices
Self-hosting OpenClaw gives you complete control over your AI assistant, but with great power comes great responsibility—especially when it comes to security. Unlike cloud services where security is handled for you, self-hosting means you’re responsible for protecting your API keys, securing network access, and safeguarding your data.
In this comprehensive guide, we’ll cover essential security practices for running OpenClaw safely, from API key management to network hardening. Whether you’re running OpenClaw on a home server or a cloud VPS, these practices will help you avoid the security pitfalls that have plagued other agent platforms.
Why OpenClaw Security Matters
Recent security breaches in the agent-native ecosystem (like the Moltbook incident) have highlighted critical vulnerabilities. When you self-host OpenClaw, you’re not just protecting your AI assistant—you’re protecting:
- API Keys: Access to expensive AI model APIs (Claude, GPT-4, Gemini)
- Personal Data: Chat history, preferences, and context
- Credentials: Passwords, tokens, and authentication data
- Network Access: Your server and local network resources
- Automation Capabilities: Tools and integrations that could be misused
Core Security Principles
Before diving into specific practices, understand these fundamental principles:
- Defense in Depth: Multiple layers of security, not just one
- Least Privilege: Only grant the minimum access necessary
- Regular Updates: Keep software and dependencies current
- Monitoring: Know what’s happening on your system
- Backup Security: Secure your backups as much as your live system
1. API Key Management
API keys are the most valuable target for attackers. Here’s how to protect them:
Never Commit Keys to Version Control
If you accidentally commit API keys to a public repository, they’re immediately exposed. Attackers scan GitHub constantly for exposed credentials.
# ✅ Use .gitignore
echo ".openclaw/openclaw.json" >> .gitignore
echo ".openclaw/credentials/" >> .gitignore
git add .gitignore
Use Environment Variables
# Set environment variables
export ANTHROPIC_API_KEY="sk-ant-..."
export OPENAI_API_KEY="sk-..."
Rotate Keys Regularly
Even if you haven’t detected a breach, rotate API keys periodically. Generate new keys, update OpenClaw, test, then revoke old keys.
Monitor API Usage
Set up alerts for unusual API usage. Check for unexpected spikes, requests from unfamiliar IPs, or unusual patterns.
2. Network Security
Firewall Configuration
# Allow only necessary ports
sudo ufw allow 7070/tcp # OpenClaw gateway
sudo ufw allow 22/tcp # SSH
sudo ufw enable
Use VPN for Remote Access
If you need remote access, use a VPN instead of exposing ports directly. OpenClaw supports Tailscale integration for secure mesh networking.
Gateway Authentication
{
"gateway": {
"auth": {
"mode": "token",
"token": "your-strong-random-token-here"
},
"controlUi": {
"allowInsecureAuth": false
}
}
}
Generate a strong token:
openssl rand -hex 32
Bind to Localhost When Possible
{
"gateway": {
"bind": "localhost"
}
}
3. File System Security
Restrict File Permissions
chmod 700 ~/.openclaw
chmod 600 ~/.openclaw/openclaw.json
chmod 600 ~/.openclaw/agents/*/agent/auth-profiles.json
Regular Backups (Securely)
# Create encrypted backup
tar -czf - ~/.openclaw | \
gpg --symmetric --cipher-algo AES256 \
-o openclaw-backup-$(date +%Y%m%d).tar.gz.gpg
4. Authentication and Access Control
Telegram Bot Security
{
"channels": {
"telegram": {
"dmPolicy": "pairing",
"groupPolicy": "allowlist"
}
}
}
Best Practices:
- Use
pairingfor DMs (requires explicit pairing) - Use
allowlistfor groups (only approved groups) - Never use
openpolicy in production
5. Common Security Pitfalls
❌ Don’t: Expose Gateway Without Authentication
✅ Do: Always Require Strong Authentication
❌ Don’t: Use Weak Tokens
✅ Do: Use openssl rand -hex 32
❌ Don’t: Commit Secrets to Git
✅ Do: Use .gitignore
❌ Don’t: Use Open Telegram Policies
✅ Do: Use Restrictive Policies (pairing/allowlist)
6. Incident Response Plan
If you suspect a security breach:
Immediate Actions
- Revoke API Keys: Immediately revoke all exposed API keys
- Disable Access: Shut down OpenClaw or disable network access
- Change Credentials: Rotate all passwords, tokens, and keys
- Review Logs: Check logs for unauthorized access
- Assess Damage: Determine what was accessed or compromised
Security Checklist
API Keys
- ☐ API keys stored securely (not in version control)
- ☐ Environment variables used where possible
- ☐ Keys rotated regularly
- ☐ API usage monitoring enabled
Network Security
- ☐ Firewall configured correctly
- ☐ Only necessary ports exposed
- ☐ VPN used for remote access
- ☐ Gateway requires authentication
File System
- ☐ Proper file permissions set
- ☐ Backups encrypted and stored securely
- ☐ .gitignore configured correctly
Frequently Asked Questions
How often should I rotate API keys?
Rotate API keys at least every 90 days, or immediately if you suspect they’ve been exposed.
Is it safe to expose OpenClaw to the internet?
Only if you use HTTPS with a reverse proxy, require strong authentication, keep OpenClaw updated, and monitor for suspicious activity. For most users, VPN access is safer.
What should I do if my API key is exposed?
Immediately revoke the key in your provider’s dashboard, generate a new key, update OpenClaw configuration, and review API usage logs.
Can I use OpenClaw without exposing it to the internet?
Yes! OpenClaw works perfectly on a local network via localhost, LAN IP, or VPN for secure remote access.
How do I know if my OpenClaw installation is compromised?
Signs include: unexpected API usage spikes, unfamiliar processes, unauthorized config changes, logs showing unknown IPs, or unusual agent behavior.
Conclusion
Securing your self-hosted OpenClaw installation requires attention to multiple layers: API keys, network access, file permissions, authentication, and monitoring.
Key Takeaways:
- Protect API keys like credit cards—they can cost you money
- Restrict network access—only expose what’s necessary
- Use strong authentication—weak tokens are easy targets
- Monitor regularly—catch problems before they become breaches
- Keep everything updated—security patches matter
For more on why security matters, see our article on the Moltbook security breach. And if you’re just getting started, check out our beginner’s guide to OpenClaw.
This article contains affiliate links. As an Amazon Associate, we earn from qualifying purchases at no additional cost to you.
