LoginSignup
0
0

More than 3 years have passed since last update.

CentOS8でポートフォワード

Posted at

firewall-cmdでポートフォワードする場合

firewall-cmdでポートの行き先を指定する場合、今までルーティングテーブルの設定したり、ルーするサーバでポートを開けたりしていたが、ポートフォワードの場合は不要だったことに今更気づいたのでメモ。

クライアント(client)がルータ(rt)のport80にアクセスしたら、サーバ(sv)のport8080に転送させたい場合の設定。こんな感じ。

(client) --------------> (rt) -------------> (sv)
             ip:10.10.20.254        ip:10.10.10.20
             port:80                port:8080

設定前

[rt]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0 eth1 eth2
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  masquerade: no #マスカレード無効
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
[rt]# cat /proc/sys/net/ipv4/ip_forward
0 # forward無効

ポートフォワードを設定する

[rt]# firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=10.10.10.20 --permanent
[rt]# firewall-cmd --reload

設定後

[rt]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0 eth1 eth2
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  masquerade: no #マスカレードは無効のままで変更なし
  forward-ports: 
        port=80:proto=tcp:toport=8080:toaddr=10.10.10.20
  source-ports: 
  icmp-blocks: 
  rich rules: 
[rt]# cat /proc/sys/net/ipv4/ip_forward
1 #forwardが有効に変更

これだけでいけるらしい。

接続確認

クライアント(client)からルータ(rt)の80番ポートにアクセスし、サーバ(sv)の8080ポートにフォワードできていることを確認。

[client ]$ curl http://10.10.20.254:80 #ポート番号なしでもOK
# 10.10.10.20:8080の内容が表示される

今まで、ルーティングを設定したりマスカレードをいじったりしてたけど意味がなかった。。。

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