Expose Ollama API Safely: Port Guide
This guide shows you how to expose the Ollama API on a network port so you can access it from other devices, while keeping it secure. You’ll learn the steps to bind Ollama to a specific interface, set up a simple firewall rule, and use an API key or reverse proxy for authentication. Before starting, you need Ollama installed and running on your machine, and you should have basic command-line knowledge.
What You’ll Need
- Ollama installed and working locally (test with
ollama list) - Administrator or sudo access on the machine running Ollama
- A firewall tool (ufw on Ubuntu, or Windows Defender Firewall on Windows)
- Optional: a reverse proxy like Nginx or Caddy for authentication
Step-by-Step
- Understand the default setup. By default, Ollama listens on
127.0.0.1:11434(localhost only). This means only your own machine can reach it. To expose it, you need to change the bind address. - Set the OLLAMA_HOST environment variable. On Linux/macOS, run
export OLLAMA_HOST=0.0.0.0:11434before starting Ollama. On Windows, useset OLLAMA_HOST=0.0.0.0:11434in Command Prompt, or set it in the system environment variables. This tells Ollama to listen on all network interfaces. - Restart Ollama. Stop the Ollama service (e.g.,
systemctl restart ollamaon Linux with systemd, or restart the Ollama app on Windows/macOS). Verify it’s now listening on all interfaces by checkingnetstat -tulpn | grep 11434(Linux) ornetstat -an | findstr 11434(Windows). You should see0.0.0.0:11434or::in the output. - Configure your firewall. Allow incoming connections on port 11434 only from trusted IPs, not from the whole internet. On Linux with ufw:
sudo ufw allow from 192.168.1.100 to any port 11434 proto tcp(replace with your client’s IP). On Windows, create an inbound rule in Windows Defender Firewall for TCP port 11434, and in the scope settings, restrict it to specific remote IP addresses. - Test locally first. From the same machine, run
curl http://localhost:11434/api/tagsto confirm the API works. Then from another device on your network, runcurl http://. If that works, you’re exposed.:11434/api/tags - Add authentication (strongly recommended). Ollama itself does not have built-in authentication. Use a reverse proxy like Nginx or Caddy to add an API key or basic auth. For example, with Caddy, you can use the
forward_authdirective or a simplebasic_authto protect the endpoint. This way, even if the port is open, only authorized users can access it. - Consider a VPN instead. If you only need access from a few devices, setting up a VPN (like WireGuard or Tailscale) is safer than exposing the port to the internet. With a VPN, you don’t need to open any ports at all.
Common Mistakes
- Binding to 0.0.0.0 without a firewall. This exposes Ollama to the entire internet, and anyone can send requests to your AI models, potentially incurring costs or misuse.
- Forgetting to restart Ollama after changing the environment variable. The change won’t take effect until restart.
- Opening the firewall to all IPs. Instead of restricting to specific IPs, some users allow all, which is risky.
- Not using authentication. Even with a firewall, if you open the port to a range of IPs, someone on the same network could access it.
Tips
- Use a specific port other than 11434 if you want to avoid default scanners. Change the
OLLAMA_HOSTto0.0.0.0:11435and adjust your firewall accordingly. - If you need to expose Ollama to the internet, use a reverse proxy with TLS (HTTPS) to encrypt traffic and add authentication.
- Monitor logs to see who is accessing the API. Ollama logs to stdout, so you can capture that with a log management tool.
- For temporary exposure, use a tool like
ngrokthat gives you a secure tunnel with an authentication token.
Troubleshooting
- Can’t access from another device. Check that the server’s IP is correct, the firewall rule is active, and Ollama is listening on 0.0.0.0. Run
curl http://localhost:11434/api/tagson the server to confirm it’s working. - Permission denied when binding to 0.0.0.0. On some systems, binding to ports below 1024 requires root, but 11434 is above that, so it should be fine. If you’re on a restricted environment, try a higher port like 18080.
- Environment variable not sticking. If you’re using a systemd service, you need to set the environment variable in the service file (e.g.,
Environment="OLLAMA_HOST=0.0.0.0:11434") and reload the daemon. - Firewall rule not working. Double-check the rule syntax and ensure the rule is enabled. On ufw, run
sudo ufw statusto see the rules. On Windows, ensure the rule is in the ‘Inbound Rules’ and is enabled.
Frequently Asked Questions
Is it safe to expose Ollama to the internet?
Not without additional security. Ollama has no built-in authentication, so anyone who can reach the port can use your models. Use a reverse proxy with an API key or a VPN.
Can I use a different port?
Yes, change the OLLAMA_HOST to 0.0.0.0:port and update your firewall rule accordingly.
How do I restrict access to specific IPs?
In your firewall rule, specify the source IP range. For example, with ufw: sudo ufw allow from 192.168.1.0/24 to any port 11434 proto tcp.
Does Ollama support HTTPS natively?
No, Ollama serves plain HTTP. Use a reverse proxy like Nginx or Caddy to add TLS/SSL.
I changed OLLAMA_HOST but it still listens on localhost. Why?
You likely didn’t restart the Ollama service. Also, ensure the environment variable is set in the same shell or in the service configuration.
Can I expose Ollama on Windows?
Yes, set the OLLAMA_HOST environment variable system-wide and configure Windows Defender Firewall to allow inbound TCP on the port.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.