In short
OpenClaw security rests on four layers: use UFW to leave only the SSH port open and keep the Gateway's port 18789 off the internet, bind the Gateway to loopback and protect it with a token, accept only approved users on each channel with dmPolicy allowlist, and store API keys in files with 600 permissions behind a spending limit. On top of that, enable only the tool permissions the assistant actually needs.
In this guide
- Threat Model: What Are We Protecting Against?
- 1. Narrow Network Access with UFW
- 2. Keep the Gateway Off the Internet and Protect It with a Token
- 3. Limit Who Can Talk to It
- 4. Store API Keys Properly
- 5. Grant Only the Tool Permissions You Need
- 6. Harden the Server Itself
- 7. Regular Audits and Log Monitoring
- Quick Security Checklist
- Next Step: Start with the Firewall Already in Place
OpenClaw security matters more than securing an ordinary web app, because OpenClaw doesn't just display data; it's an assistant that takes actions on your behalf. It uses your paid API keys, is connected to your messaging accounts and, if you allow it, can run commands on the server. In a misconfigured setup, a stranger can message your bot and drain your API balance or get it to do things on your server. This guide walks through the settings that lock OpenClaw down against abuse, in order, from the network layer all the way to API keys.
Threat Model: What Are We Protecting Against?
- Exposed Gateway: If the control interface (default port
18789) is exposed to the internet and the token is weak or missing, an outsider can take control of the assistant. - Open message policy: If the bot processes messages from anyone, anyone who finds the bot's username can call the model with your API key.
- Key leaks: API keys and bot tokens can leak through files with the wrong permissions, screenshots or Git repositories.
- Prompt injection: A web page, email or document the assistant reads can try to give the model hidden instructions and get it to call an unwanted tool. This risk never fully goes away, but limiting tool permissions shrinks its impact.
1. Narrow Network Access with UFW
The firewall is your first line of defense. The rule is simple: only SSH should be reachable from outside, and the Gateway port must never be opened. If you're configuring UFW for the first time, add the SSH rule before enabling it, or you'll lose your connection:
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw enable
ufw status verbose
If you use a different port for SSH, use it instead of 22. The Telegram and WhatsApp channels open outbound connections from the server, so you don't need to open any extra inbound ports. If you opened the Gateway port by mistake earlier, remove it:
ufw status numbered
ufw delete allow 18789/tcp
For detailed UFW usage, see the guide to setting up a Linux firewall with UFW.
2. Keep the Gateway Off the Internet and Protect It with a Token
A firewall alone isn't enough; one day a rule might change by accident. That's why the Gateway itself should listen only on the local address. First check the current state:
ss -tlnp | grep 18789
If the output shows 127.0.0.1:18789, you're good. If you see 0.0.0.0:18789 or *:18789, the Gateway is listening on every network interface. Configure the Gateway section in ~/.openclaw/openclaw.json along these lines:
"gateway": {
"bind": "loopback",
"auth": {
"mode": "token",
"token": "UZUN-RASTGELE-BIR-DEGER"
}
}
Replace the token placeholder with a long random value; you can generate one with openssl rand -hex 32. Field names can change between versions, so check the docs for your version and the official OpenClaw documentation. After the change, run openclaw gateway restart and verify again with the ss command.
Remote management: a tunnel instead of an open port
When you need to reach the control panel from your own computer, don't open the port; use an SSH tunnel:
ssh -N -L 18789:127.0.0.1:18789 kullanici@SUNUCU_IP
The tunnel is available only to someone with your SSH key; the Gateway is never visible to the internet. Publishing the panel on a domain behind a reverse proxy is technically possible, but it expands the attack surface; for a personal assistant, a tunnel is both simpler and safer.
3. Limit Who Can Talk to It
Messaging channels are the assistant's most abused entry point. Choose the dmPolicy value for each channel deliberately:
| Policy | Behavior | When to use it |
|---|---|---|
allowlist | Only messages from IDs in the allowFrom list are processed | Personal assistant, small fixed team. The safest option. |
pairing | A new sender gets a pairing code, and their messages aren't processed until you approve them on the server | When your user list grows from time to time |
open | Everyone's messages are processed | A public bot; not recommended unless tool permissions are very restricted |
Sample Telegram configuration for personal use:
"telegram": {
"enabled": true,
"botToken": "...",
"dmPolicy": "allowlist",
"allowFrom": ["111222333"]
}
On Telegram, it's safer to put the numeric user ID in allowFrom rather than a username, because usernames can be changed. On WhatsApp, enter the number in international format with the country code (for example, +90 for Türkiye). If you'll add the bot to a group, turn on the settings that make the assistant reply only when it's mentioned, and add the group to the allowlist too. Review approved pairings regularly:
openclaw pairing list telegram
4. Store API Keys Properly
A model API key is money, plain and simple. Apply these rules:
- Tighten file permissions: Only the user running OpenClaw should be able to read the configuration folder.
chmod 700 ~/.openclaw chmod 600 ~/.openclaw/openclaw.json ls -la ~/.openclaw - Set a spending limit: Set a monthly budget and an alert threshold in the model provider's console. Even if the key leaks, the damage stays capped at that limit.
- Use a dedicated key: Create an API key just for OpenClaw. Don't share it with your other projects, so you can revoke it on its own when needed.
- Keep it out of Git and screenshots: Never push the
~/.openclawfolder to a repository, and blur the token fields before sharing a screenshot of the configuration. - Act fast on a leak: At the first suspicion, revoke the key in the provider's console and create a new one; for the Telegram bot token, use the
/revokecommand in BotFather and put the new token in the configuration.
5. Grant Only the Tool Permissions You Need
The assistant's ability to run shell commands, write files or use a browser is where prompt injection can turn into real damage. A practical approach:
- Enable the command execution tool only if you truly need it; where possible, use approval-required modes or sandbox options.
- Restrict the assistant's workspace to a separate directory; don't give it access to system files or directories belonging to other applications.
- Don't install third-party skills without reviewing their source code. A skill runs with all of the assistant's permissions.
- Running OpenClaw as a separate, unprivileged Linux user instead of root shrinks the blast radius even further.
OpenClaw's built-in security audit lists common configuration mistakes. If your version supports it, run it after every change:
openclaw security audit --deep
openclaw doctor
6. Harden the Server Itself
No matter how well the assistant is configured, every key is exposed on a server compromised through SSH. Treat these three steps as the baseline:
- SSH key login with password login disabled: Follow the guide to creating an SSH key and disabling password login.
- Fail2ban: Automatically blocks brute-force attempts.
- Regular updates: Keep system packages current with
apt update && apt upgrade, and keep OpenClaw itself updated after reading the release notes.
7. Regular Audits and Log Monitoring
Security is a routine, not a one-time setting. Running these checks once a month helps you catch problems before they grow:
- Review the logs: In
openclaw logs --followor the service logs, look for message attempts from unknown senders, rejected pairing requests and unexpected tool calls. - Check model usage: A sudden spike in tokens on the provider dashboard that doesn't match your own usage can be the first sign the key is being used elsewhere.
- Check SSH logins: The output of
last -a | headandjournalctl -u ssh --since "7 days ago"should show no successful logins from IP addresses you don't recognize. - Clean up the allowlist: Remove people who no longer use the assistant from the
allowFromlist and from approved pairings. - Back up the configuration: Keep an encrypted backup of the
~/.openclawfolder off the server, so after an incident you can rebuild the server from scratch and be back up in minutes.
Quick Security Checklist
- Only the SSH port is open in the
ufw statusoutput. - The Gateway is listening on
127.0.0.1in thess -tlnpoutput. - The Gateway token is long and random.
dmPolicyis set toallowlistorpairingon every channel.~/.openclawpermissions are 700 and the configuration file is 600.- A spending limit is set at the model provider.
- SSH password login is disabled.
Next Step: Start with the Firewall Already in Place
Building on a ready-made base instead of setting up the firewall, the first of these layers, from scratch saves time and reduces the risk of mistakes. Sunucu Atlas OpenClaw server plans are delivered in Istanbul with the UFW firewall enabled and OpenClaw and Node.js 24 preinstalled; with root access, you can apply every hardening step in this guide yourself. To isolate your assistant from your personal computer, SA-OPENCLAW-1 is a good starting point for single-user setups, and SA-OPENCLAW-2 suits setups that use the browser tool and multiple channels. If you haven't installed it yet, follow the OpenClaw setup and Telegram connection guide first, then come back to this list.
Frequently asked questions
Should I open the OpenClaw Gateway port in the firewall?
No. The Gateway's port 18789 should listen only on the local address and should not be opened in UFW. Use an SSH tunnel to reach the control panel remotely.
What happens if someone else messages my OpenClaw bot?
With dmPolicy allowlist, the message isn't processed; with pairing, it waits for your approval. With dmPolicy open, the message goes to the model and uses your API balance, which is why open isn't recommended for personal use.
What should I do if my OpenClaw API key leaks?
Revoke the key in the model provider's console right away, create a new one and put it in the configuration. If the Telegram bot token leaked too, regenerate it in BotFather with /revoke and restart the Gateway.
Why does prompt injection matter for OpenClaw?
A page or document the assistant reads can try to give the model hidden instructions and make it call tools. Limiting command execution and file-writing permissions minimizes the impact of this attack.
Is it a bad idea to run OpenClaw as root?
An assistant running as root gets access to the entire server if it's abused. Running it as a separate, unprivileged Linux user limits any damage to that user's directories.