この記事では、openSSH上でSSHエージェントによるSSH公開鍵認証を利用して、パスワードを入力せずにsshとscpを行う方法を解説します
SSH 鍵ベースの認証には、2 つのレベルのセキュリティーがあります。ログインするには、秘密鍵とパスフレーズの両方が必要です。どちらかが侵害されたとしても、ログインには両方が必要なため、攻撃者はアカウントにログインできません。これは、パスワードが侵害された場合に攻撃者がシステムにアクセスできる一般的なパスワード ベースの認証よりもはるかに優れています。
パスワードを入力せずに ssh と scp を実行するには、2 つの方法があります:
<オール>
次の 8 つの手順では、openSSH システムでパスワードを入力せずにローカル ホストからリモート ホストに SSH および SCP を実行する方法について説明します
1. local-host と remote-host が openSSH を実行していることを確認します
[local-host]$ ssh -V OpenSSH_4.3p2, OpenSSL 0.9.8b 04 May 2006 [remote-host]$ ssh -V OpenSSH_4.3p2, OpenSSL 0.9.8b 04 May 2006
2. ssh-keygen を使用してローカル ホストでキー ペアを生成します
[local-host]$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):<Hit enter> Enter passphrase (empty for no passphrase): <Enter your passphrase here> Enter same passphrase again:<Enter your passphrase again> Your identification has been saved in /home/jsmith/.ssh/id_rsa. Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub. The key fingerprint is: 31:3a:5d:dc:bc:81:81:71:be:31:2b:11:b8:e8:39:a0 jsmith@local-host
公開鍵と秘密鍵は通常、ホーム ディレクトリの下の .ssh フォルダーに保存されます。この例では、/home/jsmith/.sshd の下にあります。秘密鍵は誰とも共有しないでください。
デフォルトでは、openSSH の ssh-keygen は RSA キー ペアを生成します。次を使用して DSA キー ペアを生成することもできます:ssh-keygen -t dsa コマンド。
3.リモートホストに公開鍵をインストールします。
公開鍵の内容をローカル ホストからコピーし、リモート ホストの /home/jsmith/.ssh/authorized_keys に貼り付けます。 /home/jsmith/.ssh/authorized_keys に既に他の公開鍵がある場合は、これを末尾に追加できます。 remote-host のホーム ディレクトリの下に .ssh ディレクトリが存在しない場合は、作成してください。
[remote-host]$ vi ~/.ssh/authorized_keys
ssh-rsa ABIwAAAQEAzRPh9rWfjZ1+7Q369zsBEa7wS1RxzWR jsmith@local-host
簡単に言うと、local-host:/home/jsmith/.ssh/id_rsa.pub を remote-host:/home/jsmith/.ssh/authorized_keys にコピーします。
4.リモートホストの .ssh ディレクトリに適切な権限を付与してください。
[remote-host]$ chmod 755 ~/.ssh
[remote-host]$ chmod 644 ~/.ssh/authorized_keys
5. SSH キー認証を使用してローカル ホストからリモート ホストにログインし、正常に動作するかどうかを確認します。
[local-host]$ <You are on local-host here> [local-host]$ ssh -l jsmith remote-host
Enter passphrase for key '/home/jsmith/.ssh/id_rsa': <Enter your passphrase here>
Last login: Sat Jun 07 2008 23:03:04 -0700 from 192.168.1.102
No mail. [remote-host]$ <You are on remote-host here>
6.ローカル ホストで SSH エージェントを起動して、パスフレーズを何度も入力することなく ssh と scp を実行します。
SSH エージェントが既に実行されているかどうかを確認します。実行されていない場合は、次のように開始します。
[local-host]$ ps -ef | grep ssh-agent
511 9789 9425 0 00:05 pts/1 00:00:00 grep ssh-agent
[local-host]$ ssh-agent $SHELL
[local-host]$ ps -ef | grep ssh-agent
511 9791 9790 0 00:05 ? 00:00:00 ssh-agent /bin/bash
511 9793 9790 0 00:05 pts/1 00:00:00 grep ssh-agent
7.ローカル ホスト上の SSH エージェントに秘密鍵を読み込みます。
[local-host]$ ssh-add
Enter passphrase for /home/jsmith/.ssh/id_rsa: <Enter your passphrase here>
Identity added: /home/jsmith/.ssh/id_rsa (/home/jsmith/.ssh/id_rsa)
以下は、ssh-add で利用可能なさまざまなオプションです:
- ssh-add
:特定の鍵ファイルを読み込みます。 - ssh-add -l:ssh エージェントに読み込まれたすべてのキーを一覧表示します。
- ssh-add -d
:ssh エージェントから特定のキーを削除します - ssh-add -D:すべてのキーを削除
8.パスワードを入力せずに、ローカル ホストからリモート ホームへの SSH または SCP を実行します。
[local-host]$<You are on local-host here> [local-host]$ ssh -l jsmith remote-host
Last login: Sat Jun 07 2008 23:03:04 -0700 from 192.168.1.102
No mail. <ssh did not ask for passphrase this time> [remote-host]$ <You are on remote-host here>
The Geek Stuff に関するニュースを広めるのを手伝ってください。
この記事に関するご意見やご感想をお寄せください。この投稿が気に入ったら、下のリンクから del.icio.us または Digg に追加して、「The Geek Stuff」ブログについて広めていただければ幸いです。