RSA 公開鍵暗号方式を用いた認証によるSSH接続
公開鍵、秘密鍵ペアの作成
OpenSSH 使って Linux サーバに接続するユーザの公開鍵と秘密鍵のペアを作成します。
鍵ペアの作成は ssh-keygen をを使います。
今回はSSH プロトコル Version 2 で利用する RSA 暗号を利用した鍵セットを作成します。
公開鍵と秘密鍵のペアの作成はリモートログインするユーザ権限(huong)で実行します。
作成先・ファイル名を指定しなければ、 /home/huong/.ssh/ に id_rsa, id_rsa.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 - huong [huong@rocky~]$ /usr/bin/ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/huong/.ssh/id_rsa): 空エンター Created directory '/home/huong/.ssh'. Enter passphrase (empty for no passphrase): Password Enter same passphrase again: 再度同じPassword Your identification has been saved in /home/huong/.ssh/id_rsa. Your public key has been saved in /home/huong/.ssh/id_rsa.pub. The key fingerprint is: SHA256:/s3sLJJXH+7Ul01UDQplwwd/PkzQ6fqn7OmEWCen/6g huong@rocky The key's randomart image is: +---[RSA 3072]----+ | .o=oo.+| | o.+ooo| | ..o.o| | o= | | S o +oo| | . o.B.o+| | ....oo+o+| | o.o= =o+o| | o.oE=Xoo| +----[SHA256]-----+ |
1 |
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys |
1 |
$ chmod 600 ~/.ssh/authorized_keys |
Winscpなどを利用し、秘密鍵 (~/.ssh/id_rsa) を Windows の適当な場所に保存する。
Winscpを起動する
Host name : サーバーのIPアドレス
Port number : SSHポート
User name : サーバーのログインユーザー
Password : 同上ユーザーのパスワード
を入力し、「Save」
Site name : 任意の名前
「OK」クリック

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

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

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

接続されると、右欄サーバー側、左欄PC(Windows)側になる。
サーバー側の/home/huong/.ssh 配下にある「id.rsa」を左欄Windowsにドラック&ドロップして保存する

SSH 設定ファイルの編集
パスワード認証をできないようにするため、 SSH 設定ファイルを編集する。
今度は一般ユーザではなく、 su - で rootになり行う。
1 2 3 4 5 6 7 |
$ su - Password: # vi /etc/ssh/sshd_config # 71行目 : パスワード認証不可に変更 PasswordAuthentication no # systemctl restart sshd |
Tera Termでの接続方法
Tera Termを起動し、「File」メニュー「New connection」

Host : サーバーのIPアドレス
TCP port : SSHポート番号

次のセキュリティー警告が出た場合「Replace....」にチェックを入れ「Continue」

User name : ログインユーザー名
Password :公開鍵、秘密鍵ペアの作成で指定したパスワード
「Use RSA/DSA....」にチェックを入れ、「Private key file」の欄には先ほどwindowsに保存した「id_rsa」を指定する

PuTTYgen を使った秘密鍵の作成
Winscpを起動し「Tools」からRun Puttygen を起動する
該当サーバーを選択
Loadをクリック

[ ファイルを開くダイアログ ] が開くのでファイルの種類を [ All Files (*.*) ] に変更して Linux サーバから転送した秘密鍵 id_rsa を読み込みます。
パスワードは上記「公開鍵、秘密鍵ペアの作成」で設定したパスワード

「Sve private key」をクリック

Private key file をwindowsに保存する、ファイル名は「id_rsa.ppk」とする
該当サーバーを選択し、「Edit」クリック
「Advanced」をクリック
「Authentication」を選択し、「Private key file」の欄にはwindows保存した「id_rsa.ppk」ファイルを指定する

「Save」をクリック
「Login」をクリック

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

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