質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Visual Studio Code

Visual Studio Codeとは、Microsoft社が開発したマルチプラットフォーム対応のテキストエディタです。Visual Studioファミリーの一員でもあります。拡張性とカスタマイズ性が高く、テキストエディタでありながら、IDEと遜色ない機能を備えることができます。

SSH

SSH(Secure Shell)は、セキュアチャネルを通してデータを交換するためのネットワークプロトコルです。リモートサーバーへのコマンド実行やファイル転送を行う時に一般的に使用されます。

コマンドプロンプト

コマンドプロンプト(cmd.exe)はMicrosoftによって提供されているコマンドラインインタプリタです。OS/2・Windows CE・Windows NTで使用可能です。

Q&A

解決済

2回答

2731閲覧

コマンドプロンプト、VSCodeのRemote SSHでSSH接続できない

asasepepeko

総合スコア116

Visual Studio Code

Visual Studio Codeとは、Microsoft社が開発したマルチプラットフォーム対応のテキストエディタです。Visual Studioファミリーの一員でもあります。拡張性とカスタマイズ性が高く、テキストエディタでありながら、IDEと遜色ない機能を備えることができます。

SSH

SSH(Secure Shell)は、セキュアチャネルを通してデータを交換するためのネットワークプロトコルです。リモートサーバーへのコマンド実行やファイル転送を行う時に一般的に使用されます。

コマンドプロンプト

コマンドプロンプト(cmd.exe)はMicrosoftによって提供されているコマンドラインインタプリタです。OS/2・Windows CE・Windows NTで使用可能です。

0グッド

0クリップ

投稿2022/07/01 13:17

編集2022/07/06 08:08

前提・実現したいこと

機械学習初心者です。
WinSCP、Tera Termでは、ssh接続することができたのですが、
コマンドプロンプト、VSCodeのRemote SSHを使ったSSHに失敗し、繋がりませんでした。

おそらくポート設定が上手くいっておらず、/etc/ssh/sshd_config を変更しなくてはいけないのですが、なくなっている状態です。

ご教示頂けますと幸いです。どうぞよろしくお願いいたします。

エラー、エラー時のログ

コマンドプロンプトでは、

ssh: connect to host IPアドレス port 22: Connection refused

と表示されました。

VSCodeのRemote SSHでは、

[21:35:18.296] Log Level: 2 [21:35:18.297] remote-ssh@0.82.1 [21:35:18.297] win32 x64 [21:35:18.302] SSH Resolver called for "ssh-remote+7b22686f73744e616d65223a2231382e3137372e34312e323439222c2275736572223a227562756e7475227d", attempt 1 [21:35:18.303] "remote.SSH.useLocalServer": false [21:35:18.303] "remote.SSH.showLoginTerminal": false [21:35:18.303] "remote.SSH.remotePlatform": {"IP":"linux"} [21:35:18.303] "remote.SSH.path": undefined [21:35:18.303] "remote.SSH.configFile": C:\Users\user\.ssh\config [21:35:18.304] "remote.SSH.useFlock": true [21:35:18.304] "remote.SSH.lockfilesInTmp": false [21:35:18.304] "remote.SSH.localServerDownload": auto [21:35:18.304] "remote.SSH.remoteServerListenOnSocket": false [21:35:18.304] "remote.SSH.showLoginTerminal": false [21:35:18.304] "remote.SSH.defaultExtensions": [] [21:35:18.304] "remote.SSH.loglevel": 2 [21:35:18.304] "remote.SSH.enableDynamicForwarding": true [21:35:18.304] "remote.SSH.enableRemoteCommand": false [21:35:18.304] "remote.SSH.serverPickPortsFromRange": {} [21:35:18.304] "remote.SSH.serverInstallPath": {} [21:35:18.324] SSH Resolver called for host: ubuntu@IP [21:35:18.324] Setting up SSH remote "IP" [21:35:18.347] Using commit id "30d9c6cd9483b2cc586687151bcbcd635f373630" and quality "stable" for server [21:35:18.352] Install and start server if needed [21:35:18.355] Checking ssh with "ssh -V" [21:35:18.397] > OpenSSH_for_Windows_8.1p1, [21:35:18.398] > LibreSSL 3.0.2 [21:35:18.402] Using SSH config file "C:\Users\user\.ssh\config" [21:35:18.403] Running script with connection command: ssh -T -D 55769 -F "C:\Users\user\.ssh\config" "ubuntu@18.177.41.249" bash [21:35:18.405] Terminal shell path: C:\windows\System32\cmd.exe [21:35:18.727] > ]0;C:\windows\System32\cmd.exe [21:35:18.728] Got some output, clearing connection timeout [21:35:21.057] > ssh: connect to host IP port 22: Connection refused [21:35:21.075] > プロセスが、存在しないパイプに書き込もうとしました。 [21:35:22.361] "install" terminal command done [21:35:22.362] Install terminal quit with output: プロセスが、存在しないパイプに書き込もうとしました。 [21:35:22.362] Received install output: プロセスが、存在しないパイプに書き込もうとしました。 [21:35:22.363] Failed to parse remote port from server output [21:35:22.364] Resolver error: Error: at Function.Create (c:\Users\user\.vscode\extensions\ms-vscode-remote.remote-ssh-0.82.1\out\extension.js:1:585219) at Object.t.handleInstallOutput (c:\Users\user\.vscode\extensions\ms-vscode-remote.remote-ssh-0.82.1\out\extension.js:1:583871) at Object.t.tryInstall (c:\Users\user\.vscode\extensions\ms-vscode-remote.remote-ssh-0.82.1\out\extension.js:1:680958) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async c:\Users\user\.vscode\extensions\ms-vscode-remote.remote-ssh-0.82.1\out\extension.js:1:643905 at async Object.t.withShowDetailsEvent (c:\Users\user\.vscode\extensions\ms-vscode-remote.remote-ssh-0.82.1\out\extension.js:1:647221) at async Object.t.resolve (c:\Users\user\.vscode\extensions\ms-vscode-remote.remote-ssh-0.82.1\out\extension.js:1:644955) at async c:\Users\user\.vscode\extensions\ms-vscode-remote.remote-ssh-0.82.1\out\extension.js:1:727005 [21:35:22.371] ------

と表示されました。

該当のソースコード

コマンドプロンプトでは下記の通りです。

ssh -i 鍵 ubuntu@18.177.41.xxx

~/.ssh/configは下記の通りです。

Host ubuntu@IP HostName IP User ubuntu Port 50022 IdentityFile C:\Users\user\.ssh\秘密鍵

追記

ssh/sshd_config の中身です。

# Package generated configuration file # See the sshd_config(5) manpage for details # What ports, IPs and protocols we listen for Port 50022 # Use these options to restrict which interfaces/protocols sshd will bind to ListenAddress 18.177.xx.xxx ListenAddress ::1 Protocol 2 HostKeys for protocol version 2 #HostKey /etc/ssh/ssh_host_rsa_key #HostKey /etc/ssh/ssh_host_ecdsa_key #HostKey /etc/ssh/ssh_host_ed25519_key #Privilege Separation is turned on for security UsePrivilegeSeparation yes # Lifetime and size of ephemeral version 1 server key KeyRegenerationInterval 3600 ServerKeyBits 1024 # Logging SyslogFacility AUTH LogLevel INFO # Authentication: LoginGraceTime 120 PermitRootLogin no StrictModes yes RSAAuthentication yes PubkeyAuthentication yes #AuthorizedKeysFile %h/.ssh/authorized_keys # Don't read the user's ~/.rhosts and ~/.shosts files IgnoreRhosts yes # For this to work you will also need host keys in /etc/ssh_known_hosts RhostsRSAAuthentication no # similar for protocol version 2 HostbasedAuthentication no # Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication #IgnoreUserKnownHosts yes # To enable empty passwords, change to yes (NOT RECOMMENDED) PermitEmptyPasswords no # Change to yes to enable challenge-response passwords (beware issues with # some PAM modules and threads) ChallengeResponseAuthentication no # Change to no to disable tunnelled clear text passwords #PasswordAuthentication yes # Kerberos options #KerberosAuthentication no #KerberosGetAFSToken no #KerberosOrLocalPasswd yes #KerberosTicketCleanup yes # GSSAPI options #GSSAPIAuthentication no #GSSAPICleanupCredentials yes X11Forwarding yes X11DisplayOffset 10 PrintMotd no PrintLastLog yes TCPKeepAlive yes #UseLogin no #MaxStartups 10:30:60 #Banner /etc/issue.net # Allow client to pass locale environment variables #AcceptEnv LANG LC_* Subsystem sftp /usr/lib/openssh/sftp-server # Set this to 'yes' to enable PAM authentication, account processing, # and session processing. If this is enabled, PAM authentication will # be allowed through the ChallengeResponseAuthentication and # PasswordAuthentication. Depending on your PAM configuration, # PAM authentication via ChallengeResponseAuthentication may bypass # the setting of "PermitRootLogin without-password". # If you just want the PAM account and session checks to run without # PAM authentication, then enable this but set PasswordAuthentication # and ChallengeResponseAuthentication to 'no'. UsePAM yes UseDNS no

追記(2)

ディレクトリの構造

ubuntur───────ssh ─────sshd_cinfig │ └─ sshd_config.old │ └─ id_rsa.pub │ └─ id_ecdsa │ └─ id_ecdsa.pub │ └─ id_ed25519 │ └─ id_ed25519.pub │ └─ .ssh───────.authorized_keys.swp     ├──authorized_keys     ├──known_hosts └── known_hosts.old

追記(3)

コマンドプロンプトは

ssh -i 鍵 ubuntu@18.177.41.xxx -p 50022

でポート指定のコードに変更したことによってssh接続することができました。

しかし、VSCodeでは変わらずポートを50022に指定したにもかかわらず、
22ポートを接続することができないとエラーが返ってきます。
(サーバーの22ポートが閉じているため)

また、 id_rsa、id_ecdsa、id_ed25519 などが作成は

sshd -t

でこれらのファイルが無いため作成してくださいとエラーが返ってきたにもかからわず、

ssh-keygen -t rsa -N '' -f /etc/ssh/ssh_host_rsa_key

などで作成してみると既に存在しますが上書きしますか?と返ってきました。

なお、上書きしてもsshフォルダ内にssh_host_rsa_key、/ssh_host_ecdsa_key、ssh_host_ed25519_keyは作成されていませんでした。
そのため、sshd -t では変わらずエラーが起こりました。

使用ツール

Windows 10 Home
VSCode version 1.68
Remote-SSH ver. 082.1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mike2mike4

2022/07/01 15:11

Port 50022 を22にすればよいのではないでしょうか? あえて50022にしている理由を教えてください。
asasepepeko

2022/07/01 15:29 編集

コメントありがとうございます。                                       >あえて50022にしている理由。 以前まで、22で接続していたのですが、ホストサーバーが再構築され、指定されたのが50022になったためです。                                            WinSCP、Tera Termでは、50022で接続することができました。
asasepepeko

2022/07/01 16:01

試しに22で接続を試してみましたが、接続することが出来ませんでした。
asasepepeko

2022/07/01 16:27 編集

サーバー側の設定を確認しようとしたのですが、/etc/ssh/sshd_config がなくなっていたため確認することが出来ませんでした。 OpenSSHを再インストールすれば元に戻るのでしょうか? それとも、こちらのサイトのように出荷時の/etc/ssh/sshd_configを新しく作成した方が良いのでしょか? https://ma-tech.centurysys.jp/doku.php?id=mae3xx_tips:configure_sshd:start
otn

2022/07/01 16:31

> WinSCP、Tera Termでは、ssh接続することができたのですが、 Teratermではどういう指定をしてつながったのでしょう?それと同じ設定でやれば良いのですが。 > コマンドプロンプトでは、 どんなコマンドを実行したのでしょうか?
mike2mike4

2022/07/01 16:32

/etc/ssh/sshd_configがないためデフォルトの22番で接続しようとしたと思われます。 新しく作成した方が理解が深まっていいかと思います。
asasepepeko

2022/07/02 06:36 編集

SSH設定ファイル(/etc/ssh/sshd_config)のPort項目を編集後、diffコマンドで編集を確認しようとしたのですが、 InputObject SideIndicator ----------- ------------- C:\Users\user\ssh\sshd_config => C:\Users\user\ssh\sshd_config.old <= と返され、ポート番号が確認できませんでした。
mike2mike4

2022/07/02 07:09

/etc/ssh/sshd_configはサーバー側です。 そもそも/etc/ssh/sshd_config.oldは存在するのでしょうか? もしかして、windowsサーバーにSSHしようとしてませんか? windowsサーバーはRDTです。念のため。 # What ports, IPs and protocols we listen for Port 22 を変え、 # HostKeys for protocol version 2 HostKey /etc/ssh/ssh_host_rsa_key をサーバー側公開キーを指定します
asasepepeko

2022/07/02 07:28

サーバー側に /etc/ssh/sshd_config を作成後、 サーバーでポート番号変更後、 ubuntu@ip:~/ssh$ diff /xxx/ubuntu/ssh/sshd_config で編集を確認してみると diff: missing operand after '/xxx/ubuntu/ssh/sshd_config' diff: Try 'diff --help' for more information. と返されました。
asasepepeko

2022/07/02 10:05

差分を確認したところ、 1,88c1,88 < Port 22 --- > Port 5022 となり問題ありませんでした。 しかし、 sshd -t で設定ファイルの構文をチェックしたところ、 Could not load host key: /etc/ssh/ssh_host_xx_key Could not load host key: /etc/ssh/ssh_host_xxx_key Could not load host key: /etc/ssh/ssh_host_xxxx_key とエラーが返されてしまいました。
mike2mike4

2022/07/02 10:31

AWSのEC2を立てて確認しました。 HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_dsa_key HostKey /etc/ssh/ssh_host_ecdsa_key を#でコメントアウト化してみてください。
asasepepeko

2022/07/03 08:55

Could not load host key: /etc/ssh/ssh_host_rsa_key Could not load host key: /etc/ssh/ssh_host_ecdsa_key Could not load host key: /etc/ssh/ssh_host_ed25519_key とエラーが返されたので、/etc/ssh/sshd_configに # HostKeys for protocol version 2 HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key とコメントアウトしたのですが、変わらずエラーが起こりました。
mike2mike4

2022/07/03 09:36

HostKeys for protocol version 2 #HostKey /etc/ssh/ssh_host_rsa_key #HostKey /etc/ssh/ssh_host_ecdsa_key #HostKey /etc/ssh/ssh_host_ed25519_key です
asasepepeko

2022/07/03 10:33

HostKeys for protocol version 2 #HostKey /etc/ssh/ssh_host_rsa_key #HostKey /etc/ssh/ssh_host_ecdsa_key #HostKey /etc/ssh/ssh_host_ed25519_key に変更後、再度 sshd -t を行いましたが、変わらず Could not load host key: /etc/ssh/ssh_host_rsa_key Could not load host key: /etc/ssh/ssh_host_ecdsa_key Could not load host key: /etc/ssh/ssh_host_ed25519_key とエラーが起こりました。
mike2mike4

2022/07/03 11:11

うーん、host keyはサーバー間で多段接続するときに必要なので、不要と判断したのですが(ツッコミ待ち) 以下は、コメント外して、エラーが出てもそのまま気にせずリロード。但し、SSHで繋がらなくなる可能性があるので自己責任で。正直、ポートを変えても認証鍵つかってるのだから意味はそんなにないです。 https://techblog.kyamanak.com/entry/2018/10/20/235756
asasepepeko

2022/07/03 12:20

host key もコメントアウトしてみたのですが、変わらずエラーが起こりました。 ListenAddressを指定する必要があるのかと思いましたが、変化ありませんでした。 また、ポート22が使用できないのは閉じているためです。 /etc/ssh/sshd_config の中身を追記しましたのでご確認いただければと思います。
mike2mike4

2022/07/06 10:01

ssh-keygen -t rsa -N '' -f /etc/ssh/ssh_host_rsa_key だと、ポートが変わっているため読み書きできないはずですが。
guest

回答2

0

自己解決

質問を立て直します。

投稿2022/07/07 12:44

asasepepeko

総合スコア116

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

以下でどうですか?

# This is the sshd server system-wide configuration file. See # sshd_config(5) for more information. # This sshd was compiled with PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games # The strategy used for options in the default sshd_config shipped with # OpenSSH is to specify options with their default value where # possible, but leave them commented. Uncommented options override the # default value. Port 50022 #AddressFamily any #ListenAddress 0.0.0.0 #ListenAddress :: #HostKey /etc/ssh/ssh_host_rsa_key #HostKey /etc/ssh/ssh_host_ecdsa_key #HostKey /etc/ssh/ssh_host_ed25519_key # Ciphers and keying #RekeyLimit default none # Logging #SyslogFacility AUTH #LogLevel INFO # Authentication: #LoginGraceTime 2m #PermitRootLogin prohibit-password #StrictModes yes #MaxAuthTries 6 #MaxSessions 10 #PubkeyAuthentication yes # Expect .ssh/authorized_keys2 to be disregarded by default in future. #AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2 #AuthorizedPrincipalsFile none #AuthorizedKeysCommand none #AuthorizedKeysCommandUser nobody # For this to work you will also need host keys in /etc/ssh/ssh_known_hosts #HostbasedAuthentication no # Change to yes if you don't trust ~/.ssh/known_hosts for # HostbasedAuthentication #IgnoreUserKnownHosts no # Don't read the user's ~/.rhosts and ~/.shosts files #IgnoreRhosts yes # To disable tunneled clear text passwords, change to no here! PasswordAuthentication no #PermitEmptyPasswords no # Change to yes to enable challenge-response passwords (beware issues with # some PAM modules and threads) KbdInteractiveAuthentication no # Kerberos options #KerberosAuthentication no #KerberosOrLocalPasswd yes #KerberosTicketCleanup yes #KerberosGetAFSToken no # GSSAPI options #GSSAPIAuthentication no #GSSAPICleanupCredentials yes #GSSAPIStrictAcceptorCheck yes #GSSAPIKeyExchange no # Set this to 'yes' to enable PAM authentication, account processing, # and session processing. If this is enabled, PAM authentication will # be allowed through the KbdInteractiveAuthentication and # PasswordAuthentication. Depending on your PAM configuration, # PAM authentication via KbdInteractiveAuthentication may bypass # the setting of "PermitRootLogin without-password". # If you just want the PAM account and session checks to run without # PAM authentication, then enable this but set PasswordAuthentication # and KbdInteractiveAuthentication to 'no'. UsePAM yes #AllowAgentForwarding yes #AllowTcpForwarding yes #GatewayPorts no X11Forwarding yes #X11DisplayOffset 10 #X11UseLocalhost yes #PermitTTY yes PrintMotd no #PrintLastLog yes #TCPKeepAlive yes #PermitUserEnvironment no #Compression delayed #ClientAliveInterval 0 #ClientAliveCountMax 3 #UseDNS no #PidFile /run/sshd.pid #MaxStartups 10:30:100 #PermitTunnel no #ChrootDirectory none #VersionAddendum none # no default banner path #Banner none # Allow client to pass locale environment variables AcceptEnv LANG LC_* # override default of no subsystems Subsystem sftp /usr/lib/openssh/sftp-server # Example of overriding settings on a per-user basis #Match User anoncvs # X11Forwarding no # AllowTcpForwarding no # PermitTTY no # ForceCommand cvs server

投稿2022/07/03 13:35

mike2mike4

総合スコア901

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

asasepepeko

2022/07/03 15:50 編集

変わらず Could not load host key: /etc/ssh/ssh_host_rsa_key Could not load host key: /etc/ssh/ssh_host_ecdsa_key Could not load host key: /etc/ssh/ssh_host_ed25519_key のエラーが起こってしまいました。 確認したところsshフォルダ内にssh_host_rsa_key、/ssh_host_ecdsa_key、ssh_host_ed25519_keyがなかったので、これが原因なのかと思います。
mike2mike4

2022/07/04 05:50

私のところでは、そのファイルが存在し、かつ、50022ポートで繋がっているので、作るしかないみたいですね
asasepepeko

2022/07/04 07:33

ssh-keygen -t rsa ssh-keygen -t ecdsa ssh-keygen -t ed25519 で id_rsa、id_ecdsa、id_ed25519 などが作成されたのですがファイルの名前を ssh_host_rsa_key、ssh_host_ecdsa_key、ssh_host_ed25519_key に変更すれいのでしょうか?
mike2mike4

2022/07/04 07:39

EC2で立てたときはデフォルトであったので、なんとも。 rootでchmod 600したほうが良いかも
asasepepeko

2022/07/06 08:03

コマンドプロンプトは ssh -i 鍵 ubuntu@18.177.41.xxx -p 50022 でポート指定のコードに変更したことによってssh接続することができました。 しかし、VSCodeでは変わらずポートを50022に指定したにもかかわらず、 22ポートを接続することができないとエラーが返ってきます。(22ポートは閉じているので。) また、 id_rsa、id_ecdsa、id_ed25519 などが作成は sshd -t でこれらのファイルが無いため作成してくださいとエラーが返ってきたにもかからわず、 ssh-keygen -t rsa -N '' -f /etc/ssh/ssh_host_rsa_key などで作成してみると既に存在しますが上書きしますか?と返ってきました。 なお、上書きしてもsshフォルダ内に ssh_host_rsa_key、/ssh_host_ecdsa_key、ssh_host_ed25519_keyは作成されていませんでした。 そのため、sshd -t では変わらずエラーが起こりました。
asasepepeko

2022/07/07 12:43

質問を立て直します。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問