OpenSSH : SSH 鍵ペア認証2025/12/18 |
|
クライアント用に SSH 秘密鍵、サーバー用に SSH 公開鍵を作成し、鍵ペアによる認証でログインできるようにします。 |
|
| [1] | 鍵ペアはユーザー各々で作成します。よって、鍵ペアを作成するユーザーでサーバー側にログインして作業します。 |
|
# 鍵ペア作成 freebsd@dlp:~ $ ssh-keygen Generating public/private ed25519 key pair. Enter file in which to save the key (/home/freebsd/.ssh/id_ed25519): # 変更の必要がなければ空 Enter Enter passphrase for "/home/freebsd/.ssh/id_ed25519" (empty for no passphrase): # パスフレーズ設定 (ノーパス設定にする場合は空 Enter) Enter same passphrase again: Your identification has been saved in /home/freebsd/.ssh/id_ed25519 Your public key has been saved in /home/freebsd/.ssh/id_ed25519.pub The key fingerprint is: SHA256:7729Hutz23j6gRSZWpU4esNDsrjq+aVj+eh0aQg0fYE freebsd@dlp.srv.world The key's randomart image is: ..... .....freebsd@dlp:~ $ ls -l ~/.ssh total 18 -rw------- 1 freebsd freebsd 464 Dec 18 09:17 id_ed25519 -rw-r--r-- 1 freebsd freebsd 103 Dec 18 09:17 id_ed25519.pub -rw------- 1 freebsd freebsd 846 Dec 18 08:45 known_hosts -rw-r--r-- 1 freebsd freebsd 98 Dec 18 08:45 known_hosts.oldfreebsd@dlp:~ $ mv ~/.ssh/id_ed25519.pub ~/.ssh/authorized_keys |
| [2] | サーバー側で作成した秘密鍵をクライアント側にファイル転送すると、そのクライアントから対象サーバーに、鍵認証でログイン出来るようになります。 |
|
freebsd@node01:~ $
mkdir ~/.ssh freebsd@node01:~ $ chmod 700 ~/.ssh
# サーバーで作成した秘密鍵を SCP で持ってくる freebsd@node01:~ $ scp freebsd@dlp.srv.world:/home/freebsd/.ssh/id_ed25519 ~/.ssh/ (freebsd@dlp.srv.world) Password for freebsd@dlp.srv.world: id_rsa 100% 2655 2.8MB/s 00:00freebsd@node01:~ $ ssh freebsd@dlp.srv.world Enter passphrase for key '/home/freebsd/.ssh/id_ed25519': # 設定したパスフレーズ Last login: Thu Dec 18 09:22:22 2025 from node01.srv.world FreeBSD 15.0-RELEASE (GENERIC) releng/15.0-n280995-7aedc8de6446 Welcome to FreeBSD! ..... ..... freebsd@dlp:~ $ # ログインできた |
| [3] | なお、鍵認証にした場合、以下のように SSH サーバーへのパスワード認証を禁止すると、よりセキュアな環境とすることができます。 |
|
root@dlp:~ #
vi /etc/ssh/sshd_config # 61行目 : コメント解除 PasswordAuthentication no # 68行目 : コメント解除してパスワード認証不可に変更 KbdInteractiveAuthentication no
service sshd restart |
Windows クライアントからの SSH 鍵認証 #1 |
|
Windows クライアントから鍵認証方式でログインする場合の設定です。Putty を例にします。 |
|
| [4] | Puttyの公式サイト より [Puttygen.exe] をダウンロードして [Putty.exe] と同じフォルダに保管して実行します。以下の画面が表示されたら [Load] ボタンをクリックします。 |
|
| [5] | 事前に SSH サーバーから転送しておいた秘密鍵を指定すると、パスフレーズを求められるので入力して応答します。(ノーパス設定の場合は不要) |
|
| [6] | パスフレーズでの応答が完了すると以下の画面になります。ここで [Save private key] をクリックして、任意のフォルダに任意の名前で保存します。 当例では [priv-key] というファイル名で [Putty.exe] と同じフォルダに保存しています。 |
|
| [7] | Putty 本体を起動し、左メニュー [Connection] - [SSH] - [Auth] - [Credencials] で、作成した Private Key ファイルを選択します。 |
|
| [8] | 左メニュー [Session] に戻り、接続するホストの名前または IP アドレスを入力して、接続します。 |
|
| [9] | 鍵を作成したユーザーでログインしようとすると、以下のようにパスフレーズを求められ、設定したパスフレーズを入力するとログインできます。 |
|
Windows クライアントからの SSH 鍵認証 #2 |
| [10] | こちらのリンク先に記載の通り、Windows には OpenSSH クライアントが標準搭載されているため、Putty 等のクライアントソフトウェアを用意しなくとも、サーバー側で生成した鍵ペアの秘密鍵の方を Windows クライアントへ転送し、Windows 側でログオン中のユーザーディレクトリ配下の [.ssh] フォルダ内に秘密鍵を格納するのみで、鍵ペアでの認証が可能です。 |
|
| Sponsored Link |
|
|