OpenSSH : SSH 鍵ペアでの認証2025/04/21 |
クライアント用に SSH 秘密鍵、サーバー用に SSH 公開鍵を作成し、鍵ペアによる認証でログインできるようにします。 |
|
[1] | 鍵ペアはユーザー各々で作成します。よって、鍵ペアを作成するユーザーで、任意のクライアントコンピューターにログインして作業します。 |
# 鍵ペア作成 ubuntu@client:~$ ssh-keygen Generating public/private ed25519 key pair. Enter file in which to save the key (/home/ubuntu/.ssh/id_ed25519): # 変更の必要がなければ空 Enter Enter passphrase for "/home/ubuntu/.ssh/id_ed25519" (empty for no passphrase): # パスフレーズ設定 (ノーパス設定にする場合は空 Enter) Enter same passphrase again: Your identification has been saved in /home/ubuntu/.ssh/id_ed25519 Your public key has been saved in /home/ubuntu/.ssh/id_ed25519.pub The key fingerprint is: SHA256:saW0mmPf1r0HjphkQ1+VGsCK1zqr4U7VUXKuV57pSkY ubuntu@client.srv.world The key's randomart image is: ..... .....ubuntu@client:~$ ll ~/.ssh total 16 drwx------ 2 ubuntu ubuntu 4096 Apr 21 00:44 ./ drwxr-x--- 4 ubuntu ubuntu 4096 Apr 17 23:53 ../ -rw------- 1 ubuntu ubuntu 0 Apr 17 23:49 authorized_keys -rw------- 1 ubuntu ubuntu 444 Apr 21 00:44 id_ed25519 -rw-r--r-- 1 ubuntu ubuntu 95 Apr 21 00:44 id_ed25519.pub # 作成した SSH 公開鍵を ログインしたいサーバーへコピー ubuntu@client:~$ ssh-copy-id dlp.srv.world /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys ubuntu@dlp.srv.world's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'dlp.srv.world'" and check to make sure that only the key(s) you wanted were added. # 公開鍵をコピーしたサーバーへ鍵認証でログインできるか確認 ubuntu@client:~$ ssh dlp.srv.world hostname
Enter passphrase for key '/home/ubuntu/.ssh/id_ed25519': # パスフレーズを設定した場合は入力
dlp.srv.world
|
[2] | なお、鍵ペア認証にした場合、以下のように SSH サーバー側でパスワード認証を禁止すると、よりセキュアな環境とすることができます。 |
root@dlp:~#
vi /etc/ssh/sshd_config # 78, 83行目 : コメント解除してパスワード認証不可に変更 PasswordAuthentication no ..... ..... KbdInteractiveAuthentication no
systemctl restart ssh |
Windows クライアントからの SSH 鍵ペア認証 #1
|
Windows クライアントから SSH 鍵ペア認証でログインする場合の設定です。 |
|
[3] | Putty に同梱されている [Puttygen.exe] を実行します。([Putty.exe] と同じフォルダに保管) 同梱されていない場合は、公式サイト (www.chiark.greenend.org.uk/~sgtatham/putty/) より、[Puttygen.exe] をダウンロードして [Putty.exe] と同じフォルダに保管します。 [Puttygen.exe] 起動後、以下の画面で [Load] ボタンをクリックします。 |
![]() |
[4] | 事前に SSH サーバーから転送しておいた SSH 秘密鍵を指定すると、パスフレーズを求められるので入力して応答します。(ノーパス設定の場合は不要) |
![]() |
[5] | パスフレーズでの応答が完了すると以下の画面になります。[Save private key] ボタンをクリックして、任意のフォルダーに任意のファイル名で保存します。 |
![]() |
[6] | Putty 本体を起動し、左メニューで [Connection] - [SSH] - [Auth] - [Credencials] と開き、下段のフィールドに、作成した [Private key] ファイルを選択します。 |
![]() |
[7] | 左メニューの [Session] に戻り、接続するホストの名前または IP アドレスを入力して、接続します。 |
![]() |
[8] | SSH 鍵ペアを作成したユーザーでログインしようとすると、以下のようにパスフレーズを求められ、設定したパスフレーズを入力するとログインできます。 |
![]() |
Windows クライアントからの SSH 鍵ペア認証 #2
|
[9] | Windows 11 の場合は OpenSSH クライアントが標準搭載されているため、Putty 等の SSH クライアントソフトウェアを用意しなくとも、サーバー側で生成した鍵ペアの秘密鍵の方を Windows クライアントへ転送し、Windows 側でログオン中のユーザーディレクトリ配下の [.ssh] フォルダ内に秘密鍵を格納するのみで、SSH 鍵ペアでの認証が可能です。 |
![]() |
Sponsored Link |