大网数据广告活动
服务器运维问答

How can I forward the HTTP and SSH port to my internal server using iptables?

· 52CE 我爱测

I do not have the router so I made my CentOS 6.4 Linux system into a router, forwarding the public network traffic to my local LAN. It has two NIC cards, one for the public IP address (eth1) and another for the private IP address…

问题原文

原帖链接 · Rahul · CC BY-SA 4.0

I do not have the router so I made my CentOS 6.4 Linux system into a router, forwarding the public network traffic to my local LAN. It has two NIC cards, one for the public IP address (eth1) and another for the private IP address (eth2).

I have a server connected to my local LAN, let's say at 192.168.1.2.

Router

  • eth1: x.x.x.x, the public IP address
  • eth2: 192.168.1.1, the internal router address

When someone from an outside network runs ssh on the public IP, it should be redirected to the server:

ssh x.x.x.x or using PuTTY

I disabled the SELinux and iptables firewall on the server. I tried some iptables modifications on the Linux system router, but the SSH request is still not redirected:

iptables -t nat -A PREROUTING -p tcp -d 192.168.1.1 --dport 22 -j DNAT --to-destination 192.168.1.2:22

I also want to forward all HTTP (80) traffic to my internal server using iptables from the same Linux system router.

已采纳解答

原帖链接 · sandeep.s85 · CC BY-SA 3.0

First of all make sure the packet forwarding is enabled in the kernel

# echo 1 > /proc/sys/net/ipv4/ip_forward

You can also make it permanent by adding below line to /etc/sysctl.conf

net.ipv4.ip_forward = 1

You have to do this on Linux Machine acting as a router. You can try the following rule on router machine.

iptables -t nat -A PREROUTING -i eth1 -d x.x.x.x -p tcp --dport 22 -j  DNAT --to-destination 192.168.1.2:22

Also let us know the output of your NAT Rules from the router box.

iptables -t nat -L -n -v

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-01-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0 / CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。