Ubuntu 26.04

OpenSSH : SSH 鍵ペア認証2026/04/24

 

クライアント用に 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:mU2GVu3ZgC6Xcab6XQ2KIazz9JOeDzQBOTdyxwHet7E ubuntu@client.srv.world
The key's randomart image is:
.....
.....

ubuntu@client:~$
ll ~/.ssh

total 16
drwx------ 2 ubuntu ubuntu 4096 Apr 24 07:16 ./
drwxr-x--- 4 ubuntu ubuntu 4096 Apr 24 07:09 ../
-rw------- 1 ubuntu ubuntu    0 Apr 24 04:15 authorized_keys
-rw------- 1 ubuntu ubuntu  464 Apr 24 07:16 id_ed25519
-rw-r--r-- 1 ubuntu ubuntu  105 Apr 24 07:16 id_ed25519.pub

# 作成した SSH 公開鍵を ログインしたいサーバーへコピー

ubuntu@client:~$
ssh-copy-id dlp.srv.world

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/ubuntu/.ssh/id_ed25519.pub"
/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:

umber 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行目 : コメント解除してパスワード認証不可に変更

PasswordAuthentication
no
# 85行目 : 以下の設定になっているか確認

KbdInteractiveAuthentication no
# 以下のファイルが存在する場合は 同様に内容を変更 または ファイル自体を削除

root@dlp:~#
cat /etc/ssh/sshd_config.d/50-cloud-init.conf

PasswordAuthentication yes
root@dlp:~#
rm /etc/ssh/sshd_config.d/50-cloud-init.conf

root@dlp:~#
systemctl restart ssh

Windows クライアントからの SSH 鍵認証 #1

 

Windows クライアントから鍵認証方式でログインする場合の設定です。Putty を例にします。
事前に こちらを参考に、秘密鍵を Windows クライアントに転送しておいてください。

[3] Puttyの公式サイト より [Puttygen.exe] をダウンロードして [Putty.exe] と同じフォルダに保管して実行します。以下の画面が表示されたら [Load] ボタンをクリックします。
[4] 事前に SSH サーバーから転送しておいた秘密鍵を指定すると、パスフレーズを求められるので入力して応答します。(ノーパス設定の場合は不要)
[5] パスフレーズでの応答が完了すると以下の画面になります。ここで [Save private key] をクリックして、任意のフォルダに任意の名前で保存します。 当例では [priv-key] というファイル名で [Putty.exe] と同じフォルダに保存しています。
[6] Putty 本体を起動し、左メニュー [Connection] - [SSH] - [Auth] - [Credentials] で、作成した Private Key ファイルを選択します。
[7] 左メニュー [Session] に戻り、接続するホストの名前または IP アドレスを入力して、接続します。
[8] 鍵を作成したユーザーでログインしようとすると、以下のようにパスフレーズを求められ、設定したパスフレーズを入力するとログインできます。

Windows クライアントからの SSH 鍵認証 #2

[9] こちらのリンク先に記載の通り、Windows には OpenSSH クライアントが標準搭載されているため、Putty 等のクライアントソフトウェアを用意しなくとも、サーバー側で生成した鍵ペアの秘密鍵の方を Windows クライアントへ転送し、Windows 側でログオン中のユーザーディレクトリ配下の [.ssh] フォルダ内に秘密鍵を格納するのみで、鍵ペアでの認証が可能です。
関連コンテンツ