1.2. SSH 鍵ペアの生成
ローカルシステム上で SSH キーペアを生成し、公開鍵を OpenSSH サーバーにコピーすることで、パスワードなしでログインできるようになります。鍵を作成する各ユーザーは、この手順を実行する必要があります。
システムを再インストールした後も生成されたキーペアを保持するには、新しいキーを作成する前に ~/.ssh/ ディレクトリーをバックアップします。再インストール後に、このディレクトリーをホームディレクトリーにコピーします。これは、(root を含む) システムの全ユーザーで実行できます。
前提条件
- OpenSSH サーバーに鍵を使用して接続するユーザーとしてログインしている。
- OpenSSH サーバーが鍵ベースの認証を許可するように設定されている。
手順
ECDSA 鍵ペアを生成します。
$ ssh-keygen -t ecdsa Generating public/private ecdsa key pair. Enter file in which to save the key (/home/<username>/.ssh/id_ecdsa): Enter passphrase (empty for no passphrase): <password> Enter same passphrase again: <password> Your identification has been saved in /home/<username>/.ssh/id_ecdsa. Your public key has been saved in /home/<username>/.ssh/id_ecdsa.pub. The key fingerprint is: SHA256:Q/x+qms4j7PCQ0qFd09iZEFHA+SqwBKRNaU72oZfaCI <username>@<localhost.example.com> The key's randomart image is: +---[ECDSA 256]---+ |.oo..o=++ | |.. o .oo . | |. .. o. o | |....o.+... | |o.oo.o +S . | |.=.+. .o | |E.*+. . . . | |.=..+ +.. o | | . oo*+o. | +----[SHA256]-----+代替案: ECDSA の代わりに、以下を生成できます。
-
RSA キーペア (パラメーターを指定せずに
ssh-keygenコマンドを使用) Ed25519 キーペア (
ssh-keygen -t ed25519コマンドを入力)Ed25519 アルゴリズムは FIPS-140 に準拠しておらず、FIPS モードでは OpenSSH は Ed25519 鍵で機能しないことに注意してください。
-
RSA キーペア (パラメーターを指定せずに
公開鍵をリモートマシンにコピーします。
$ ssh-copy-id <username>@<ssh-server-example.com> /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed <username>@<ssh-server-example.com>'s password: … Number of key(s) added: 1 Now try logging into the machine, with: "ssh '<username>@<ssh-server-example.com>'" and check to make sure that only the key(s) you wanted were added.<username>@<ssh-server-example.com>は、認証情報に置き換えます。ssh-agentを使用しない場合、このコマンドは最後に変更された~/.ssh/id*.pub公開鍵をコピーします。別の公開鍵ファイルを使用する場合、またはssh-agentによってメモリーに保存されている鍵よりもファイルからの鍵を優先する場合は、ssh-copy-idコマンドに-iオプションを付けて使用します。
検証
鍵ファイルを使用して OpenSSH サーバーにログインします。
$ ssh -o PreferredAuthentications=publickey <username>@<ssh-server-example.com>