Ollama 的未授权访问问题

Ollama 是一个简单易用的本地大模型运行框架,它可以让你在本地电脑上轻松运行和管理各种大语言模型。
Github 地址:https://github.com/ollama/ollama

有用户提出给 Ollama API 添加认证的需求 Requesting support for basic auth or API key authentication, Ollama 官方没有处理,所以在默认设置下 Ollama 存在未授权访问问题。

以下的代码在 Ollama v0.5.11 下测试成功。

判断是否 Ollama 运行在 11434 端口

curl -i http://127.0.0.1:11434
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Tue, 18 Feb 2025 11:12:57 GMT
Content-Length: 17

Ollama is running

列出 Ollama 提供的 LLM 模型

curl http://127.0.0.1:11434/api/tags | jq -r '.models[] | {model: .model, quant: .details.quantization_level}'

...
{
 "model": "qwen2.5:14b-instruct-q8_0-4k",
 "quant": "Q8_0"
}
{
 "model": "deepseek-coder-v2:16b-lite-instruct-q8_0",
 "quant": "Q8_0"
}
{
 "model": "qwen2.5-coder:14b-instruct-q8_0",
 "quant": "Q8_0"
}
{
 "model": "qwen2.5:7b-instruct-fp16-4k",
 "quant": "F16"
}
{
 "model": "qwen2.5:14b-instruct-fp16",
 "quant": "F16"
}
{
 "model": "qwen2.5-coder:14b-instruct-fp16",
 "quant": "F16"
}
{
 "model": "qwen2.5-coder:32b-instruct-q8_0",
 "quant": "Q8_0"
}
...

使用 Ollama 的 LLM 模型

curl http://localhost:11434/api/chat -d '{
 "model": "qwen2.5:7b-instruct-fp16",
 "messages": [
   {
     "role": "user",
     "content": "why is the sky blue?"
   }
 ],
 "stream": false
}'
{"model":"qwen2.5:7b-instruct-fp16","created_at":"2025-02-18T11:43:18.104978493Z","message":{"role":"assistant","content":"The sky appears blue because of a phenomenon called Rayleigh scattering. When sunlight
enters Earth's atmosphere, it collides with molecules and small particles in the air such as nitrogen and oxygen. Sunlight is made up of different colors, each of which has a different wavelength. Blue light wa
ves are shorter than other colors like red or yellow, and they scatter more easily when they encounter gas molecules in the atmosphere.\n\nRayleigh scattering causes blue light to scatter in all directions. Thi
s scattered blue light is what we see when we look up at the sky during clear daylight hours. The reason the sky doesn't appear white (which would be a result of scattering all colors equally) is that blue ligh
t is scattered more strongly than other colors, and because the sun's radiation has a higher proportion of blue light compared to its less scattered red wavelengths.\n\nAt sunrise or sunset, when sunlight trave
ls through a thicker layer of atmosphere, more blue and green light is scattered out of our line of sight, leaving predominantly red and orange hues to reach our eyes, which makes for beautiful and colorful sun
rises and sunsets."},"done_reason":"stop","done":true,"total_duration":3682997078,"load_duration":18632621,"prompt_eval_count":35,"prompt_eval_duration":19000000,"eval_count":220,"eval_duration":3641000000}

参考

  1. 警惕,你的ollama已经暴露!
  2. Nvidia 驱动安装和 Ollama 的使用

Ollama 的未授权访问问题
https://usmacd.com/cn/ollama_unauthenticated/
作者
henices
发布于
2025年2月20日
许可协议