LoginSignup
2
0

More than 1 year has passed since last update.

WindowsマシンとVirtualBox(Ubuntu)で公開鍵認証のssh接続を勉強してみた

Last updated at Posted at 2023-01-13

はじめに

サーバ側での公開鍵認証の手順を勉強するため、WindowsマシンからVirtualBoxのUbuntuにssh接続をした時の手順を示します。

1. Windowsマシンで鍵ペアを用意

Windowsマシンでssh-keygen -t rsa -b 4096などで鍵ペアを作成し、公開鍵(id_rsa.pubなど)の中身をコピーしておきます。

2. Ubuntuに公開鍵をコピー

Ubuntuを起動し、ログインさせたいユーザーの~/.ssh/authorized_keysに、先ほどコピーしておいたホストOSの公開鍵を貼り付けます。

# .sshが無い場合
linuxuser@wkani-VirtualBox:~$ mkdir .ssh
linuxuser@wkani-VirtualBox:~$ chmod 700 .ssh
linuxuser@wkani-VirtualBox:~$ cd .ssh
linuxuser@wkani-VirtualBox:~/.ssh$ vim authorized_keys
# 公開鍵を貼り付ける
linuxuser@wkani-VirtualBox:~/.ssh$ chmod 600 authorized_keys
linuxuser@wkani-VirtualBox:~/.ssh$ ls
authorized_keys
~/.ssh/authorized_keys
ssh-rsa xxxxxxxxxxx

3. Ubuntuにopenssh-serverをインストールする

openssh-serverがインストールされていない場合、openssh-serverというパッケージをインストールします。

linuxuser@wkani-VirtualBox:~$ sudo apt update && sudo apt upgrade
linuxuser@wkani-VirtualBox:~$ sudo apt install -y openssh-server

インストールが完了すると自動で実行されるので、systemctl status sshdで確認します。
Active: active (running)~とあれば、実行中の状態です。
もし実行中でない場合は、systemctl start sshdで実行できます。

linuxuser@wkani-VirtualBox:~$ sudo systemctl status sshd
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: e>
     Active: active (running) since Wed 2022-03-23 14:22:59 JST; 1 day 1h ago
       Docs: man:sshd(8)
             man:sshd_config(5)
   Main PID: 20426 (sshd)
      Tasks: 1 (limit: 4627)
     Memory: 2.2M
     CGroup: /system.slice/ssh.service
             └─20426 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups

4. sshd-configを編集

このままssh接続をするとパスワード認証方式になっているため、sshサーバの設定ファイルであるsshd-configを編集します。
場所は/etc/ssh/sshd_configにあります。
# から始まるコメントアウトされている行はデフォルトの設定となっています。
また、管理者にしか書き込み権限がないためsudo vim ~のように編集します。

/etc/ssh/ssd_config
#	$OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin
     ・
     ・
     ・

デフォルトから書き換える設定は、
# PubkeyAuthentication noPubkeyAuthentication yes
# PasswordAuthentication yesPasswordAuthentication no
の2つです。
また実際のサーバでは、rootのログインを禁止する# PermitRootLogin yesPermitRootLogin noなどの設定も必要ですがここでは省略します。

5. ポートフォワーディングを設定する

VirtualBoxの、設定→ネットワーク→ポートフォワーディングの順にクリックし、以下のようにルールを設定します。(httpも登録していますが、sshだけで大丈夫です)
R&D_qiita_1.PNG
image.png

6. PowerShellからssh接続をする

最後に、WindowsマシンのPowerShellからssh接続をします。

PS C:\Users\wkani> ssh linuxuser@localhost -p 2222
Enter passphrase for key 'C:\Users\wkani/.ssh/id_rsa':
Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.13.0-37-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

20のアップデートはすぐに適用されます。
10 of these updates are standard security updates.
これらの追加アップデートを確認するには次を実行してください: apt list --upgradable

Your Hardware Enablement Stack (HWE) is supported until April 2025.
Last login: Wed Mar 30 16:50:36 2022 from 10.0.2.2
linuxuser@wkani-VirtualBox:~$

何かメッセージが出ていますが無事接続できました。

2
0
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
2
0