SSH の公開鍵暗号方式設定
1.公開鍵、秘密鍵ペアの作成
OpenSSH 使って Linux サーバに接続するユーザの公開鍵と秘密鍵のペアを作成します。
鍵ペアの作成は ssh-keygen をを使います。
公開鍵と秘密鍵のペアの作成はリモートログインするユーザ権限で実行します。
作成先・ファイル名を指定しなければ、 /home/(ユーザ名)/.ssh/ に id_ed25519, id_ed25519.pub が作成される。 途中、鍵用のパスワードも入力する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# su – <user name> $ 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:nIJyadLBDmXJoozL8K2TGA6yXzPSV181nbT3R3+QSVE huong@localhost.localdomain The key's randomart image is: +--[ED25519 256]--+ | .o. o+E| | .+o ..+o| |o...o *o+| |+. + + . . . ++| |ooo.B . S . =| |=..*. o . . o| |++.o= . . | |o.+o + | | ... | +----[SHA256]-----+ |
1 2 3 |
$ cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys $ chmod 600 ~/.ssh/authorized_keys $ chmod 700 ~/.ssh/ |
2.秘密鍵ログイン設定
1 2 3 4 5 6 7 8 |
$ su - パスワード: # vi /etc/ssh/sshd_config ■49 行目あたり行頭の「#」を削除 変更前) #PubkeyAuthentication yes 変更後) PubkeyAuthentication yes |
SSH を再起動
1 |
# systemctl restart sshd.service |
秘密鍵をクライントPC にコピー(WinSCP使う) WinSCPの設定を行います
①WinSCPを起動し、「New Site」で次のように設定し、「Save」
Host name : サーバーのIPアドレス
Port number : SSHポート番号
User name : ユーザー名
Password : ユーザーのパスワード

接続名を任意の名前でつける

下記の画面に戻りますので「Login」

下記の画面が出れば「Update」

下記の画面では「Pasword」にユーザーのパスワードを入力する

下記の画面になりますので /home/ユーザー/.ssh ディレクトリーにあるid_ed25519をwindows側の任意の場所にコピーする(id_ed25519を選択し、左側にドラッグ&コピー)

3.秘密鍵でのみログインするよう変更します
1 2 3 4 5 |
# vi /etc/ssh/sshd_config ■69 行目あたり 「#PasswordAuthentication yes」の下に「PasswordAuthentication no」を追加 #PasswordAuthentication yes PasswordAuthentication no |
SSH を再起動
1 |
# systemctl restart sshd.service |
4.PuTTYgen を使った秘密鍵の作成
Winscp起動しツールからRun PuTTYgen を起動

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


サーバー側で秘密鍵を作成したときに入力したパスフレーズを入力します

以下のように表示されると [ OK ] ボタンをクリックします。

[ Save private key ] ボタンをクリックして秘密鍵を保存します。

ここでは id_ed25519.ppk として保存します(Windowsに)。

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

「Advanced」をクリック

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

「Save」をクリック

「Login」をクリック

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

正常に接続されると下記のように右側にサーバー側、左側にWindows側が表示される

Tera Termで接続する場合
「Use RSA/DSA/ECDSA/...」の欄にはwindowsに保存した「id_ed25519.ppk」を指定する

これでSSHサービスのセキュリティ設定は完了です。