Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu Wang 2021-01-06 21:24:59 +08:00
commit f89a973f84
3 changed files with 307 additions and 384 deletions

View File

@ -1,8 +1,8 @@
[#]: collector: (lujun9972)
[#]: translator: (FSSlc)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-12987-1.html)
[#]: subject: (How to Check Dependencies of a Package in Ubuntu/Debian-based Linux Distributions)
[#]: via: (https://itsfoss.com/check-dependencies-package-ubuntu/)
[#]: author: (Abhishek Prakash https://itsfoss.com/author/abhishek/)
@ -10,13 +10,15 @@
如何在基于 Ubuntu 或 Debian 的 Linux 发行版中查看一个软件包的依赖
======
![](https://img.linux.net.cn/data/attachment/album/202101/06/112738sv0dmjojmjokpxt0.jpg)
在 Ubuntu 或 Debian 中通过命令行来安装应用是一件很简单的事,你只需要执行 `apt install package_name` 就可以了。
但如果你想在安装一个软件包之前或之后知晓这个软件包的依赖,那该怎么办呢?
在本教程中,我将向你展示多种方法来在 Ubuntu 或其他使用 [APT 包管理器][1] 的 Debian 系 Linux 发行版中查看一个软件包的依赖。
### 在 Ubuntu 中什么是包依赖?
### 什么是 Ubuntu 中的包依赖?
当你在 Linux 中安装一个软件包,有时这个软件包还需要其他的软件包来使它工作正常。这些额外的软件包就叫作这个包的依赖。假如这些软件包之前没有在系统中被安装,那么这些依赖在安装这个软件包的同时会被自动安装上。
@ -30,18 +32,18 @@
#### 使用 apt show 来查看依赖
你可以使用 [apt show 命令][6] 来展示一个包的详细信息。其中依赖信息就是其中一部分,你可以在以 Depends 打头的那些行中看到它们。
你可以使用 [apt show 命令][6] 来展示一个包的详细信息。其中依赖信息就是其中一部分,你可以在以 Depends 打头的那些行中看到它们。
例如,下面展示的是使用 `apt show` 展示 [ubuntu-restricted-extras][7] 这个包的详细信息:
```
[email protected]:~$ apt show ubuntu-restricted-extras
abhishek@itsfoss:~$ apt show ubuntu-restricted-extras
Package: ubuntu-restricted-extras
Version: 67
Priority: optional
Section: multiverse/metapackages
Origin: Ubuntu
Maintainer: Ubuntu Developers <[email protected]>
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 14.3 kB
Depends: ubuntu-restricted-addons
@ -72,15 +74,17 @@ Description: Commonly used media codecs and fonts for Ubuntu
> **什么是推荐包?**
>
> 你注意到了上面结果输出中以 Recommends 开头的那些行了吗?
> 你注意到了上面结果输出中以 Recommends 开头的那些行了吗?
>
> 推荐包不是软件包的直接依赖,但它们可以开启软件包的一些额外功能。
>
> 正如你上面看到的那样, `ubuntu-restricted-extras` 包有 `ttf-mscorefonts-installer` 这个推荐包,用来在 Ubuntu 上安装 Microsoft 的字体。
> 正如你上面看到的那样, `ubuntu-restricted-extras` 包有 `ttf-mscorefonts-installer` 这个推荐包,用来在 Ubuntu 上安装 Microsoft 的字体。
>
> 这些推荐包也会默认被一同安装上,假如你想显式地禁止这些推荐包的安装,你可以像下面这样使用 `-no-install-recommends` 选项。
>
> `sudo apt install --no-install-recommends package_name`
> ```
> sudo apt install --no-install-recommends package_name
> ```
#### 使用 apt-cache 来直接获取依赖信息
@ -96,7 +100,7 @@ apt-cache depends package_name
#### 使用 dpkg 来查看一个 DEB 文件的依赖
`apt``apt-cache` 都作用于一个软件仓库中的软件包,但假如你下载了一个 DEB 文件,那么这两个命令就不起作用了。
`apt``apt-cache` 都作用于软件仓库中的软件包,但假如你下载了一个 DEB 文件,那么这两个命令就不起作用了。
在这种情形下,你可以使用 `dpkg` 命令的 `-I``--info` 选项。
@ -104,7 +108,7 @@ apt-cache depends package_name
dpkg -I path_to_deb_file
```
依赖信息就可以在以 Depends 开头的那些行中找到。
依赖信息就可以在以 Depends 开头的那些行中找到。
![][9]
@ -112,7 +116,7 @@ dpkg -I path_to_deb_file
假如你想查看更多关于依赖的信息,那么你可以使用 `apt-rdepends` 工具。这个工具可以创建完整的依赖树。这样你就可以得到一个软件包的依赖以及这些依赖的依赖。
它不是一个常规的 apt 命令,所以你需要从 universe 软件仓库中安装上它:
它不是一个常规的 `apt` 命令,所以你需要从 universe 软件仓库中安装上它:
```
sudo apt install apt-rdepends
@ -151,9 +155,9 @@ apt-rdepends -r package_name
输出可能会非常多,因为它将打印出反向依赖树。
```
[email protected]:~$ apt-rdepends -r ffmpeg
abhishek@itsfoss:~$ apt-rdepends -r ffmpeg
Reading package lists... Done
Building dependency tree
Building dependency tree
Reading state information... Done
ffmpeg
Reverse Depends: ardour-video-timeline (>= 1:5.12.0-3ubuntu4)
@ -172,7 +176,7 @@ via: https://itsfoss.com/check-dependencies-package-ubuntu/
作者:[Abhishek Prakash][a]
选题:[lujun9972][b]
译者:[FSSlc](https://github.com/FSSlc)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出

View File

@ -0,0 +1,287 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-12988-1.html)
[#]: subject: (Level up your shell history with Loki and fzf)
[#]: via: (https://opensource.com/article/20/10/shell-history-loki-fzf)
[#]: author: (Ed Welch https://opensource.com/users/ewelch)
用 Loki 和 fzf 进阶你的 Shell 历史记录
======
> Loki 扩展了 Prometheus 用于度量监测和日志聚合的模型。
![](https://img.linux.net.cn/data/attachment/album/202101/06/155012r4khll9zlqgx79fs.jpg)
[Loki][2] 是一个 Apache 2.0 许可的开源日志聚合框架,由 Grafana 实验室设计,并在不断发展的社区的巨大支持之下建立。它也是我每天为之努力的项目。在这篇文章中,我将不只是谈论 Loki 如何工作,而是提供一个实际操作的介绍,以解决实际问题。
### 问题:一个持久的集中式 Shell 历史记录
我喜欢我的 shell 历史,一直是 `CTRL+R` 的狂热用户。大约一年前,我的终端生活发生了翻天覆地的变化,我的同行 Dieter Plaetinck 向我介绍了命令行模糊查找器 [fzf][3]。
突然间,在命令中搜索就从这样:
![在 Loki 和 fzf 之前][4]
变成了这样:
![在 Loki 和 fzf 之后][6]
虽然 `fzf` 极大地提高了我的生活质量,但围绕着我的 shell 历史记录,还是缺少了一些片段:
* 终端突然关闭、电脑崩溃、死机、整盘加密密钥被遗忘等情况下会丢失 shell 历史记录。
* 想从我的所有电脑上访问我的 shell 历史记录。
我认为我的 shell 历史记录是文件:它是一个重要的故事,我不想失去。将 Loki 与我的 shell 历史结合起来,有助于解决这些问题和更多问题。
### 关于 Loki
Loki 采用了开源 [Prometheus][7] 项目用于度量的直观的标签模型,并将其扩展到日志聚合的世界。这使得开发人员和运维人员能够使用相同的标签集在他们的度量和日志之间无缝切换。即使你没有使用 Prometheus也有很多理由说明 Loki 可能很适合你的日志存储需求:
* **低开销:** Loki 不做全文日志索引;它只创建你放在日志上的标签的索引。保持小的索引大大降低了 Loki 的运维要求。我在 [树莓派][8] 上运行我的 loki-shell 项目,该项目使用 Loki 来存储 shell 历史记录,只使用了 50MB 多一点的内存。
* *成本低:**日志内容被压缩并存储在对象存储中,如 Amazon S3、Google 云存储、Azure Blob甚至可以直接存储在文件系统中。我们的目标是使用价格低廉且持久的存储。
* **灵活性:** Loki 以单个二进制文件的形式提供,可以直接下载并运行,也可以作为 Docker 镜像在任何容器环境中运行。在 Kubernetes 中可以用一个 [Helm 海图][9] 快速上手。如果你对日志工具的要求很高,可以看看运行在 Grafana 实验室的 [生产环境][10]。它使用开源的 [Jsonnet][11] 和 [Tanka][12] 部署了同样的 Loki 镜像作为离散的构件,以实现大规模的水平扩展、高可用性、复制、读写路径的分别扩展、高度可并行的查询等。
总而言之Loki 的方法是保留一个关于你的日志元数据的小索引(标签),并将未索引的、压缩的日志内容存储在廉价的对象存储中,以使操作更容易和更便宜。该应用程序被构建为单进程运行,并很容易演变成一个高可用的分布式系统。你可以通过并行化和查询的分片,在较大的日志工作负载上获得较高的查询性能 —— 有点像为你的日志设计的 MapReduce。
此外,这个功能是任何人都可以免费使用的。与其 [Grafana][13] 开放观测性平台一样Grafana 实验室致力于将 Loki 打造成一个任何人都可以使用的全功能、全开放的日志聚合软件。
### 开始吧
我在树莓派上运行 Loki并将我的 shell 历史记录异地存储在 S3 bucket 中。
当我按下 `CTRL+R`Loki 的 [LogCLI][14] 命令行界面会发起几个批处理请求,传输至 `fzf`。下面是一个例子,上半部分显示的是树莓派上的 Loki 服务器日志。
![树莓派上 Loki 服务器的日志][15]
准备试试?下面的指南将帮助你设置和运行 Loki与你的 shell 历史记录集成。为了让本教程保持简洁,此设置将 Loki 本地运行在你的计算机上,并在文件系统上存储所有文件。
在 [loki-shell 的 GitHub 版本库][16],你可以找到所有这一切,以及如何设置一个更复杂的安装的信息。
请注意,本教程不会改变任何围绕你的历史记录的现有行为,所以 _你现有的 shell 历史记录命令和历史记录设置不会被触动_。相反,这将用 Bash 中的 `$PROMPT_COMMAND` 和 Zsh 中的 `precmd` 复制命令历史记录到 Loki。在 `CTRL+R` 方面,它重载了 `fzf` 用来访问 `CTRL+R` 命令的函数。因此试一试是安全的,如果你觉得不喜欢它,只需按照 GitHub 版本库中的 [卸载步骤][17] 来删除所有痕迹。你的 shell 历史记录不会被触及。
#### 第一步:安装 fzf
安装 `fzf` 有几种方法,但我更喜欢 [Git 方法][18]
```
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
```
对所有的问题提示说 `yes`
如果你已经安装了 `fzf`,确保你已经启用了键绑定(即,确保当你输入 `CTRL+R` 时,`fzf` 会弹出)。如果有必要的话,你可以重新运行 `fzf` 安装过程来启用键绑定。
#### 第二步:安装 loki-shell
`fzf` 一样loki-shell 也有一个 Git 版本库和安装脚本:
```
git clone --depth 1 https://github.com/slim-bean/loki-shell.git ~/.loki-shell
~/.loki-shell/install
```
首先,该脚本创建了 `~/.loki-shell` 目录,所有的文件都将保存在该目录下(包括 Loki 数据),接下来,它将下载 [Promtail][19]、LogCLI 和 Loki 的二进制文件。
然后它会问:
```
Do you want to install Loki? ([y]/n)
```
如果你已经为 Loki-shell 运行了一个集中化的 Loki你可以回答 `n`;然而,对于本教程,回答 `y` 或按回车键。
在本地运行 Loki 有两种方式:作为一个 Docker 镜像或作为一个单一的二进制文件(支持添加为 systemd 服务)。如果可以,我建议使用 Docker因为我认为它稍微简化了操作但这两者都可以工作。
##### 使用 Docker 运行
将 Loki 作为 Docker 镜像运行:
```
[y] to run Loki in Docker, [n] to run Loki as a binary ([y]/n) y
Error: No such object: loki-shell
Error response from daemon: No such container: loki-shell
Error: No such container: loki-shell
54843ff3392f198f5cac51a6a5071036f67842bbc23452de8c3efa392c0c2e1e
```
如果这是你第一次运行这个安装程序,你可以忽略错误信息。这个脚本将停止和替换运行的 Loki 容器,如果版本不匹配,你可以重新运行此脚本升级 Loki。
就是这样Loki 现在作为一个 Docker 容器运行了。
Loki 的数据将存储在 `~/.loki-shell/data` 中。
由于带着 `-restart=unless-stopped` 标志运行该镜像,所以它会在系统重启时重启该服务,但如果你运行 `docker stop loki-shell` 则会保持停止。
(如果你使用的是 Docker你可以跳到 “Shell 集成”一节。)
##### 以二进制文件运行
在 Linux 系统上运行二进制文件的方法有很多。这个脚本可以安装一个 systemd 服务。如果你没有 systemd你也可以使用二进制安装
```
[y] to run Loki in Docker, [n] to run Loki as a binary ([y]/n) n
Run Loki with systemd? ([y]/n) n
This is as far as this script can take you
You will need to setup an auto-start for Loki
It can be run with this command: /home/username/.loki-shell/bin/loki -config.file=/home/username/.loki-shell/config/loki-binary-config.yaml
```
脚本会输出你需要用来运行 Loki 的命令,你可以自己设置一个 init 脚本或其他方法来自动启动它。
如果你想的话,你可以直接运行该命令,从你当前的 shell 运行 Loki。
如果你有 systemd你可以选择让脚本安装 systemd 服务或显示出你自己运行它的命令:
```
Run Loki with systemd? ([y]/n) y
Installing the systemd service requires root permissions.
[y] to run these commands with sudo [n] to print out the commands and you can run them yourself. ([y]/n) n
sudo cp /home/ed/.loki-shell/config/loki-shell.service /etc/systemd/system/loki-shell.service
sudo systemctl daemon-reload
sudo systemctl enable loki-shell
sudo systemctl start loki-shell
Copy these commands and run them when the script finishes. (press enter to continue)
```
##### Shell 集成
无论你如何安装 Loki你现在应该看到一个提示
```
Enter the URL for your Loki server or press enter for default (http://localhost:4100)
```
如果你已经设置了一个中心化的 Loki你应在这里输入其 URL。然而这个演示只是使用了默认的 URL所以你可以按回车键。
它会输出很多文本来解释添加到你的 `~.bashrc``~.zshrc`(或两者)的所有条目。
好了!
```
Finished. Restart your shell or reload config file.
   source ~/.bashrc  # bash
   source ~/.zshrc   # zsh
```
#### 第三步:试试吧!
开始使用你的 shell并使用 `CTRL+R` 查看你的命令。
打开多个终端窗口,在一个窗口中输入命令,在另一个窗口中输入 `CTRL+R`,你会看到你的命令立即可用。
另外,请注意,当你在终端之间切换并输入命令时,使用 `CTRL+R` 可以立即使用它们,但向上箭头的操作在终端之间不受影响。(如果你安装了 Oh My Zsh情况可能就不一样了因为它会自动将所有命令追加到历史记录中。
多次按下 `CTRL+R` 可以在按时间排序和按相关性排序之间切换。
请注意,此配置将只显示当前主机的查询历史记录,即使你正在从多个主机向 Loki 发送 shell 数据。我认为默认情况下这是最合理的。如果你想改变这种行为,有很多地方可以调整;请参见 loki-shell 版本库了解更多。
它还安装了一个名为 `hist` 的别名。
```
alias hist="$HOME/.loki-shell/bin/logcli --addr=$LOKI_URL"
```
LogCLI 可以用来直接在 Loki 上查询和搜索你的历史,也允许你搜索其他主机。查看 LogCLI 的入门指南,了解更多关于查询的信息。
Loki 的日志查询语言LogQL提供了度量查询可以让你做一些有趣的事情例如我可以看到在过去 30 天里我发出了多少次 `kc` 命令(我对 `kubectl` 的别名)。
![计数一个命令的使用次数][20]
### 额外增强
安装 Grafana摆弄一下你的 shell 历史记录。
```
docker run -d -p 3000:3000 --name=grafana grafana/grafana
```
打开 Web 浏览器,访问 `http://localhost:3000`,使用默认的 `admin`/`admin` 用户名和密码登录。
在左边,导航到“<ruby>配置<rt>Configuration</rt></ruby>-><ruby>数据源<rt>Datasources</rt></ruby>”,点击“<ruby>添加数据源<rt>Add Datasource</rt></ruby>”按钮,然后选择 “Loki”。
对于 URL你应该可以使用 `http://localhost:4100`(然而,在我的 WSL2 机器上,我必须使用计算机的实际 IP 地址)。
单击“<ruby>保存并测试<rt>Save and Test</rt></ruby>”。你应该看到连接了数据源并找到了标签。
点击左边的“<ruby>管理器<rt>Explore</rt></ruby>”图标,确保选择 Loki 数据源,并尝试这个查询:
```
{job="shell"}
```
如果发送 shell 命令的主机较多,可以使用“<ruby>主机<rt>Host</rt></ruby>”标签将结果限制在某个主机上:
```
{job="shell", hostname="myhost"}.
```
你也可以用过滤表达式寻找特定的命令:
```
{job="shell"} |= "docker"
```
或者你可以从日志中探索度量的世界,看看你使用 shell 的频率:
```
rate({job="shell"}[1m])
```
![计算过去 20 天内 shell 的使用情况][21]
想从一个事件中重建一个时间线?你可以通过特定的命令进行过滤,查看它的运行时间:
![计算命令的使用次数][22]
要想知道你还能做什么,并了解更多关于 Loki 查询语言的信息,请查看 LogQL 指南。
### 总结
更多的想法、故障排除和更新,请关注该 GitHub 版本库。这仍然是一项正在进行中的工作,所以请在那里报告发现的任何问题。
要了解更多关于 Loki 的信息,请查看文档、博客文章和该 GitHub 版本库,或者在 Grafana Cloud 中试用。
* * *
特别感谢我的同事 Jack Baldry 为这个想法播下的种子。我有 Loki 的知识来实现这个想法,但如果不是他的建议,我想我永远也不会做到这一点。
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/10/shell-history-loki-fzf
作者:[Ed Welch][a]
选题:[lujun9972][b]
译者:[wxy](https://github.com/wxy)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/ewelch
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/chaos_engineer_monster_scary_devops_gear_kubernetes.png?itok=GPYLvfVh (Gears above purple clouds)
[2]: https://github.com/grafana/loki
[3]: https://github.com/junegunn/fzf
[4]: https://opensource.com/sites/default/files/uploads/before.gif (Before Loki and fzf)
[5]: https://creativecommons.org/licenses/by-sa/4.0/
[6]: https://opensource.com/sites/default/files/uploads/with_fzf.gif (After Loki and fzf)
[7]: https://prometheus.io/
[8]: https://www.raspberrypi.org/
[9]: https://helm.sh/docs/topics/charts/
[10]: https://grafana.com/docs/loki/latest/installation/tanka/
[11]: https://jsonnet.org
[12]: https://tanka.dev/
[13]: https://grafana.com/
[14]: https://grafana.com/docs/loki/latest/getting-started/logcli/
[15]: https://opensource.com/sites/default/files/uploads/example_logcli.gif (Logs of the Loki server on Raspberry Pi)
[16]: https://github.com/slim-bean/loki-shell
[17]: https://github.com/slim-bean/loki-shell/blob/master/uninstall
[18]: https://github.com/junegunn/fzf#using-git
[19]: https://grafana.com/docs/loki/latest/clients/promtail/
[20]: https://opensource.com/sites/default/files/uploads/count_kc.png (Counting use of a command)
[21]: https://opensource.com/sites/default/files/uploads/last_20.png (Counting use of the shell over previous 20 days)
[22]: https://opensource.com/sites/default/files/uploads/command_hist.png (Counting use of a command)

View File

@ -1,368 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Level up your shell history with Loki and fzf)
[#]: via: (https://opensource.com/article/20/10/shell-history-loki-fzf)
[#]: author: (Ed Welch https://opensource.com/users/ewelch)
Level up your shell history with Loki and fzf
======
Loki expands the model Prometheus uses for metrics for monitoring and
log aggregation.
![Gears above purple clouds][1]
[Loki][2] is an Apache 2.0-licensed open source log-aggregation framework designed by Grafana Labs and built with tremendous support from a growing community. It is also the project I work on every day. In this article, rather than just talking about how Loki works, I will provide a hands-on introduction to solving real problems with it.
### The problem: a durable centralized shell history
I love my shell history and have always been a fanatical CTRL+R user. About a year ago, my terminal life changed forever when my peer Dieter Plaetinck introduced me to the command-line fuzzy finder **[fzf][3]**.
Suddenly, searching through commands went from this:
![Before Loki and fzf][4]
(Ed Welch, [CC BY-SA 4.0][5])
To this:
![After Loki and fzf][6]
(Ed Welch, [CC BY-SA 4.0][5])
While fzf significantly improved my quality of life, there were still some pieces missing around my shell history:
* Losing shell history when terminals close abruptly, computers crash, computers die, whole disk encryption keys are forgotten
* Having access to my shell history _from_ all my computers _on_ all my computers
I think of my shell history as documentation: it's an important story I don't want to lose. Combining Loki with my shell history helps solve these problems and more.
### About Loki
Loki takes the intuitive label model that the open source [Prometheus][7] project uses for metrics and expands it into the world of log aggregation. This enables developers and operators to seamlessly pivot between their metrics and logs using the same set of labels. Even if you're not using Prometheus, there are still plenty of reasons Loki might be a good fit for your log-storage needs:
* **Low overhead:** Loki does not do full-text log indexing; it only creates an index of the labels you put on your logs. Keeping a small index substantially reduces Loki's operating requirements. I'm running my loki-shell project, which uses Loki to store shell history, on a [Raspberry Pi][8] using just a little over 50MB of memory.
* **Low cost:** The log content is compressed and stored in object stores like Amazon S3, Google Cloud Storage, Azure Blob, or even directly on a filesystem. The goal is to use storage that is inexpensive and durable.
* **Flexibility:** Loki is available in a single binary that can be downloaded and run directly or as a Docker image to run in any container environment. A [Helm chart][9] is available to get started quickly in Kubernetes. If you demand a lot from your logging tools, take a look at the [production setup][10] running at Grafana Labs. It uses open source [Jsonnet][11] and [Tanka][12] to deploy the same Loki image as discrete building blocks to enable massive horizontal scaling, high availability, replication, separate scaling of read and write paths, highly parallelizable querying, and more.
In summary, Loki's approach is to keep a small index of metadata about your logs (labels) and store the unindexed and compressed log content in inexpensive object stores to make operating easier and cheaper. The application is built to run as a single process and easily evolve into a highly available distributed system. You can obtain high query performance on larger logging workloads through parallelization and sharding of queries—a bit like MapReduce for your logs.
In addition, this functionality is available for anyone to use for free. As with its [Grafana][13] open observability platform, Grafana Labs is committed to making Loki a fully featured, fully open log-aggregation software anyone can use.
### Get started
I'm running Loki on a Raspberry Pi on my home network and storing my shell history offsite in an S3 bucket.
When I hit CTRL+R, Loki's [LogCLI][14] command-line interface makes several batching requests that are streamed into fzf. Here is an example—the top part shows the Loki server logs on the Pi.
![Logs of the Loki server on Raspberry Pi][15]
(Ed Welch, [CC BY-SA 4.0][5])
Ready to give it a try? The following guide will help you set up and run Loki to be integrated with your shell history. Since this tutorial aims to keep things simple, this setup will run Loki locally on your computer and store all the files on the filesystem.
You can find all of this, plus information about how to set up a more elaborate installation, in the [loki-shell GitHub repository][16].
Note that this tutorial will not change any existing behaviors around your history, so _your existing shell history command and history settings will be untouched._ Instead, this duplicates the command history to Loki with `$PROMPT_COMMAND` in Bash and `precmd` in Zsh. On the CTRL+R side of things, it overloads the function that fzf uses to access the CTRL+R command. Trying this is safe, and if you decide you don't like it, just follow the [uninstall steps][17] in the GitHub repo to remove all traces. Your shell history will be untouched.
#### Step 1: Install fzf
There are several ways to install fzf, but I prefer [the Git method][18]:
```
git clone --depth 1 <https://github.com/junegunn/fzf.git> ~/.fzf
~/.fzf/install
```
Say yes to all the question prompts.
If you already have fzf installed, make sure you have the key bindings enabled (i.e., make sure when you type CTRL+R, fzf pops up). You can rerun the fzf installation to enable key bindings if necessary.
#### Step 2: Install loki-shell
Like fzf, loki-shell also has a Git repo and install script:
```
git clone --depth 1 <https://github.com/slim-bean/loki-shell.git> ~/.loki-shell
~/.loki-shell/install
```
First, the script creates the `~/.loki-shell` directory where all files will be kept (including Loki data). Next, it will download binaries for [Promtail][19], LogCLI, and Loki.
Then it will ask:
```
Do you want to install Loki? ([y]/n)
```
If you already have a centralized Loki running for loki-shell, you could answer n; however, for this tutorial, answer y or press Enter.
There are two options available for running Loki locally: as a Docker image or as a single binary (with support for adding a systemd service). I recommend using Docker if it's available, as I think it simplifies operations a bit, but both work just fine.
```
#### Running with Docker
```
To run Loki as a Docker image:
[code]
```
[y] to run Loki in Docker, [n] to run Loki as a binary ([y]/n) y
Error: No such object: loki-shell
Error response from daemon: No such container: loki-shell
Error: No such container: loki-shell
54843ff3392f198f5cac51a6a5071036f67842bbc23452de8c3efa392c0c2e1e
```
```
If this is the first time you're running the installation, you can disregard the error messages. This script will stop and replace a running Loki container if the version does not match, which allows you to rerun this script to upgrade Loki.
That's it! Loki is now running as a Docker container.
Data from Loki will be stored in ~/.loki-shell/data.
The image runs with --restart=unless-stopped, so it will restart at reboot but will stay stopped if you run docker stop loki-shell.
(If you're using Docker, you can skip down to Shell integration.)
```
##### Running as binary
```
There are many ways to run a binary on a Linux system. This script can install a systemd service. If you don't have systemd, you can still use the binary install:
[code]
```
[y] to run Loki in Docker, [n] to run Loki as a binary ([y]/n) n
Run Loki with systemd? ([y]/n) n
This is as far as this script can take you
You will need to setup an auto-start for Loki
It can be run with this command: /home/username/.loki-shell/bin/loki -config.file=/home/username/.loki-shell/config/loki-binary-config.yaml
```
```
The script will spit out the command you need to use to run Loki, and you will be on your own to set up an init script or another method of auto-starting it.
You can run the command directly, if you want, and run Loki from your current shell.
If you do have systemd, you have the option of letting the script install the systemd service or showing you the commands to run it yourself:
[code]
```
Run Loki with systemd? ([y]/n) y
Installing the systemd service requires root permissions.
[y] to run these commands with sudo [n] to print out the commands and you can run them yourself. ([y]/n) n
sudo cp /home/ed/.loki-shell/config/loki-shell.service /etc/systemd/system/loki-shell.service
sudo systemctl daemon-reload
sudo systemctl enable loki-shell
sudo systemctl start loki-shell
Copy these commands and run them when the script finishes. (press enter to continue)
```
```
```
##### Shell integration
```
Regardless of how you installed Loki, you should now see a prompt:
[code]Enter the URL for your Loki server or press enter for default (http://localhost:4100)
```
If you had set up a centralized Loki, you would enter that URL here. However, this demo just uses the default, so you can press Enter.
A lot of text will spit out explaining all the entries added to your ~.bashrc or ~.zshrc (or both).
That's it!
[code]
```
Finished. Restart your shell or reload config file.
   source ~/.bashrc  # bash
   source ~/.zshrc   # zsh
```
```
```
#### Step 3: Try it out!
```
Start using your shell, and use CTRL+R to see your commands.
Open multiple terminal windows, type a command in one and CTRL+R in another, and you'll see your commands available immediately.
Also, notice that when you switch between terminals and enter commands, they are available immediately with CTRL+R, but the Up arrow's operation is not affected between terminals. (This may not be true if you have Oh My Zsh installed, as it automatically appends all commands to the history.)
Use CTRL+R multiple times to toggle between sorting by time and by relevance.
Note that this configuration will show only the current hosts' query history, even if you are sending shell data from multiple hosts to Loki. I think by default this makes the most sense. There is a lot you can tweak if you want this behavior to change; see the loki-shell repo to learn more.
It also installed an alias called hist:
[code]alias hist="$HOME/.loki-shell/bin/logcli --addr=$LOKI_URL"
```
LogCLI can be used to query and search your history directly in Loki, including allowing you to search other hosts. Check out the getting started guide for LogCLI to learn more about querying.
Loki's log query language (LogQL) provides metric queries that allow you to do some interesting things; for example, I can see how many times I issued the kc command (my alias for kubectl) in the last 30 days:
```
![Counting use of a command][20]
(Ed Welch, [CC BY-SA 4.0][5])
```
```
## Extra credit
```
Install Grafana and play around with your shell history:
[code]docker run -d -p 3000:3000 --name=grafana grafana/grafana
```
Open a web browser at http://localhost:3000 and log in using the default admin/admin username and password.
On the left, navigate to Configuration -> Datasources, click the Add Datasource button, and select Loki.
For the URL, you should be able to use http://localhost:4100 (however, on my WSL2 machine, I had to use the computer's actual IP address).
Click Save and Test. You should see Data source connected and labels found.
Click on the Explore icon on the left, make sure the Loki data source is selected, and try out a query:
[code]{job="shell"}
```
If you have more hosts sending shell commands, you can limit the results to a certain host using the hostname label:
[code]{job="shell", hostname="myhost"}.
```
You can also look for specific commands with filter expressions:
[code]{job="shell"} |= "docker"
```
Or you can start exploring the world of metrics from logs to see how often you are using your shell:
[code]rate({job="shell"}[1m])
```
```
![Counting use of the shell over previous 20 days][21]
(Ed Welch, [CC BY-SA 4.0][5])
```
Want to reconstruct a timeline from an incident? You can filter by a specific command and see when it ran.
```
![Counting use of a command][22]
(Ed Welch, [CC BY-SA 4.0][5])
```
To see what else you can do and learn more about Loki's query language, check out the LogQL guide.
```
### Final thoughts
```
For more ideas, troubleshooting, and updates, follow the GitHub repo. This is still a work in progress, so please report any issues there.
To learn more about Loki, check out the documentation, blog posts, and GitHub repo, or try it in Grafana Cloud.
```
* * *
```
A special thanks to my colleague Jack Baldry for planting the seed for this idea. I had the Loki knowledge to make this happen, but if it weren't for his suggestion, I don't think I ever would have made it here.
```
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/10/shell-history-loki-fzf
作者:[Ed Welch][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/ewelch
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/chaos_engineer_monster_scary_devops_gear_kubernetes.png?itok=GPYLvfVh (Gears above purple clouds)
[2]: https://github.com/grafana/loki
[3]: https://github.com/junegunn/fzf
[4]: https://opensource.com/sites/default/files/uploads/before.gif (Before Loki and fzf)
[5]: https://creativecommons.org/licenses/by-sa/4.0/
[6]: https://opensource.com/sites/default/files/uploads/with_fzf.gif (After Loki and fzf)
[7]: https://prometheus.io/
[8]: https://www.raspberrypi.org/
[9]: https://helm.sh/docs/topics/charts/
[10]: https://grafana.com/docs/loki/latest/installation/tanka/
[11]: https://jsonnet.org
[12]: https://tanka.dev/
[13]: https://grafana.com/
[14]: https://grafana.com/docs/loki/latest/getting-started/logcli/
[15]: https://opensource.com/sites/default/files/uploads/example_logcli.gif (Logs of the Loki server on Raspberry Pi)
[16]: https://github.com/slim-bean/loki-shell
[17]: https://github.com/slim-bean/loki-shell/blob/master/uninstall
[18]: https://github.com/junegunn/fzf#using-git
[19]: https://grafana.com/docs/loki/latest/clients/promtail/
[20]: https://opensource.com/sites/default/files/uploads/count_kc.png (Counting use of a command)
[21]: https://opensource.com/sites/default/files/uploads/last_20.png (Counting use of the shell over previous 20 days)
[22]: https://opensource.com/sites/default/files/uploads/command_hist.png (Counting use of a command)