Linux
作者: Jakub Rusinowski · 最后更新: 2026年9月1日
AI 教育者、本地 LLM 部署工作坊讲师
$ export OLLAMA_MODELS=/mnt/data/models
$ ollama pull llama3.1:8b
# ... still writes to /usr/share/ollama/.ollama/models
| 如果你看到 | 原因是 | 前往 |
|---|---|---|
You exported the variable in your shell and nothing changed | The systemd service never sees your shell environment | Fix 1: set it with systemctl edit |
ollama serve run by hand honours it, the service does not | The manual run inherits your shell; the service does not | Fix 1 — this contrast is the whole confusion |
| You set OLLAMA_MODELS and Ollama cannot write there | The path is not owned by the ollama service user | Fix 3: chown the directory |
| Pulls fail behind a corporate proxy | HTTPS_PROXY is the variable Ollama reads, not HTTP_PROXY | Fix 4: the variable table |
| You edited the unit file and an upgrade reverted it | Package upgrades overwrite /etc/systemd/system/ollama.service | Fix 2: use a drop-in, never edit the unit |
You set the variable exactly as the documentation for another platform described, restarted, and Ollama carried on as if you had done nothing. There is no error because, from Ollama's point of view, nothing was configured.
.bashrc or .profile and rebooted, with no effect.ollama serve yourself and stops working after systemctl restart ollama.http_proxy being set.On Linux the installer sets Ollama up as a systemd service running as its own ollama user, started by the init system at boot. A systemd service does not inherit your login shell's environment — it gets whatever the unit file gives it and nothing else. So export OLLAMA_HOST=... configures the shell you typed it into, and the service, which is a different process owned by a different user started before you logged in, never sees it. This also explains the detail that makes people doubt their own eyes: ollama serve typed into your terminal does read your shell environment, because that process really is your child. Same binary, same variable, two different answers depending on who started it.
systemctl edit ollama.service opens a drop-in override in your editor. Add the variables under a [Service] section, save, reload the daemon and restart. This is the documented mechanism and it survives package upgrades, because the drop-in lives in a separate file from the unit itself.
# bash
sudo systemctl edit ollama.service
# Add, between the marked lines:
# [Service]
# Environment="OLLAMA_HOST=0.0.0.0:11434"
# Environment="OLLAMA_MODELS=/mnt/data/ollama-models"
sudo systemctl daemon-reload
sudo systemctl restart ollamasystemctl show ollama --property=Environment
journalctl -u ollama --no-pager -n 20It is tempting to open /etc/systemd/system/ollama.service and add the line there. Do not: a package upgrade replaces that file and your configuration disappears at the least convenient moment, usually weeks later when nobody connects the two events. systemctl edit writes to /etc/systemd/system/ollama.service.d/override.conf, which upgrades leave alone. You can inspect and version-control that file directly.
# bash — where the override actually lives
sudo cat /etc/systemd/system/ollama.service.d/override.conf
systemctl cat ollama # unit + every drop-in, in load ordersystemctl cat ollama shows your override listed after the base unit, which is what makes it win.systemctl cat ollama | tail -20OLLAMA_MODELS is the variable people hit permission trouble with. The default on Linux is /usr/share/ollama/.ollama/models, owned by the service user. Point the service at a directory your own account owns and it cannot write there — the service is not you. Create the directory, hand it to ollama:ollama, and only then restart.
⚠️ Run chown against the model directory only. A recursive chown aimed a level too high — at /mnt or a whole data volume — hands unrelated files to the service user and is tedious to undo.
# bash
sudo mkdir -p /mnt/data/ollama-models
sudo chown -R ollama:ollama /mnt/data/ollama-models
sudo systemctl restart ollama
ollama listollama pull gemma3:1b
sudo ls -l /mnt/data/ollama-models/blobs | tail -3Most "Ollama ignores my config" reports are one of these set in the wrong place. OLLAMA_HOST (default 127.0.0.1:11434) is the bind address. OLLAMA_MODELS (default /usr/share/ollama/.ollama/models on Linux) is the store. OLLAMA_CONTEXT_LENGTH defaults to 4096, which is why long conversations truncate on a machine with memory to spare. OLLAMA_KEEP_ALIVE defaults to 5m before a model unloads. OLLAMA_MAX_LOADED_MODELS defaults to 3× your GPU count, or 3 on CPU, and OLLAMA_NUM_PARALLEL to 1 request per model. OLLAMA_DEBUG turns on verbose logging, OLLAMA_VULKAN=0 disables the Vulkan backend, and OLLAMA_TMPDIR matters on hardened servers where /tmp is mounted noexec — a real failure mode, since Ollama needs to execute from its temporary directory. For proxies the variable is HTTPS_PROXY, not HTTP_PROXY; the model registry is HTTPS and the lowercase HTTP form is simply not read.
# bash — a typical server drop-in
sudo systemctl edit ollama.service
# [Service]
# Environment="OLLAMA_HOST=0.0.0.0:11434"
# Environment="OLLAMA_CONTEXT_LENGTH=8192"
# Environment="OLLAMA_KEEP_ALIVE=30m"
# Environment="OLLAMA_NUM_PARALLEL=2"
# Environment="HTTPS_PROXY=http://proxy.internal:3128"
# Environment="OLLAMA_TMPDIR=/var/tmp/ollama"systemctl show ollama --property=Environment | tr ' ' '\n'Readers arrive here having followed instructions for a different operating system, and the three genuinely differ. Linux: systemctl edit ollama.service, a drop-in, then daemon-reload and restart. macOS: launchctl setenv OLLAMA_HOST "0.0.0.0:11434", then quit and reopen the Ollama app. Windows: Settings → environment variables for your account, then quit from the system tray and relaunch. Each is documented, none transfers, and all three share one requirement — the application must be fully restarted, not merely closed.
# macOS (zsh) — for reference; does not apply on Linux
launchctl setenv OLLAMA_HOST "0.0.0.0:11434"
# then quit and reopen the Ollama app
# Windows (PowerShell) — for reference
# [Environment]::SetEnvironmentVariable("OLLAMA_HOST", "0.0.0.0:11434", "User")
# then quit Ollama from the system tray and relaunchOLLAMA_CONTEXT_LENGTH deserves a moment on its own because its default of 4096 is the quiet cause of a lot of "the model forgot what I said". Raising it is free in configuration and expensive in memory: the KV cache grows with context, and on a card that was already close to full, doubling the window is what tips you into offload. Work out the memory cost for your model and card before you raise it, rather than raising it and wondering why throughput collapsed.
# bash — what is the service actually using?
systemctl show ollama --property=Environment | grep -o 'OLLAMA_CONTEXT_LENGTH=[0-9]*'
ollama ps # Processor column shows whether you are still fully on the GPUIf the variables are landing and the API still is not reachable from another machine, binding is only half the story and the firewall is the other half. If you are configuring a server rather than a workstation, the headless guide covers the operational pieces — the disk filling, keeping models warm, and exposing the API safely.
上报问题时请附上这些信息
systemctl show ollama --property=Environmentsystemctl cat ollama including every drop-injournalctl -u ollama --no-pagerBecause on Linux Ollama runs as a systemd service under its own user, started before you logged in. Services do not inherit a login shell's environment. Your export configures your shell; the service reads only what its unit file and drop-ins provide.
Because that process is a child of your shell and inherits its environment, while the service is not. Same binary, two different environments. Naming that difference usually resolves the confusion faster than any command.
No. Package upgrades replace that file and your settings vanish without warning. Use systemctl edit ollama.service, which writes a drop-in under ollama.service.d/ that upgrades leave alone. systemctl cat ollama shows the merged result.
Because the service runs as the ollama user and the directory is owned by you. Create it and sudo chown -R ollama:ollama the path, then restart the service. Keep the chown scoped to the model directory itself.
Ollama reads HTTPS_PROXY, not HTTP_PROXY, because the model registry is served over HTTPS. Set HTTPS_PROXY in the service drop-in. Behind a TLS-inspecting proxy you also need its certificate installed as a system certificate.