以下是在Linux中配置Llama3安全设置的方法,主要从限制访问、防火墙、认证等方面保障安全:
修改Ollama服务配置,仅允许本地访问,避免暴露在公网:
sudo vim /etc/systemd/system/ollama.service
Environment="OLLAMA_HOST=127.0.0.1:11434" # 仅允许本地访问
Environment="OLLAMA_ORIGINS=http://localhost" # 限制API跨域来源
sudo systemctl daemon-reload && sudo systemctl restart ollama
使用ufw
或iptables
限制11434端口的访问,仅允许可信IP:
# 使用ufw(推荐)
sudo ufw deny 11434 # 拒绝所有外部访问该端口
sudo ufw allow from 192.168.1.100 to any port 11434 # 允许特定IP(替换为实际IP)
sudo ufw reload
通过Nginx或API层添加用户名/密码认证:
sudo apt install nginx
/etc/nginx/sites-available/llama3
):server {
listen 80;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:11434;
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}
sudo htpasswd -c /etc/nginx/.htpasswd 用户名 # 首次创建需加-c
sudo systemctl restart nginx
在调用Llama3的API请求中添加Authorization
头,需在服务端代码中验证密钥(参考Ollama的API文档)。
ollama pull llama3
获取最新模型版本,修复潜在漏洞。cgroups
或systemd
限制Llama3的CPU/内存占用,避免资源滥用。journalctl -u ollama
或Nginx日志,检测异常访问行为。以上设置可有效降低Llama3在Linux环境中的安全风险,具体操作需根据实际部署场景调整。