業務用エアコン関連の技術情報、エラーコード、環境問題対策に関する別サイト「エアコンの安全な修理・適切なフロン回収」

Debian12.14 : 公開鍵暗号方式を用いたSSH接続

 公開鍵、秘密鍵ペアの作成

OpenSSH 使って Linux サーバに接続するユーザの公開鍵と秘密鍵のペアを作成します。
鍵ペアの作成は ssh-keygen をを使います。
今回はSSH プロトコル Version 2 で利用する RSA 暗号を利用した鍵セットを作成します。
公開鍵と秘密鍵のペアの作成はリモートログインするユーザ権限(huong)で実行します。
作成先・ファイル名を指定しなければ、 /home/(ユーザ名)/.ssh/ に id_ed25519, id_ed25519.pub が作成される。 途中、鍵用のパスワードも入力する。

# su - huong
huong@Lepard:~$ ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/huong/.ssh/id_ed25519): 
Created directory '/home/huong/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/huong/.ssh/id_ed25519
Your public key has been saved in /home/huong/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:LxmtAES5mRgxHcnXcBX0ryyW+1Iy25PC7Zmy3bDXevU huong@Lepard
The key's randomart image is:
+--[ED25519 256]--+
|  o=++.o.o+.     |
|  .o= ...  .     |
|   o.=      .    |
|  . +.   .   .   |
|      . S .   .  |
|       . =oo..  .|
|        +.=B+. .o|
|         o*+=*. E|
|          oBBooo |
+----[SHA256]-----+
$ chmod 700 ~/.ssh
$ cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys

作成した秘密鍵id_ed25519 をwinSCPでWindowsの[c:\USERS\ユーザー\.ssh\]配下に保存する
Winscpを起動する
Host name  :  サーバーのIPアドレス
Port number : SSHポート
User name : サーバーのログインユーザー
Password : 同上ユーザーのパスワード
を入力し、「Save」

Site name : 任意の名前
「OK」クリック

該当サーバーを確認後、「Login」クリック

下記画面が出れば「Update」クリック

Passwordの欄にはログインユーザーのパスワード

接続されると、左欄サーバー側、右欄PC(Windows)側になる。
.sshディレクトリーにある"id_ed25519"ファイルを右欄Windowsの[c:\USERS\ユーザー\.ssh\]配下にコピーする

SSH 設定ファイルの編集

パスワード認証をできないようにするため、 SSH 設定ファイルを編集する。
今度は一般ユーザではなく、 su - で rootになり行う。

$ su -
Password:

# vi /etc/ssh/sshd_config
# 57行目あたり : コメント解除し、noに変更
PasswordAuthentication no

sshサービス再起動

# systemctl restart sshd

Tabby Terminalで接続する方法
オプション[-i c:\USERS\xxxxx\.ssh\id_ed25519]でkeyを指定する

C:\Users\xxxxx>ssh huong@192.168.11.83 -p 2244 -i c:\USERS\xxxxx\.ssh\id_ed25519

Enter passphrase for key 'c:\USERS\mhori.ssh\id_ed25519':[公開鍵の作成時作成したパスワード]
Linux Lepard 6.1.0-48-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.172-1 (2026-05-15) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed May 27 12:29:08 2026 from 192.168.11.6

PuTTYgen を使った秘密鍵の作成

Winscpを起動し、該当サーバーを選択後、「Tools」から[Run Puttygen] を起動する

Loadをクリック

[ ファイルを開くダイアログ ] が開くのでファイルの種類を [ All Files (*.*) ] に変更して Linux サーバから転送した秘密鍵 id_ed25519 を読み込みます。

パスワードは上記「公開鍵、秘密鍵ペアの作成」で設定したパスワード

「Save private key」をクリック

Windows側に先ほど保存した「id_ed25519」と同じところに拡張子「.ppk」として「id_ed25519.ppk」の名称で保存

該当サーバーを選択し、「Edit」クリック

「Advanced」をクリック

「Authentication」メニューを開き、「Private key file」に先ほどWindowsに保存した「id_ed25519.ppk」を指定する

「Save」をクリック

「Login」をクリック

パスワードは上記「公開鍵、秘密鍵ペアの作成」で設定したパスワード

Tabby Terminalで接続する場合は以下のように

C:\Users\xxxxx>ssh huong@192.168.11.83 -p 2244 -i c:\USERS\xxxxx\.ssh\id_ed25519.ppk
Enter passphrase for key 'c:\USERS\xxxxx\.ssh\id_ed25519.ppk': ←公開鍵作成の時に指定したパスワード

もし、下記のようなエラーが出た場合は秘密鍵をOpenSSH形式に変換する

C:\Users\xxxxx>ssh huong@192.168.11.83 -p 2244 -i c:\USERS\xxxxx\.ssh\id_ed25519.ppk
Load key "c:\USERS\xxxxx\.ssh\id_ed25519.ppk": invalid format

[WinSCP]を起動し、該当サーバーを選択後、[Run PuTTygen]を起動し、[Conversions]タブの[Import key]で秘密キーをインポートする

インポートした秘密キーをOpenSSH形式に変換して、別名で保存する
公開鍵作成の時に指定したパスワード

openssh形式に変更した[id_ed25519_2.ppk]を利用し、下記の通り接続できる

C:\Users\xxxxx>ssh huong@192.168.11.83 -p 2244 -i c:\USERS\xxxxx\.ssh\id_ed25519_2.ppk
Enter passphrase for key 'c:\USERS\mhori.ssh\id_ed25519_2.ppk':公開鍵作成の時に指定したパスワード
Linux Lepard 6.1.0-48-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.172-1 (2026-05-15) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed May 27 13:07:03 2026 from 192.168.11.6