RPMコマンド　備忘録

# rpmコマンドの備忘録

Red Hat Linuxの勉強をしていて、結構忘れがちなRPMコマンドを備忘として残しておきます。

#### 1. `rpm -qa`　全てのパッケージを表示する

```
vagrant@rhel8 ~]$ rpm -qa
libunistring-0.9.9-3.el8.x86_64
libsss_certmap-2.2.3-20.el8.x86_64
bzip2-1.0.6-26.el8.x86_64
virt-what-1.18-6.el8.x86_64
xkeyboard-config-2.28-1.el8.noarch
...
```

#### 2. `rpm -qf <filename>`　対象ファイルのパッケージを探す

```
[vagrant@rhel8 scratch]$ rpm -qf /etc/nginx/nginx.conf
nginx-1.14.1-9.module+el8.0.0+4108+af250afe.x86_64
```

#### 3. `rpm -q <package>`　パッケージのバージョンを表示する

```
[vagrant@rhel8 ~]$ rpm -q ansible
ansible-2.9.10-1.el8.noarch
```

#### 4. `rpm -qi <package>`　パッケージの詳細を表示する

```
[vagrant@rhel8 ~]$ rpm -qi ansible
Name        : ansible
Version     : 2.9.10
Release     : 1.el8
Architecture: noarch
Install Date: Tue Jun 23 21:19:24 2020
Group       : Unspecified
Size        : 102332126
License     : GPLv3+
Signature   : RSA/SHA256, Fri Jun 19 05:08:18 2020, Key ID 21ea45ab2f86d6a1
Source RPM  : ansible-2.9.10-1.el8.src.rpm
Build Date  : Fri Jun 19 03:20:14 2020
Build Host  : buildvm-ppc64le-14.iad2.fedoraproject.org
Relocations : (not relocatable)
Packager    : Fedora Project
Vendor      : Fedora Project
URL         : http://ansible.com
Bug URL     : https://bugz.fedoraproject.org/ansible
Summary     : SSH-based configuration management, deployment, and task execution system
Description :
Ansible is a radically simple model-driven configuration management,
multi-node deployment, and remote task execution system. Ansible works
over SSH and does not require any software or daemons to be installed
on remote nodes. Extension modules can be written in any language and
are transferred to managed machines automatically.
```

#### 5. `rpm -ql <package>`　インストールされているファイルを表示する

```
[vagrant@rhel8 ~]$ rpm -ql ansible
/etc/ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
/etc/ansible/roles
/usr/bin/ansible
/usr/bin/ansible-config
/usr/bin/ansible-connection
/usr/bin/ansible-console
/usr/bin/ansible-doc
...
```

#### 6. `rpm -qc <package>`　パッケージに関連する設定ファイルを表示する

```
[vagrant@rhel8 ~]$ rpm -qc ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
```

#### 7. `rpm -qd <package>`　パッケージに関連するドキュメントを表示する

```
[vagrant@rhel8 ~]$ rpm -qd ansible
/usr/share/doc/ansible/CHANGELOG-v2.9.rst
/usr/share/doc/ansible/PKG-INFO
/usr/share/doc/ansible/README.rst
/usr/share/man/man1/ansible-config.1.gz
/usr/share/man/man1/ansible-console.1.gz
/usr/share/man/man1/ansible-doc.1.gz
/usr/share/man/man1/ansible-galaxy.1.gz
/usr/share/man/man1/ansible-inventory.1.gz
/usr/share/man/man1/ansible-playbook.1.gz
/usr/share/man/man1/ansible-pull.1.gz
/usr/share/man/man1/ansible-vault.1.gz
/usr/share/man/man1/ansible.1.gz
```

#### 8. `rpm -q --scripts <package>`　パッケージのインストールおよびアンインストールの前後に実行されるスクリプトを表示する

```
[vagrant@rhel8 ~]$ rpm -q --scripts nginx
postinstall scriptlet (using /bin/sh):

if [ $1 -eq 1 ] ; then 
        # Initial installation 
        systemctl --no-reload preset nginx.service &>/dev/null || : 
fi
preuninstall scriptlet (using /bin/sh):

if [ $1 -eq 0 ] ; then 
        # Package removal, not upgrade 
        systemctl --no-reload disable --now nginx.service &>/dev/null || : 
fi
postuninstall scriptlet (using /bin/sh):

if [ $1 -ge 1 ]; then
    /usr/bin/nginx-upgrade >/dev/null 2>&1 || :
fi
```

#### 9. `rpm -q --changelog <package>`　パッケージの変更履歴を表示する

```
[vagrant@rhel8 ~]$ rpm -q --changelog ansible
* Thu Jun 18 2020 Kevin Fenzi <kevin@scrye.com> - 2.9.10-1
- Update to 2.9.10.

* Tue May 12 2020 Kevin Fenzi <kevin@scrye.com> - 2.9.9-1
- Update to 2.9.9. Fixes bug #1834582
- Fixes gathering facts on f32+ bug #1832625

* Sat Apr 18 2020 Kevin Fenzi <kevin@scrye.com> - 2.9.7-1
- Update to 2.9.7.
- fixes CVE-2020-1733 CVE-2020-1735 CVE-2020-1740 CVE-2020-1746 CVE-2020-1753 CVE-2020-10684 CVE-2020-10685 CVE-2020-10691
- Drop the -s from the shebang to allow ansible to use locally installed modules.
...
```

#### 10. `rpm -ivh <filename.rpm>`　対象ファイルのパッケージをインストールする

依存関係があるパッケージは先にインストールしておかないとエラーとなる。

```
[vagrant@rhel8 ~]$ sudo rpm -ivh nmap-7.70-5.el8.x86_64.rpm 
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:nmap-2:7.70-5.el8                ################################# [100%]
```


#### `yumdownloader <pakage>`　パッケージのRPMファイルをダウンロードする

```
[vagrant@rhel8 ~]$ yumdownloader nginx
Failed to set locale, defaulting to C.UTF-8
2020-06-23 21:28:10,301 [ERROR] yumdownloader:3139:MainThread @logutil.py:194 - [Errno 13] Permission denied: '/var/log/rhsm/rhsm.log' - Further logging output will be written to stderr
Not root, Subscription Management repositories not updated
Last metadata expiration check: 0:09:45 ago on Tue Jun 23 21:18:25 2020.
nginx-1.14.1-9.module+el8.0.0+4108+af250afe.x86_64.rpm                                                                                              913 kB/s | 570 kB     00:00    
```

#### `rpm2cpio <filename.rpm> | cpio -id`　RPMファイルを展開して、展開したフォルダにファイルを配置する。

詳細を表示したい場合は、最後のオプションに`v`を追加し、`rpm2cpio <filename.rpm> | cpio -idv`とする。

```
[vagrant@rhel8 scratch]$ rpm2cpio ansible-2.9.10-1.el8.noarch.rpm | cpio -id
207302 blocks
[vagrant@rhel8 scratch]$ ls -l
total 17688
-rw-rw-r--. 1 vagrant vagrant 18108720 Jun 23 21:22 ansible-2.9.10-1.el8.noarch.rpm
drwxrwxr-x. 3 vagrant vagrant       21 Jun 23 21:32 etc
drwxrwxr-x. 5 vagrant vagrant       41 Jun 23 21:32 usr
```

#### `rpm2cpio <filename.rpm> | cpio -id "<検索文字>"`　検索文字にヒットするRPMファイルを展開して、展開したフォルダにファイルを配置する。

詳細を表示したい場合は、最後のオプションに`v`を追加し、`rpm2cpio <filename.rpm> | cpio -id "<検索文字>"`とする。

```
[vagrant@rhel8 scratch]$ rpm2cpio ansible-2.9.10-1.el8.noarch.rpm | cpio -idv "*txt"
./usr/lib/python3.6/site-packages/ansible-2.9.10-py3.6.egg-info/SOURCES.txt
./usr/lib/python3.6/site-packages/ansible-2.9.10-py3.6.egg-info/dependency_links.txt
./usr/lib/python3.6/site-packages/ansible-2.9.10-py3.6.egg-info/requires.txt
./usr/lib/python3.6/site-packages/ansible-2.9.10-py3.6.egg-info/top_level.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/completion/docker.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/completion/network.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/completion/remote.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/completion/windows.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/ansible-test.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/constraints.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/coverage.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/integration.cloud.aws.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/integration.cloud.azure.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/integration.cloud.cs.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/integration.cloud.hcloud.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/integration.cloud.nios.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/integration.cloud.opennebula.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/integration.cloud.openshift.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/integration.cloud.vcenter.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/integration.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/network-integration.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/sanity.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/units.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/requirements/windows-integration.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/sanity/pep8/current-ignore.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/sanity/rstcheck/ignore-substitutions.txt
./usr/lib/python3.6/site-packages/ansible_test/_data/sanity/shellcheck/exclude.txt
207302 blocks
[vagrant@rhel8 scratch]$ ls -l
total 17688
-rw-rw-r--. 1 vagrant vagrant 18108720 Jun 23 21:22 ansible-2.9.10-1.el8.noarch.rpm
drwxrwxr-x. 3 vagrant vagrant       17 Jun 23 21:33 usr
```