Keychain

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Keychain and the translation is 70% complete.

この文書では SSH 共有鍵をキーチェーンプログラムとともに使用する方法を記述します。公開鍵暗号方式について基本的な知識を持っていることを仮定します。

キーチェーンは、ユーザがパスフレーズを 1 回入力するだけで長く続くセッションを行えるようにする、ssh-agent および ssh-add に対するフロントエンドです。キーチェーンはまた、スクリプトが SSH 接続にアクセスできるようにするためにも使われます。

背景

目下の課題

すべてのシステムに、それぞれのログインパスワードを入力する必要があるというのは、特に多数のシステムが管理されているときには不便なものです。システム管理者の中には、スクリプトまたは cron ジョブが必要とする ssh 接続を使うための簡便な方法を必要としている者もいるでしょう。いずれにせよ、この問題には解決方法があり、その第一となるものは公開鍵認証です。

公開鍵認証はどのように働くか?

あるクライアントがあるサーバの ssh デーモンに接続したいとします。クライアントはまず鍵対を生成し、サーバに公開鍵を渡します。その後、クライアントが接続を試みたときは、サーバはその公開鍵で暗号化されたチャレンジを送信します。対応する秘密鍵の所有者 (このクライアント) だけがそれを復号できるので、正しいレスポンスを返し、これにより認証が成功します。

公開鍵認証の使い方

鍵対を生成する

最初のステップは鍵対を生成することです。これを行うには、ssh-keygen コマンドを使用してください:

user $ssh-keygen

デフォルト値を採用し、強いパスフレーズを入力してください。

警告
強いパスフレーズを選択してください。この鍵を root ログインに使用する場合は特に!

生成が終わると、秘密鍵は ~/.ssh/id_rsa に、公開鍵は ~/.ssh/id_rsa.pub に配置されているはずです。これで公開鍵を遠隔のホストにコピーする準備ができました。

秘密鍵のパスフレーズの追加または変更は、以下のようにして行えます:

user $ssh-keygen -p -f ~/.ssh/id_rsa

サーバを準備する

~/.ssh/id_rsa.pub ファイルは、sshd を実行しているサーバにコピーされている必要があります。これを、遠隔サーバ上の、接続したいユーザに所属する ~/.ssh/authorized_keys ファイルに追加する必要があります。サーバへの ssh アクセスがインフラ係によって確保されたら、遠隔サーバへの公開鍵を利用した自動ログインをセットアップするために、次のステップを使用できます:

user $ssh-copy-id -i ~/.ssh/id_rsa.pub server_user@server

ssh-copy-id はこのステップのためのラッパースクリプトです。これを利用できない場合は、次のステップを利用できます:

user $scp ~/.ssh/id_rsa.pub server_user@server:~/myhost.pub
user $ssh server_user@server "cat ~/myhost.pub >> ~/.ssh/authorized_keys"
user $ssh server_user@server "cat ~/.ssh/authorized_keys"

最後の行のコマンドの出力は ~/.ssh/authorized_keys ファイルの内容を表示するはずです。出力が正しそうか確認してください。

構成をテストする

理論上は、以上のことがすべてうまく行き、サーバの sshd デーモンで公開鍵認証が許可されていれば (そう設定することができます)、すでにパスワードを入力せずにサーバへ ssh アクセスができるようになっているはずです。まだ上で使用したパスワードを使ってクライアント上の秘密鍵を復号する必要がありますが、これをサーバ上のユーザアカウントのパスワードと混同しないでください。

user $ssh <server_user>@<server>

上のコマンドで、id_rsa に対するパスフレーズを求められ、その後サーバ上のユーザ <server_user> として ssh を介したアクセスが許可されるはずです。されない場合は <server_user> としてログインし、~/.ssh/authorized_keys の内容が、各項目 (公開鍵を表します) が一行ごとに書かれているか確認してください。sshd 設定を確認して、利用可能な場合は公開鍵認証の使用を許可する設定になっているか確認するのもいい考えでしょう。

この時点で、もしかするとこう考えているかもしれません: 「パスワードを別のパスワードで置き換えただけ?これに何の意味が?」大丈夫、これを利用して、一回だけパスフレーズを入力すれば複数回のログインで (復号された) 鍵を再利用できるような方法を、次の節で説明します。

公開鍵認証をより便利にする

ssh-agent による典型的な鍵管理

次のステップは、秘密鍵を一度だけ復号して、パスワード入力なしに ssh を自由に使えるようにすることです。ssh-agent プログラムはまさにそのためにあります。

ssh-agent は通常、X セッションの開始時に、または ~/.bash_profile のようなシェルスタートアップスクリプトから開始されます。ssh-agent は UNIX ソケットを作成して適切な環境変数を設定し、後続のアプリケーションがそのソケットに接続してサービスの利益を受ける、という流れで機能します。ここから明らかなように、後続のすべての X アプリケーションで復号された秘密鍵の集合を利用するには、X セッションの親プロセス内で開始しなければ意味がありません。

user $eval `ssh-agent`
メモ
この ssh-agent はキルされるまで復号された鍵を保持します。鍵の保持期間を設定するには、man ssh-agent の記述に従って -t 引数を使用してください。

ssh-agent を実行すると、実行中の ssh-agent の PID を出力して、SSH_AUTH_SOCKSSH_AGENT_PID という名前の環境変数を設定するはずです。さらに、自身のコレクションに ~/.ssh/id_rsa を自動で追加して、対応するパスフレーズをユーザに求めるはずです。実行中の ssh-agent に追加する必要のある秘密鍵が他にある場合は、ssh-add コマンドを使用してください:

user $ssh-add somekeyfile

ここからが魔法です。秘密鍵が復号され準備できたら、パスワードを入力(公開鍵が設定された) サーバに ssh してください:

user $ssh server

ssh-agent をシャットダウンする (後でまたパスフレーズの入力を必要とする) ためには:

user $ssh-agent -k
メモ
初期設定に試行錯誤しているときなどは特に、複数の ssh-agent プロセスが実行中のままになることがあります。これらのプロセスは他のプロセスと同様に、killall ssh-agent を実行してキルすることができます。

ssh-agent をさらに便利に使うために、次のキーチェーンの使用についての節に進んでください。キーチェーンは自身で ssh-agent セッションを処理するので、実行中の ssh-agent をキルしておいてください。

Squeezing the last drop of convenience out of ssh-agent

Keychain will allow to reuse an ssh-agent between logins, and optionally prompt for passphrases each time the user logs in. Let's emerge it first:

root #emerge --ask net-misc/keychain

Assuming that was successful, keychain can now be used.

Add the following to the shell initialization file (~/.bash_profile, ~/.zshrc, or similar) to enable it:

ファイル ~/.bash_profilebash でキーチェーンを有効化する
keychain ~/.ssh/id_rsa
. ~/.keychain/${HOSTNAME}-sh
. ~/.keychain/${HOSTNAME}-sh-gpg
ファイル ~/.zshrczsh でキーチェーンを有効化する
keychain ~/.ssh/id_rsa
. ~/.keychain/${HOST}-sh
. ~/.keychain/${HOST}-sh-gpg
ヒント
More keys can be appended to the command line as desired. Also, to have it ask for passphrases each time a shell is spawned, add the --clear option.
メモ
When not using Bash or zsh, check the EXAMPLES section of man keychain for examples of use in other shells. The idea is to get those commands to run each time a shell is used.

Now test it. First make sure the ssh-agent processes from the previous section are killed, then start up a new shell, usually by just logging in, or spawning a new terminal. It should prompt for the password for each key specified on the command line.

All shells opened after that point should reuse the ssh-agent, allowing to use passwordless SSH connections over and over.

Using keychain with Plasma 5

Plasma 5 users, instead of using ~/.bash_profile, can let Plasma manage ssh-agent for them. In order to do so, edit /etc/xdg/plasma-workspace/env/10-agent-startup.sh, which is read during Plasma's startup, and /etc/xdg/plasma-workspace/shutdown/10-agent-shutdown.sh, which is executed during its shutdown.

Here is how one could edit those files:

ファイル /etc/plasma/startup/10-agent-startup.shPlasma 5 用に編集する
SSH_AGENT=true
ファイル /etc/xdg/plasma-workspace/shutdown/10-agent-shutdown.shPlasma 5 用に編集する
if [ -n "${SSH_AGENT_PID}" ]; then
  eval "$(ssh-agent -k)"
fi

Now, all that has to be done is launch a terminal of choice, like kde-apps/konsole, and load the right set of keys to use. For example:

user $keychain ~/.ssh/id_rsa

The keys will be remembered until the end of the Plasma session (or until the ssh-agent process is killed manually).

Alternatively use KWallet with kde-plasma/ksshaskpass under Plasma 5

You can also have Plasma automatically ask you for your passphrase upon desktop login. Emerge kde-plasma/ksshaskpass, which will set up an environment variable to use the ksshaskpass application whenever ssh-add is run outside of a terminal. Then create a script as follows, and install it via the Plasma -> System Settings -> Startup and Shutdown -> Autostart.

ファイル ~/ssh.shCreate ssh.sh script
#!/bin/sh
ssh-add < /dev/null
メモ
Recent versions of plasma seem to require autostart scripts to have user-only permissions. You may need to chmod 700 ssh.sh before adding the script via the Autostart GUI

Concluding remarks

セキュリティの考慮事項

Of course, the use of ssh-agent may add a bit of insecurity to the system. If another user would gain access to a running shell, he could login to all of the servers without passwords. As a result, it is a risk to the servers, and users should be sure to consult the local security policy (if any). Be sure to take the appropriate measures to ensure the security of all sessions.

トラブルシューティング

Most of this should work pretty well, but if problems do come up, then the following items might be of assistance.

  • If connecting without ssh-agent does not seem to work, consider using ssh with the -vvv options to find out what's happening. Sometimes the server is not configured to use public key authentication, sometimes it is configured to ask for local passwords anyway! If that is the case, try using the -o option with ssh, or change the server's sshd_config.
  • If connecting with ssh-agent or keychain does not seem to work, then it may be that the current shell does not understand the commands used. Consult the man pages for ssh-agent and keychain for details on working with other shells.

関連項目

  • SSH — リモートマシンに安全にログインし、作業を行うためのユビキタスなツールです。

外部資料



This page is based on a document formerly found on our main website gentoo.org.
The following people contributed to the original document: Eric Brown, Marcelo Goes,
They are listed here because wiki history does not allow for any external attribution. If you edit the wiki article, please do not add yourself here; your contributions are recorded on each article's associated history page.