1.2. SSH 鍵ペアの生成


ローカルシステム上で SSH キーペアを生成し、公開鍵を OpenSSH サーバーにコピーすることで、パスワードなしでログインできるようになります。鍵を作成する各ユーザーは、この手順を実行する必要があります。

システムを再インストールした後も生成されたキーペアを保持するには、新しいキーを作成する前に ~/.ssh/ ディレクトリーをバックアップします。再インストール後に、このディレクトリーをホームディレクトリーにコピーします。これは、(root を含む) システムの全ユーザーで実行できます。

前提条件

  • OpenSSH サーバーに鍵を使用して接続するユーザーとしてログインしている。
  • OpenSSH サーバーが鍵ベースの認証を許可するように設定されている。

手順

  1. 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 鍵で機能しないことに注意してください。

  2. 公開鍵をリモートマシンにコピーします。

    $ 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 オプションを付けて使用します。

検証

  1. 鍵ファイルを使用して OpenSSH サーバーにログインします。

    $ ssh -o PreferredAuthentications=publickey <username>@<ssh-server-example.com>
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

Red Hat ドキュメントについて

Legal Notice

Theme

© 2026 Red Hat
トップに戻る