0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[小ネタ]Linuxでインストールしたいライブラリやコマンドが含まれるパッケージを検索する方法

Posted at

画像1.png

概要

Linuxを動かしていて、このライブラリやコマンドってどのパッケージに含まれるんだろう。って考えることがあるかと思います。私です。
そんな時に役立つコマンドがあることを知ったので備忘録として残しておきます。

RedHat系

  • Amazon Linux 2023で確認
  • 検証のため既存でインストールされているコマンドをアンインストール済

dnf provides

調べ方は以下の通りです。

# dnf provides 知りたいコマンドorライブラリ

試しにpingコマンドを調べて必要なパッケージをインストールしてみます。
現状pingコマンドはインストールされていません。

# which ping
/usr/bin/which: no ping in (/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)

providesコマンドを実行すると、amazonlinuxリポジトリで管理しているiputilsパッケージに含まれていることがわかりますね。

# dnf provides ping
Last metadata expiration check: 1:06:33 ago on Mon Feb  3 12:39:28 2025.
iputils-20210202-2.amzn2023.0.3.x86_64 : Network monitoring tools including ping
Repo        : amazonlinux
Matched from:
Filename    : /usr/bin/ping
Filename    : /usr/sbin/ping
Provide    : /bin/ping

iputils-20210202-2.amzn2023.0.4.x86_64 : Network monitoring tools including ping
Repo        : amazonlinux
Matched from:
Filename    : /usr/bin/ping
Filename    : /usr/sbin/ping
Provide    : /bin/ping

providesコマンドは、サーバーに登録されているリポジトリから検索しています。

# dnf repolist
repo id                             repo name
amazonlinux                         Amazon Linux 2023 repository
kernel-livepatch                    Amazon Linux 2023 Kernel Livepatch repository

必要なパッケージがわかったのでインストールしていきます。

# dnf install -y iputils
Last metadata expiration check: 1:18:15 ago on Mon Feb  3 12:39:28 2025.
Dependencies resolved.
=================================================================================================================================================================================================================
 Package                                      Architecture                                Version                                                         Repository                                        Size
=================================================================================================================================================================================================================
Installing:
 iputils                                      x86_64                                      20210202-2.amzn2023.0.4                                         amazonlinux                                      169 k

Transaction Summary
=================================================================================================================================================================================================================
Install  1 Package

Total download size: 169 k
Installed size: 527 k
Downloading Packages:
iputils-20210202-2.amzn2023.0.4.x86_64.rpm                                                                                                                                       1.6 MB/s | 169 kB     00:00
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                            973 kB/s | 169 kB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                         1/1
  Installing       : iputils-20210202-2.amzn2023.0.4.x86_64                                                                                                                                                  1/1
  Running scriptlet: iputils-20210202-2.amzn2023.0.4.x86_64                                                                                                                                                  1/1
  Verifying        : iputils-20210202-2.amzn2023.0.4.x86_64                                                                                                                                                  1/1

Installed:
  iputils-20210202-2.amzn2023.0.4.x86_64

Complete!

再度pingコマンドを確認すると、利用できるようになっていますね。
ライブラリも同様の方法で確認できます。「*」をつけて部分一致検索も可能です。

# which ping
/usr/sbin/ping

providesコマンド実行時にRepo欄が「@System」になっているものはインストール済みパッケージを表しています。

# dnf provides ping
Last metadata expiration check: 1:23:31 ago on Mon Feb  3 12:39:28 2025.
iputils-20210202-2.amzn2023.0.3.x86_64 : Network monitoring tools including ping
Repo        : amazonlinux
Matched from:
Provide    : /bin/ping
Filename    : /usr/bin/ping
Filename    : /usr/sbin/ping

iputils-20210202-2.amzn2023.0.4.x86_64 : Network monitoring tools including ping
Repo        : @System
Matched from:
Provide    : /bin/ping
Filename    : /usr/bin/ping
Filename    : /usr/sbin/ping

iputils-20210202-2.amzn2023.0.4.x86_64 : Network monitoring tools including ping
Repo        : amazonlinux
Matched from:
Provide    : /bin/ping
Filename    : /usr/bin/ping
Filename    : /usr/sbin/ping

Debian系

  • Ubuntu 22.04で確認
  • 検証のため既存でインストールされているコマンドをアンインストール済

apt-file search

調べ方は以下の通りです。

# apt-file search 知りたいコマンドorライブラリ

apt-fileはデフォルトで入っていないことがあるのでその場合はインストールします。

# apt install -y apt-file

apt-fileインストール時に見つからないとなった場合は、 apt update を実行し、登録しているリポジトリから最新のパッケージリストを取得しローカルのパッケージリスト(キャッシュ)を最新化してから実行してください。
リポジトリとローカルのパッケージリストの差分をなくすことで、パッケージマネージャ(apt)が正しく認識してくれます。

まず、apt-fileがパッケージを認識できるようにするため以下のコマンドを実行します。

# apt-file update
Hit:1 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:3 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu jammy-backports InRelease
Get:4 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu jammy amd64 Contents (deb) [46.9 MB]
Get:5 http://security.ubuntu.com/ubuntu jammy-security InRelease [129 kB]
Get:6 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu jammy-updates amd64 Contents (deb) [175 MB]
Get:7 http://security.ubuntu.com/ubuntu jammy-security amd64 Contents (deb) [169 MB]
Get:8 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu jammy-backports amd64 Contents (deb) [760 kB]
Fetched 392 MB in 13min 23s (488 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
27 packages can be upgraded. Run 'apt list --upgradable' to see them.

登録しているリポジトリを見てみると、リポジトリから情報をとってきているような動きをしてますね。

# grep -rE '^[^#]' /etc/apt/sources.list /etc/apt/sources.list.d/
/etc/apt/sources.list:deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ jammy main restricted
/etc/apt/sources.list:deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ jammy-updates main restricted
/etc/apt/sources.list:deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ jammy universe
/etc/apt/sources.list:deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ jammy-updates universe
/etc/apt/sources.list:deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ jammy multiverse
/etc/apt/sources.list:deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ jammy-updates multiverse
/etc/apt/sources.list:deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse
/etc/apt/sources.list:deb http://security.ubuntu.com/ubuntu jammy-security main restricted
/etc/apt/sources.list:deb http://security.ubuntu.com/ubuntu jammy-security universe
/etc/apt/sources.list:deb http://security.ubuntu.com/ubuntu jammy-security multiverse

試しにpingコマンドを調べて必要なパッケージをインストールしてみます。
現状pingコマンドはインストールされていません。

# which ping
#

apt-fileを実行すると、複数ヒットしました。

# apt-file search /bin/ping
ctdb: /usr/bin/ping_pong
inetutils-ping: /bin/ping
inetutils-ping: /bin/ping6
iputils-ping: /bin/ping
iputils-ping: /bin/ping4
iputils-ping: /bin/ping6
rsplib-services: /usr/bin/pingpongclient

今回はiputils-pingパッケージが該当しますのでインストールしていきます。

# apt install -y iputils-ping
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  iputils-ping
0 upgraded, 1 newly installed, 0 to remove and 27 not upgraded.
Need to get 42.9 kB of archives.
After this operation, 116 kB of additional disk space will be used.
Get:1 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu jammy/main amd64 iputils-ping amd64 3:20211215-1 [42.9 kB]
Fetched 42.9 kB in 0s (123 kB/s)
Selecting previously unselected package iputils-ping.
(Reading database ... 65960 files and directories currently installed.)
Preparing to unpack .../iputils-ping_3%3a20211215-1_amd64.deb ...
Unpacking iputils-ping (3:20211215-1) ...
Setting up iputils-ping (3:20211215-1) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...
Scanning linux images...

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.

再度pingコマンドを確認すると、利用できるようになっていますね。

# which ping
/usr/bin/ping

apt-file はライブラリがインストールされるフルパスを指定しない検索が大変な印象です。

まとめ

今回はLinuxにおいてライブラリやコマンドが含まれるパッケージの検索方法についてまとめてみました!
少しでも誰かの役に立てば幸いです。

参考文献

リファレンス

ブログ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?