Keychain/ja

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

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

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

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

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

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

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

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

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

Testing the setup
Theoretically, if all went well, and the daemon on the server allows it (as this can be configured),  access without entering a password should now be possible on the server. The private key on the client will still need to be decrypted with the passphrase used previously, but this should not be confused with the password of the user account on the server.

It should have asked for a passphrase for, and then grant access via as the user   on the server. If not, login as, and verify that the contents of  has each entry (which is a public key) on a single line. It is also a good idea to check the sshd configuration to make sure that it allows to use public key authorization when available.

At this point, readers might be thinking, "What's the point, I just replaced one password with another?!" Relax, the next section will show exactly how we can use this to only enter the passphrase once and re-use the (decrypted) key for multiple logins.

Typical key management with ssh-agent
The next step is to decrypt the private key(s) once, and gain the ability to ssh freely, without any passwords. That is exactly what the program is for.

is usually started at the beginning of the X session, or from a shell startup script like. It works by creating a UNIX socket, and registering the appropriate environment variables so that all subsequent applications can take advantage of its services by connecting to that socket. Clearly, it only makes sense to start it in the parent process of an X session to use the set of decrypted private keys in all subsequent X applications.

When running, it should output the PID of the running ssh-agent, and also set a few environment variables, namely SSH_AUTH_SOCK and SSH_AGENT_PID. It should also automatically add to its collection and ask the user for the corresponding passphrase. If other private keys exist which need to be added to the running ssh-agent, use the command:

Now for the magic. With the decrypted private key ready, ssh into a (public key configured) server without entering any passwords:

In order to shut down ssh-agent (and as such require entry of the passphrase again later):

To get even more convenience from ssh-agent, proceed to the next section on using keychain. Be sure to kill the running ssh-agent as keychain will handle the sessions itself.

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:

Assuming that was successful, can now be used. Add the following to the file to enable it:

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, can let Plasma manage ssh-agent for them. In order to do so, edit, which is read during Plasma's startup, and , which is executed during its shutdown. Here is how one could edit those files:

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

The keys will be remembered until the end of the Plasma session (or until the 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.

Using keychain with Plasma 4
As above for Plasma 5 except replace with.

Security considerations
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.

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


 * If connecting without does not seem to work, consider using ssh with the   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   option with, or change the server's.
 * If connecting with or  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.

External resources

 * The official Keychain project page at Funtoo.org.
 * IBM developerWorks article series introducing the concepts behind Keychain.