Appearance
Grafana DEB 包安装指南 | 兼容 Ubuntu/Debian

前言
Grafana 作为最流行的开源数据可视化平台,在监控领域有着广泛的应用。在 Ubuntu/Debian 系统上,除了使用 APT 包管理器外,DEB 包安装是最直接、最可控的方式,尤其适合以下场景:
- 开发测试环境:快速验证 Grafana 功能
- 单机监控:只需要监控几台物理机或虚拟机,搭建轻量级监控平台
- 版本锁定:需要精确控制 Grafana 版本,避免 APT 自动升级
- 离线环境:内网服务器无法访问 APT 仓库
本文详细记录在 Ubuntu 22.04 系统上通过 DEB 包安装 Grafana OSS 开源版的完整流程,从下载、安装、配置、防火墙开放到服务管理,涵盖所有关键步骤和排障经验。
💡 版本选择提示:
Grafana 提供 OSS(开源版) 和 Enterprise(企业版) 两个版本。本文使用 OSS 开源版进行演示,它包含了 Grafana 的核心可视化、告警和数据源等全部基础功能,完全满足个人开发、测试及大部分生产场景。Enterprise 企业版则额外提供报表、RBAC、SAML 等高级功能,适用于有企业级需求的环境。两者安装流程完全一致,您可以根据自身需要替换下载链接。
无论您是运维工程师、开发人员还是 Homelab 爱好者,都可以通过本文快速搭建一个可用的 Grafana 监控平台。
环境说明
| 项目 | 信息 |
|---|---|
| 操作系统 | Ubuntu 22.04 LTS+ / Debian 11+ |
| 架构 | x86_64 (amd64) |
| Grafana 版本 | 13.2.0 |
| 安装方式 | DEB 包 |
| 服务管理 | systemd |
第一部分:下载 Grafana DEB 包
1.1 从 GitHub Releases 下载
bash
# 进入临时目录
cd /tmp
# 下载 Grafana OSS 开源版 DEB 包
wget https://github.com/grafana/grafana/releases/download/v13.2.0/grafana_13.2.0_32077357341_linux_amd64.deb1.2 从 APT 仓库安装(备选)
如果网络允许,可以直接从 Grafana 官方 APT 仓库安装:
bash
# 添加 Grafana 官方 APT 仓库
sudo apt-get install -y software-properties-common wget
sudo wget -q -O /usr/share/keyrings/grafana.key https://apt.grafana.com/gpg.key
echo "deb [signed-by=/usr/share/keyrings/grafana.key] https://apt.grafana.com stable main" | sudo tee /etc/apt/sources.list.d/grafana.list
# 更新并安装
sudo apt-get update
sudo apt-get install grafana -y1.3 版本选择说明
| 版本类型 | 适用场景 | 说明 |
|---|---|---|
| OSS(开源版) | 开发测试、个人 Homelab | 完全开源,功能足够大多数场景,无需 License |
| Enterprise(企业版) | 企业生产环境 | 包含高级商业功能,需申请 License |
第二部分:安装 DEB 包
2.1 首次安装
bash
# 使用 dpkg 安装 DEB 包
sudo dpkg -i grafana_13.2.0_linux_amd64.deb预期输出:
Selecting previously unselected package grafana.
(Reading database ... 123456 files and directories currently installed.)
Preparing to unpack grafana_13.2.0_linux_amd64.deb ...
Unpacking grafana (13.2.0) ...
Setting up grafana (13.2.0) ...
Created symlink /etc/systemd/system/multi-user.target.wants/grafana-server.service → /usr/lib/systemd/system/grafana-server.service.2.2 处理依赖问题
如果安装过程中提示依赖缺失:
bash
# 修复依赖
sudo apt-get install -f2.3 升级安装
如果系统已安装旧版本 Grafana:
bash
# 使用 dpkg -i 覆盖安装(会自动升级)
sudo dpkg -i grafana_13.2.0_linux_amd64.deb2.4 验证安装
bash
# 查看安装的文件列表
dpkg -L grafana
# 查看版本
/usr/sbin/grafana-server -v
# 输出:Version 13.2.0 (commit: f681b1359f, branch: HEAD)
# 查看 Grafana CLI 版本
/usr/sbin/grafana-cli -v第三部分:配置 Grafana
3.1 默认配置文件位置
| 文件 | 路径 | 说明 |
|---|---|---|
| 主配置文件 | /etc/grafana/grafana.ini | Grafana 核心配置 |
| Systemd 服务 | /usr/lib/systemd/system/grafana-server.service | 服务管理 |
| 数据目录 | /var/lib/grafana | 存储数据(默认) |
| 日志目录 | /var/log/grafana | 日志文件 |
| 插件目录 | /var/lib/grafana/plugins | 插件存放位置 |
| 默认配置文件 | /usr/share/grafana/conf/defaults.ini | 默认配置参考 |
3.2 修改配置文件
bash
# 备份原配置
sudo cp /etc/grafana/grafana.ini /etc/grafana/grafana.ini.bak
# 编辑配置
sudo vim /etc/grafana/grafana.ini关键配置项:
ini
[server]
# Protocol (http, https, h2, socket, socket_h2)
protocol = http
# Minimum TLS version allowed. By default, this value is empty. Accepted values are: TLS1.2, TLS1.3. If nothing is set TLS1.2 would be taken
;min_tls_version = ""
# The ip address to bind to, empty will bind to all interfaces
http_addr =
# The http port to use
http_port = 3000
# The public facing domain name used to access grafana from a browser
domain = 192.168.0.2313.3 修改数据目录权限
bash
# DEB 包安装会自动创建 grafana 用户,确保目录权限正确
sudo chown -R grafana:grafana /var/lib/grafana
sudo chown -R grafana:grafana /var/log/grafana第四部分:启动 Grafana 服务
4.1 使用 systemd 管理
bash
# 重新加载 systemd 配置
sudo systemctl daemon-reload
# 启动服务
sudo systemctl start grafana-server
# 设置开机自启
sudo systemctl enable grafana-server
# 查看服务状态
sudo systemctl status grafana-server4.2 验证服务正常运行
bash
# 查看端口监听
sudo netstat -lntp | grep 3000
# 预期输出:
# tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN 12345/grafana-server
# 或使用 ss 命令
ss -lntp | grep 3000
# 测试本地访问
curl http://192.168.0.231:3000 | head -20
# 查看服务日志
sudo journalctl -u grafana-server -f第五部分:开放防火墙
这是最容易忽略的一步,也是导致外部无法访问的主要原因。
5.1 使用 ufw(Ubuntu 默认)
bash
# 查看防火墙状态
sudo ufw status
# 开放 3000 端口
sudo ufw allow 3000/tcp
# 重新加载防火墙
sudo ufw reload
# 验证规则
sudo ufw status | grep 30005.2 使用 iptables
bash
# 添加规则
sudo iptables -A INPUT -p tcp --dport 3000 -j ACCEPT
# 保存规则(Ubuntu)
sudo apt-get install iptables-persistent -y
sudo netfilter-persistent save5.3 关闭防火墙(仅测试环境)
bash
# 临时关闭
sudo ufw disable
# 永久关闭(不推荐生产环境)
sudo ufw disable第六部分:SELinux/AppArmor 配置
6.1 Ubuntu 使用 AppArmor
Ubuntu 默认使用 AppArmor 而非 SELinux,通常不会阻止 Grafana。如果遇到权限问题:
bash
# 检查 AppArmor 状态
sudo aa-status
# 查看 Grafana 相关日志
sudo journalctl -u grafana-server | grep -i apparmor6.2 如果 AppArmor 阻止了 Grafana
bash
# 临时禁用 Grafana 的 AppArmor 配置文件(不推荐)
sudo aa-disable /etc/apparmor.d/usr.sbin.grafana-server
# 重启服务
sudo systemctl restart grafana-server第七部分:访问 Grafana
7.1 Web 登录
bash
# 浏览器访问
http://<服务器IP>:3000
# 默认凭证
用户名: admin
密码: admin7.2 首次登录
首次登录会强制要求修改密码,建议设置强密码。
7.3 验证 API
bash
# 检查 Grafana 健康状态
curl http://localhost:3000/api/health
# 预期返回:
# {
# "database": "ok",
# "version": "13.2.0",
# "commit": "f681b1359f6a0b8ecb9f2c49a88ac72b75bde73b"
# }第八部分:常见问题与排障
Q1:dpkg -i 报依赖缺失
dpkg: dependency problems prevent configuration of grafana:
grafana depends on libc6 (>= 2.34); however:解决方案:
bash
# 修复依赖
sudo apt-get install -fQ2:服务启动失败,端口被占用
bash
# 检查端口占用
sudo lsof -i:3000
sudo netstat -tlnp | grep 3000
# 杀死占用进程
sudo kill -9 <PID>
sudo systemctl start grafana-serverQ3:外部无法访问(Connection refused)
检查顺序:
bash
# 1. 服务是否运行
sudo systemctl status grafana-server
# 2. 端口是否监听
sudo netstat -lntp | grep 3000
# 3. 防火墙是否开放
sudo ufw status
# 4. 配置是否绑定 0.0.0.0
cat /etc/grafana/grafana.ini | grep http_addr
# 5. 云服务商安全组(如 AWS、阿里云)
# 需要在控制台开放 3000 端口Q4:忘记 admin 密码
bash
# 重置密码为 admin
sudo /usr/sbin/grafana-cli admin reset-admin-password admin
# 重启服务
sudo systemctl restart grafana-serverQ5:DEB 包与 APT 版本冲突
如果同时存在 APT 安装的版本和手动安装的 DEB 包:
bash
# 查看已安装版本
dpkg -l | grep grafana
# 彻底卸载
sudo dpkg -r grafana
sudo apt-get autoremove -y
# 重新安装 DEB 包
sudo dpkg -i grafana_13.2.0_linux_amd64.deb第九部分:常用管理命令速查
| 操作 | 命令 |
|---|---|
| 启动服务 | sudo systemctl start grafana-server |
| 停止服务 | sudo systemctl stop grafana-server |
| 重启服务 | sudo systemctl restart grafana-server |
| 查看状态 | sudo systemctl status grafana-server |
| 设置开机自启 | sudo systemctl enable grafana-server |
| 查看日志 | sudo journalctl -u grafana-server -f |
| 查看版本 | /usr/sbin/grafana-server -v |
| 重置密码 | sudo /usr/sbin/grafana-cli admin reset-admin-password <新密码> |
| 安装插件 | sudo /usr/sbin/grafana-cli plugins install <插件名> |
| 查看配置 | cat /etc/grafana/grafana.ini |
第十部分:升级指南
10.1 查看当前版本
bash
/usr/sbin/grafana-server -v10.2 下载新版本
bash
wget https://github.com/grafana/grafana/releases/download/v13.2.0/grafana_13.2.0_linux_amd64.deb10.3 执行升级
bash
# 停止服务
sudo systemctl stop grafana-server
# 备份配置和数据
sudo cp -r /etc/grafana /etc/grafana.bak
sudo cp -r /var/lib/grafana /var/lib/grafana.bak
# 升级安装
sudo dpkg -i grafana_13.2.0_linux_amd64.deb
# 启动服务
sudo systemctl start grafana-server
# 验证版本
/usr/sbin/grafana-server -v第十一部分:DEB vs RPM vs Docker 对比
| 对比项 | DEB 安装 | RPM 安装 | Docker 容器 |
|---|---|---|---|
| 适用系统 | Ubuntu/Debian | CentOS/RHEL | 所有 Linux |
| 依赖管理 | ✅ APT 自动处理 | ✅ YUM 自动处理 | ✅ 镜像自带 |
| 服务管理 | ✅ systemd | ✅ systemd | ⚠️ 容器管理 |
| 安装速度 | ⚡ 极快 | ⚡ 极快 | ⚡ 快 |
| 数据持久化 | ✅ 本地目录 | ✅ 本地目录 | ⚠️ 需挂载卷 |
| 学习曲线 | 低 | 低 | 中 |
| 适合场景 | Ubuntu/Debian 生产环境 | CentOS/RHEL 生产环境 | 云原生环境 |
结语
通过本文,您已经掌握了在 Ubuntu/Debian 系统上通过 DEB 包安装 Grafana 的完整流程:
- ✅ 下载并安装 Grafana DEB 包
- ✅ 配置 Grafana(绑定了
0.0.0.0,允许外部访问) - ✅ 使用 systemd 管理服务
- ✅ 开放防火墙(ufw)配置
- ✅ 访问 Grafana Web 界面
- ✅ 常见问题排障
- ✅ 版本升级流程
DEB 安装方式的核心优势:简单、快速、可控,是 Ubuntu/Debian 系统下单机和传统运维场景的最佳选择。
相关资源:
本文全部操作在 Ubuntu 22.04 LTS 环境下验证通过,适用 Debian 11+ 及兼容发行版。
