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

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

新規登録して質問してみよう
ただいま回答率
85.46%
SSH

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

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

ネットワーク

ネットワークとは、複数のコンピューター間を接続する技術です。インターネットが最も主流なネットワークの形態で、TCP/IP・HTTP・DNSなどの様々なプロトコルや、ルータやサーバーなどの様々な機器の上に成り立っています。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

Q&A

解決済

1回答

1983閲覧

SSHのGatewayPortsの設定が反映されない?

dlrow

総合スコア22

SSH

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

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

ネットワーク

ネットワークとは、複数のコンピューター間を接続する技術です。インターネットが最も主流なネットワークの形態で、TCP/IP・HTTP・DNSなどの様々なプロトコルや、ルータやサーバーなどの様々な機器の上に成り立っています。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

0グッド

0クリップ

投稿2021/04/10 06:34

編集2021/04/10 08:46

概要

SSHリモートフォーワーディングを行う際に、GatewayPortsの設定をclientspecifiedにしています。しかし、なぜかこの設定が効いておらず指定したプライベートIPではなく、localhostにバインドされてしまいます。

全体のイメージ
remote <==ssh== host

remote
GCPのVMインスタンス
OSはdebian

host
AWSのEC2インスタンス
OSはubuntu
localhost:3128にてnginxが起動している

実施した手順

  1. hostにてvi /etc/ssh/sshd_configでGatewayPortsの設定値をclientspecifiedに設定する
  2. hostにて/etc/init.d/ssh reloadを実行する
  3. hostにてssh -R remoteのプライペートIP:3128:localhost:3128 -i ssh_key user@remoteのパブリックIPを実行する
  4. remoteにてcurl localhost:3128を実行すると、htmlファイルが帰って来て接続がリモートフォーワーディング自体がうまく行っていることが確認できる
  5. remoteにてcurl remoteのプライペートIP:3128を実行すると、curl: (7) Failed to connect to remoteのプライペートIP port 3128: Connection refusedというメッセージが表示される

やったこと

  • /etc/init.d/ssh reloadで設定を反映させる
  • OSをubuntuからdebianに変えて同じことを試す
  • GatewayPortsの値をyesに変える
  • remoteにてnetstat -lt | grep 3128を実行しました。結果は以下の通り、localhostにしかバインドされていません。
tcp 0 0 localhost:3128 0.0.0.0:* LISTEN tcp6 0 0 localhost:3128 [::]:* LISTEN
  • remoteにてsocat tcp4-listen:3128,reuseaddr,fork TCP:127.0.0.1:3128を実行しました。結果、E bind(5, {AF=2 0.0.0.0:3128}, 16): Address already in useというメッセージがでてエラーになりました。netstat、lsofで確認しましたが、0.0.0.0:3128は使われていないみたいです。

補足

  • GatewayPortsの設定を使わず別の手法を用いて、同じことを実現することはできると思います。しかし、できるだけこのやり方でやりたいです。

SSHの設定ファイル

GatewayPorts以外何もいじっていません。

#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 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 #AllowAgentForwarding yes #AllowTcpForwarding yes GatewayPorts yes 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 /var/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

参考にしたもの

https://blog.cles.jp/item/5699

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

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

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

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

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

guest

回答1

0

ベストアンサー

  1. hostにてvi /etc/ssh/sshd_configでGatewayPortsの設定値をclientspecifiedに設定する
  2. hostにて/etc/init.d/ssh reloadを実行する
  3. hostにてssh -R remoteのプライペートIP:3128:localhost:3128 -i ssh_key user@remoteのパブリックIPを実行する
  • 1、2はhost側ではなく、remote側の設定ですね。(sshdが動いているのはremoteなので)
  • 3. は-R <リモート側のバインドアドレス>:3128:localhost:3128 としてみて下さい。
    • すみません。指定されていましたね。訂正します。

man ssh(1)には下記のように記載されています。

Port forwardings can also be specified in the configuration file. Privileged ports can be forwarded only when logging in as root on the remote machine. IPv6 addresses can be specified by enclosing the address in square braces or using an alternative syntax:

[bind_address/]host/port/hostport.

By default, the listening socket on the server will be bound to the loopback interface only. This may be overridden by specifying a bind_address. An empty bind_address, or the address '*', indicates that the remote socket should listen on all interfaces. Specifying a remote bind_address will only succeed if the server's GatewayPorts option is enabled (see sshd_config(5)).

→サーバ側でGatewayPortsが設定されていれば、バインドアドレス指定できる。*にすると0.0.0.0で待ち受ける旨
GatewayPortsはクライアント側の指示にサーバが従うか否かの設定のようですね。

投稿2021/04/10 11:07

編集2021/04/10 11:10
yskszk63

総合スコア161

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

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

dlrow

2021/04/10 11:29

ありがとうございます!一瞬で解決しました! 諦めてsocatを使いポート転送する方法で行おうと考えてました。。 次回からよりわかりやすい質問をしていきたいので、 もしお時間あれば、質問の仕方についてフィードバックお願いします。
yskszk63

2021/04/10 11:41

解決してよかったです! 私はわかりやすい文章について何とか言える能力はないので偉そうなことは言えないのですが、、 host / remoteのどちらが何をしたか、しっかり書かれていたので分かりやすかったですよ。
dlrow

2021/04/10 11:47

フィードバックありがとうございます! 参考にさせていただきます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問