wsl2で構築したUbuntu20.04にTeraTermからssh接続する手順をまとめました。
以前構築したUbuntu20.04の記事はこちらから
/etc/ssh/sshd_configを編集しよう
/etc/ssh/sshd_configはsshサーバー側の設定ファイルです。
デフォルトだとほとんどが無効になっており、パスワード認証も無効になっています。
ここではパスワード認証を有効にします。
sshd_configを開きます。
sudo vi /etc/ssh/sshd_config

赤枠の[PasswordAuthentication no]を見つけてください。
(Ubuntu20.04では58行目にあったので[:58]でジャンプしてみてください)

[PasswordAuthentication yes]に変更して上書き保存します。

ssh-keygenでホスト認証鍵を作成
パスワード認証を有効にしたとしてもssh接続ができるようにはなりません。

sshd: no hostkeys available — exiting.
ssh接続はパスワード認証を行う前にホスト認証をおこなうため、
ホスト認証鍵を作成する必要があります。
ホスト認証はクライアントがサーバに接続する際にサーバの正当性を確かめるために行います。
TeraTermでサーバに接続する際にこのような画面を見たことがあるかもしれません。
初回接続時は正当性を確かめるのは困難ですが2回目以降はサーバのIPアドレスが
変わらない前提でknown hostsリストに追加しておくと
こういった警告は出てこないと考えられます。

ちなみに「このホストをknown hostsリストに追加する」に☑すると
クライアントの以下フォルダ内の「ssh_known_hosts」に追記されます。
%LOCALAPPDATA%\VirtualStore\Program Files (x86)\teraterm

ssh_known_hostsの中身はこのような感じです。

前置きが長くなりましたが以下コマンドでホスト認証鍵を作成します。
sudo -ssh-keygen -A

TeraTermでssh接続してみる
それではsshdを起動しましょう。
sudo service ssh restart

ip addr等でIPアドレスを確認し、TeraTermで接続してみます。

前章で紹介した通り、ssh_known_hostsに記載のない場合は以下の警告が出ます。

ユーザ名、パスワードを入力して[OK]をクリックします。

無事接続できました。

コメント