LoginSignup
4
3

More than 1 year has passed since last update.

apt-keyを使わないサードパーティーリポジトリからのパッケージのインストール方法

Last updated at Posted at 2022-06-16

apt-keyが廃止予定なのでその代替方法をまとめました。また、それに基づいたchromeのインストール方法と注意する点もメモしました。間違いや指摘あればコメントください。

以下の記事を参考に情報をまとめました。

第675回 apt-keyはなぜ廃止予定となったのか:Ubuntu Weekly Recipe|gihyo.jp … 技術評論社
apt-key が非推奨になったので | Zenn
Dockerfile: 非推奨化されたapt-keyを置き換える|TechRacho by BPS株式会社

今までサードパーティーリポジトリからパッケージをインストールする際は、apt-key addを用いてそのサードパーティーリポジトリの公開鍵を/etc/apt/trusted.gpgファイルに追加していました。

これからは/usr/local/share/keyringsなどに公開鍵をダウンロードし、検証することにするようです。

apt-keyを使った今までのやり方

例えば、公式サイトや古い記事で、サードパーティーリポジトリからのインストール方法の案内にapt-keyを使った今までのやり方が次のように書いてあったとします。

まず、次のコマンドによって/etc/apt/trusted.gpgファイルに公開鍵を追加します。

$ curl -sS [リポジトリの公開鍵ファイルのURL] | sudo apt-key add -

またはcurl -sSwget -qO -で代替できるので次のコマンドでもいいようです。

$ wget -qO - [リポジトリの公開鍵ファイルのURL] | sudo apt-key add -

そしてリポジトリ情報を/etc/apt/sources.list.d/以下に保存します。

$ sudo echo "deb リポジトリのURI オプション" > /etc/apt/sources.list.d/パッケージ名.list

オプションにはリポジトリの構成に応じたオプションします。この部分はインストール方法の案内に書いてあります。よくあるのはstable mainなどです。

そして、次のコマンドでパッケージをインストールします。

$ sudo apt-get update
$ sudo apt-get install パッケージ名

具体例を挙げると、yarnのインストール方法は2022/6/16現在、次のように書かれています。

$ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
$ sudo apt update
$ sudo apt install yarn

apt-keyを使わないやり方

まず、/usr/local/share/keyringsディレクトリを作成します。

$ mkdir /usr/local/share/keyrings

そしてサードパーティーリポジトリの公開鍵をダウンロードして、gpgでバイナリ形式に変換し、↑で作成したディレクトリに格納します。

$ curl -sS [リポジトリの公開鍵ファイルのURL] | sudo gpg --dearmor -o /usr/local/share/keyrings/パッケージ名-archive-keyring.gpg

そしてリポジトリ情報を/etc/apt/sources.list.d/以下に保存します。その際debとリポジトリのURIの間に、↑でダウンロードしたgpgファイルの場所を[signed-by=/usr/local/share/keyrings/パッケージ名-archive-keyring.gpg]のように指定します。

$ sudo echo "deb [signed-by=/usr/local/share/keyrings/パッケージ名-archive-keyring.gpg] リポジトリのURI オプション" > /etc/apt/sources.list.d/パッケージ名.list

そして、次のコマンドでパッケージをインストールします。

$ sudo apt-get update
$ sudo apt-get install パッケージ名

例えば、先ほどのyarnのインストール方法は次のようになります。

$ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo gpg --dearmor -o /usr/local/share/keyrings/yarn-archive-keyring.gpg
$ echo "deb [signed-by=/usr/local/share/keyrings/yarn-archive-keyring.gpg] https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
$ sudo apt update
$ sudo apt install yarn

Google Chromeのインストールについて

またapt-keyを使わずにGoogle Chromeインストールする方法は次のようになります。

$ curl -sS https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo gpg --dearmor -o /usr/local/share/keyrings/chrome-archive-keyring.gpg
$ echo "deb [signed-by=/usr/local/share/keyrings/chrome-archive-keyring.gpg] http://dl.google.com/linux/chrome/deb/ stable main" | sudo tee /etc/apt/sources.list.d/chrome.list
$ sudo apt update
$ sudo apt install google-chrome-stable

ただ、この方法でchromeをインストールすると、インストール後、chromeのリポジトリの公開鍵が/etc/apt/trusted.gpg.d/google-chrome.gpgとして、リポジトリ情報が/etc/apt/sources.list.d/google-chrome.listとしてファイルが勝手に新規作成され保存されます。このせいで、↑で自分で保存した公開鍵/usr/local/share/keyrings/chrome-archive-keyring.gpgとリポジトリ情報/etc/apt/sources.list.d/chrome.listと情報が競合し、次のようにapt-get updateapt listなどapt関係のコマンドがエラーになってしまいます。

$ apt-get update
E: Conflicting values set for option Signed-By regarding source http://dl.google.com/linux/chrome/deb/ stable: /usr/local/share/keyrings/chrome-archive-keyring.gpg != 
E: The list of sources could not be read

なので、Google Chromeをインストールしたら、次のように自分で保存した公開鍵とリポジトリ情報は削除しておきます。

$ sudo rm /usr/local/share/keyrings/chrome-archive-keyring.gpg
$ sudo rm /etc/apt/sources.list.d/chrome.list

↓のDbianのWikiによると、サードパーティリポジトリの公開鍵を保存するのに/etc/apt/trusted.gpg.d/は使ってはならないと書いてあるので、今後この動作は修正されるかもしれません。
DebianRepository/UseThirdParty - Debian Wiki

補足メモ

Ubuntuのリポジトリの基礎知識

APTについて理解を深めるためにUbuntuのリポジトリのWikiが参考になりました。
Repositories - Community Help Wiki
Repositories/CommandLine - Community Help Wiki

読んでみると、Ubuntu開発者としては、できるだけサードパーティーリポジトリを使ってほしくないことが分かります。サードパーティーリポジトリはスパイウェアだらけだとも言っています。

It's important to know that most of the tools you'll want to use in Ubuntu are already in Ubuntu's repositories. You can go search the internet for packages, or even source code, for others, but these will be more difficult to install and won't, most of the time, integrate as well with your system.

So now you know: no more endless searching looking for spyware-infested shareware and freeware. The vast majority of useful software available for Linux is pre-packaged for you.

手動で検証してみる。

第675回 apt-keyはなぜ廃止予定となったのか:Ubuntu Weekly Recipe|gihyo.jp … 技術評論社

↑の記事にあるようにAPTはGPGを用いて公開鍵でリポジトリを検証しています。この記事ではgpgvコマンドを使って、手動で検証する方法も紹介しています。

ただ、その際使用するInReleaseファイルの場所が載っていませんでした。

調べてみると、InReleaseファイルの場所は下記記事に載っていました。

DebianRepository/Format - Debian Wiki
日本語解説が
メモ:Debianレポジトリのフォーマット - Technically, technophobic.にあります。

つまり、source.listの記載が

deb http://example.com ubuntu main

であれば、InReleaseファイルはhttp://example.com/dists/ubuntu/InReleaseにあります。

deb http://example.com main

であれば、InReleaseファイルはhttp://example.com/InReleaseにあります。

つまり、InReleaseファイルは、yarnであればhttps://dl.yarnpkg.com/debian/dists/stable/InReleaseで、google-chromeであればhttp://dl.google.com/linux/chrome/deb/dists/stable/InReleaseです。

なので、下記コマンドでInReleasファイルをダウンロードすることができます。

$ wget https://dl.yarnpkg.com/debian/dists/stable/InRelease

debファイルでの直接インストール

例えばchromeであれば、https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.debをダウンロードしてapt install debファイルで直接インストールできますが、依存するパッケージは自動的にはインストールしてくれないようです。

↑のgpgで公開鍵を保存して、aptでインストールする方法であれば依存するパッケージも自動的にダウンロードしてくれるようです。
(または、gdebiというパッケージを使えば依存関係を解決できるようです。)

4
3
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
4
3