Guide to Opening Ports for LAN Access to Ollama
Ollama is a powerful open-source tool for running large language models that, by default, only allows local access. However, in practical applications, we might want other devices within the local network to access the Ollama service. This article will detail how to open ports on Windows, Linux, and Mac systems so that the Ollama service can be accessible on the local network.
On Windows
To allow the Ollama service to be accessible on the local network, you primarily need to complete the following two steps:
-
Set the OLLAMA_HOST Environment Variable
- The Ollama service uses the environment variable OLLAMA_HOST to specify the address it listens on. By default, it only listens to localhost, which means it can only be accessed locally.
- To allow other devices on the local network to access the Ollama service, you should set OLLAMA_HOST to 0.0.0.0. This indicates that Ollama will listen for connection attempts on all network interfaces.
- Here are the steps to set the environment variable:
- Right-click on “This PC” or “My Computer” and select “Properties.”
- Click on “Advanced system settings” on the left side.
- In the pop-up window, click on “Environment Variables.”
- In the “System variables” section, click “New.”
- In the “Variable name” input box, enter OLLAMA_HOST.
- In the “Variable value” input box, enter 0.0.0.0, then click “OK.”
- After setting it up, make sure to close and reopen the command prompt for the new environment variable to take effect.
-
Open the Port in Windows Firewall
- To enable access to the Ollama service from other devices, you need to open the port (e.g., 11434) that you’re running the Ollama service on in the Windows Firewall.
- Open the “Control Panel” and search for “Windows Defender Firewall.”
- Click on “Advanced settings” on the left to access the advanced firewall settings page.
- In the left menu, select “Inbound Rules,” then click on “New Rule” on the right.
- Choose “Port” as the rule type, then click “Next.”
- Select TCP and enter 11434 in the “Specific local ports” field, then click “Next.”
- Select “Allow the connection” and click “Next.”
- Choose the network types for the rule (Domain, Private, or Public) as needed, then click “Next.”
- Name the rule (e.g., “Allow Ollama Port 11434”), and click “Finish.”
On Linux
To make the Ollama service accessible on the local network with Linux, you mainly need to complete the following two steps:
-
Modify the Configuration File
- For Linux distributions managed by Systemd, you can edit the Ollama service configuration using the following command:
1
sudo systemctl edit ollama.service
- After executing this command, it will open the configuration file using the nano editor by default. In the opened configuration file, add the following lines at an appropriate location:
1 2
[Service] Environment="OLLAMA_HOST=0.0.0.0:11434"
- This step binds the Ollama service to port 11434 across all available network interfaces (0.0.0.0), ensuring that other devices on the local network can access it.
- For Linux distributions managed by Systemd, you can edit the Ollama service configuration using the following command:
-
Apply the Configuration and Restart the Service
- After modifying the configuration file, reload the Systemd daemon’s configuration and restart the Ollama service to make the changes effective:
1 2
sudo systemctl daemon-reload sudo systemctl restart ollama.service
- After modifying the configuration file, reload the Systemd daemon’s configuration and restart the Ollama service to make the changes effective:
-
Open Firewall Ports (if necessary)
- If your system has a firewall enabled, you’ll also need to open the corresponding TCP port for external communication. For example, using the firewalld tool, you can execute the following commands:
1 2
sudo firewall-cmd --zone=public --add-port=11434/tcp --permanent sudo firewall-cmd --reload
- If your system has a firewall enabled, you’ll also need to open the corresponding TCP port for external communication. For example, using the firewalld tool, you can execute the following commands:
On Mac
To make the Ollama service accessible on the local network with Mac, you primarily need to follow these steps:
-
Set the OLLAMA_HOST Environment Variable
- Open the Terminal application and run the following command to set the environment variable:
1
export OLLAMA_HOST=0.0.0.0:11434
- Note that this environment variable is temporary and needs to be set again each time the computer restarts. To make it permanent, you can add the above command to your
~/.zshrc
file:Add the following lines to the end of the file:1
nano ~/.zshrc
Save and exit the editor (in nano, press Ctrl+X, then Y to confirm saving).1
export OLLAMA_HOST=0.0.0.0:11434
- Open the Terminal application and run the following command to set the environment variable:
-
Test Access
- In your Mac’s browser, enter
http://127.0.0.1:11434
to test access. - In the browser of other devices on the local network, enter
http://<Mac's IP Address>:11434
to test access. For example, if the Mac’s IP address is 192.168.1.100, you would enterhttp://192.168.1.100:11434
in the browser of another device.
- In your Mac’s browser, enter
Conclusion
By following the steps outlined above, you can successfully open ports on Windows, Linux, and Mac systems, allowing the Ollama service to be accessed on the local network. This enables other devices within the network to conveniently utilize the Ollama service, facilitating a broader range of applications.