1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

VirtualBox CentOS7 SSH接続(公開鍵認証)を検証してみた

Last updated at Posted at 2022-06-06

はじめに

VirtualBoxで公開鍵認証を検証してみた。
検証した結果を参考にして頂けたら幸いです。

公開鍵認証とは、秘密鍵と公開鍵でSSH接続すること

前提条件

VirtualBoxをインストール済みであること
OSはCentOS7であること
インターネットに接続できる環境であること
接続先のサーバー用とクライアントのサーバー用 計2台のゲストOSを作成する事
ネットワークはブリッジを使用していること

検証環境

クライアントサーバー(192.168.0.3) test-clientユーザーから接続先のサーバ(192.168.0.2) testユーザーへSSH接続を試してみた

公開鍵認証

接続先のサーバ(192.168.0.2) 公開鍵認証を設定した

接続先のサーバの/etc/ssh/sshd_configのPubkeyAuthentication yesして
公開鍵認証を許可した

[root@localhost ssh]# ip a | grep 192.168.0.2
inet 192.168.0.2/24 brd 192.168.0.255 scope global noprefixroute enp0s3
[root@localhost ssh]# 
[root@localhost ssh]# cat sshd_config | grep "Pub"
PubkeyAuthentication yes
[root@localhost ssh]#
[root@localhost ssh]#

sshdを再起動して、上記の設定ファイルを反映させた

[root@localhost ssh]# systemctl restart sshd

接続先サーバーで秘密鍵と公開鍵を作成した

[test@localhost .ssh]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/test/.ssh/id_rsa.
Your public key has been saved in /home/test/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:zFuPWpePDCj8l1u8Ow6MfTP0TF4CLfRBhSo4HDD/6fg test@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|      o.    ..oo.|
|       o.  . o.. |
|       ..o  o.o  |
|       o+....o   |
|        S.+.. o .|
|     .   X = * o |
|      o = O.X +  |
|       o +oB.*   |
|        o.Eo*o.  |
+----[SHA256]-----+
[test@localhost .ssh]$ ls -tlr
合計 12
-rw-r--r-- 1 test test  415  6月  5 07:53 id_rsa_client.pub
-rw-r--r-- 1 test test  408  6月  6 08:25 id_rsa.pub
-rw------- 1 test test 1766  6月  6 08:25 id_rsa
[test@localhost .ssh]$

接続先サーバーでauthorized_keysに公開鍵を追加した

authorized_keysという空ファイルを新規に作成し、
サーバーの公開鍵をauthorized_keysへ追記した。

[test@localhost .ssh]$
[test@localhost .ssh]$ cat id_rsa.pub >> authorized_keys
※authorized_keysの権限を600にしないとSSH接続できなくなる
[test@localhost .ssh]$ chmod 600 authorized_keys
[test@localhost .ssh]$ ls -tlr authorized_keys
-rw------- 1 test test 408  6月  6 08:27 authorized_keys
[test@localhost .ssh]$
[test@localhost .ssh]$

接続先のサーバ(192.168.0.2)の秘密鍵をクライアントサーバーへコピーした

新規で秘密鍵のファイルを開く

vi id_rsa_server

image.png
保存してviを閉じる

:wq

クライアントサーバーへコピーした秘密鍵ファイルの権限変更

秘密鍵のファイル名は任意です。
秘密鍵を権限を600に変更する必要がある。

[test-client@localhost .ssh]$ ls -tlr
合計 16
-rw-------. 1 test-client test-client 1675  6月  5 07:16 id_rsa
-rw-r--r--. 1 test-client test-client  415  6月  5 07:16 id_rsa.pub
-rw-r--r--. 1 test-client test-client  173  6月  6 07:29 known_hosts
※秘密鍵ファイル作成
-rw-rw-r--. 1 test-client test-client 1679  6月  6 08:11 id_rsa_server
※秘密鍵は600(r+w)にする
[test-client@localhost .ssh]$ chmod 600 id_rsa_server
[test-client@localhost .ssh]$
[test-client@localhost .ssh]$ ls -tlr id_rsa_server
-rw-------. 1 test-client test-client 1679  6月  6 08:11 id_rsa_server
[test-client@localhost .ssh]$

接続先のサーバ(192.168.0.2)へSSH接続

クライアントサーバー(192.168.0.3) test-clientユーザーから接続先のサーバー(192.168.0.2)のtestユーザーへSSH接続してみた

[test-client@localhost .ssh]$ ssh -i id_rsa_server test@192.168.0.2
Enter passphrase for key 'id_rsa_server':
Last login: Mon Jun  6 07:58:01 2022
[test@localhost ~]$
[test@localhost ~]$
[test@localhost ~]$ ip a | grep 192.168.0.2
    inet 192.168.0.2/24 brd 192.168.0.255 scope global noprefixroute enp0s3
[test@localhost ~]$
[test@localhost ~]$

念のため、秘密鍵を指定しない場合、SSH接続できないことを確認した

[test-client@localhost .ssh]$ ssh test@192.168.0.2
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
[test-client@localhost .ssh]$
[test-client@localhost .ssh]$
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?