场景
假如你在用 resin 调试一个 Web 程序,需要频繁地重启 resin。这个 Web 程序需要开在 80 端口上,而 Linux 限制 1024 以下的端口必须有 root 权限才能开启。但是你又不愿意在调程序的时候总是开着一个 root 终端。在这种情况下,你可以把 resin 开在默认的 8080 端口上,然后使用 iptables 来实现和真的把服务开在 80 端口上一样的效果。
方法
将与 80 端口的 TCP 连接转接到本地的 8080 端口上。使用 DNAT (Destination Network Address Translation) 技术可以满足这一要求。因为 iptables 在处理本地连接和远程连接的方法不同,所以需要分开处理。下面假设本机的 IP 是 192.168.4.177。
远程连接
远程连接指的是由另外一台机器连接到这台机器上。这种连接的数据包在 iptables 会首先经过 PREROUTING 链,所以只需在 PREROUTING 链中作 DNAT。
复制代码
代码如下:
# iptables -t nat -A PREROUTING -p tcp -i eth0 -d 192.168.4.177 –dport 80 -j DNAT –to 192.168.4.177:8080
本地连接
本地连接指的是在本机上,用 127.0.0.1 或者本机 IP 来访问本机的端口。本地连接的数据包不会通过网卡,而是由内核处理后直接发给本地进程。这种数据包在 iptables 中只经过 OUTPUT 链,而不会经过 PREROUTING 链。所以需要在 OUTPUT 链中进行 DNAT。除了对 127.0.0.1 之外,对本机 IP (即 192.168.4.177) 的访问也属于本地连接。
代码如下:
# iptables -t nat -A PREROUTING -p tcp -i eth0 -d 192.168.4.177 –dport 80 -j DNAT –to 192.168.4.177:8080
本地连接
本地连接指的是在本机上,用 127.0.0.1 或者本机 IP 来访问本机的端口。本地连接的数据包不会通过网卡,而是由内核处理后直接发给本地进程。这种数据包在 iptables 中只经过 OUTPUT 链,而不会经过 PREROUTING 链。所以需要在 OUTPUT 链中进行 DNAT。除了对 127.0.0.1 之外,对本机 IP (即 192.168.4.177) 的访问也属于本地连接。
复制代码
代码如下:
# iptables -t nat -A OUTPUT -p tcp -d 127.0.0.1 –dport 80 -j DNAT –to 127.0.0.1:8080
# iptables -t nat -A OUTPUT -p tcp -d 192.168.4.177 –dport 80 -j DNAT –to 127.0.0.1:8080
注意事项
你也许需要通过以下命令打开 IP 转发:
代码如下:
# iptables -t nat -A OUTPUT -p tcp -d 127.0.0.1 –dport 80 -j DNAT –to 127.0.0.1:8080
# iptables -t nat -A OUTPUT -p tcp -d 192.168.4.177 –dport 80 -j DNAT –to 127.0.0.1:8080
注意事项
你也许需要通过以下命令打开 IP 转发:
复制代码
代码如下:
# echo 1 > /proc/sys/net/ipv4/ip_forward
在进行试验时,如果要重新设置 iptables,需要首先清空 nat 表:
代码如下:
# echo 1 > /proc/sys/net/ipv4/ip_forward
在进行试验时,如果要重新设置 iptables,需要首先清空 nat 表:
复制代码
代码如下:
# iptables -F -t nat
实例操作
这里将本地接口IP 61.144.a.b 的3389端口 转发到 116.6.c.d的3389 (主要访问到61.144.a.b的3389端口,就会跳转到116.6.c.d的3389)
1、 首先应该做的是/etc/sysctl.conf配置文件的 net.ipv4.ip_forward = 1 默认是0 这样允许iptalbes FORWARD。
2、 service iptables stop 关闭防火墙
3、 重新配置规则
代码如下:
# iptables -F -t nat
实例操作
这里将本地接口IP 61.144.a.b 的3389端口 转发到 116.6.c.d的3389 (主要访问到61.144.a.b的3389端口,就会跳转到116.6.c.d的3389)
1、 首先应该做的是/etc/sysctl.conf配置文件的 net.ipv4.ip_forward = 1 默认是0 这样允许iptalbes FORWARD。
2、 service iptables stop 关闭防火墙
3、 重新配置规则
复制代码
代码如下:
iptables -t nat -A PREROUTING –dst 61.144.a.b -p tcp –dport 3389 -j DNAT –to-destination 116.
6.c.d:3389
iptables -t nat -A POSTROUTING –dst 116.6.c.d -p tcp –dport 3389 -j SNAT –to-source 61.144.a.b
service iptables save
代码如下:
iptables -t nat -A PREROUTING –dst 61.144.a.b -p tcp –dport 3389 -j DNAT –to-destination 116.
6.c.d:3389
iptables -t nat -A POSTROUTING –dst 116.6.c.d -p tcp –dport 3389 -j SNAT –to-source 61.144.a.b
service iptables save
将当前规则保存到 /etc/sysconfig/iptables
若你对这个文件很熟悉直接修改这里的内容也等于命令行方式输入规则。
5、 启动iptables 服务, service iptables start
可以写进脚本,设备启动自动运行;
复制代码
代码如下:
# vi /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.</p> <p>touch /var/lock/subsys/local</p> <p>sh /root/myshipin.log
———————————————————————
vi myshipin.log
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.</p> <p>iptables -F -t nat
iptables -t nat -A PREROUTING –dst 61.144.a.b -p tcp –dport 3389 -j DNAT –to-destination 116.6.c.d:3389
iptables -t nat -A POSTROUTING –dst 116.6.a.b -p tcp –dport 3389 -j SNAT –to-source 61.144.c.d
~
—————————————————————-
TCP</p> <p>iptables -t nat -A PREROUTING –dst 61.144.a.b -p tcp –dport 9304 -j DNAT –to-destination 10.94.a.b:9304
iptables -t nat -A POSTROUTING –dst 10.94.a.b -p tcp –dport 9304 -j SNAT –to-source 61.144.a.b</p> <p>UDP
iptables -t nat -A PREROUTING –dst 61.144.a.b -p udp –dport 9305 -j DNAT –to-destination 10.94.a.b:9305
iptables -t nat -A POSTROUTING –dst 10.94.a.b -p udp –dport 9305 -j SNAT –to-source 61.144.a.b
另:
代码如下:
# vi /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.</p> <p>touch /var/lock/subsys/local</p> <p>sh /root/myshipin.log
———————————————————————
vi myshipin.log
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.</p> <p>iptables -F -t nat
iptables -t nat -A PREROUTING –dst 61.144.a.b -p tcp –dport 3389 -j DNAT –to-destination 116.6.c.d:3389
iptables -t nat -A POSTROUTING –dst 116.6.a.b -p tcp –dport 3389 -j SNAT –to-source 61.144.c.d
~
—————————————————————-
TCP</p> <p>iptables -t nat -A PREROUTING –dst 61.144.a.b -p tcp –dport 9304 -j DNAT –to-destination 10.94.a.b:9304
iptables -t nat -A POSTROUTING –dst 10.94.a.b -p tcp –dport 9304 -j SNAT –to-source 61.144.a.b</p> <p>UDP
iptables -t nat -A PREROUTING –dst 61.144.a.b -p udp –dport 9305 -j DNAT –to-destination 10.94.a.b:9305
iptables -t nat -A POSTROUTING –dst 10.94.a.b -p udp –dport 9305 -j SNAT –to-source 61.144.a.b
另:
iptables配置文件的位置:/etc/sysconfig/iptables 外网地址发变化在配置文件里修改就可以了。
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 89
-
2025-06-04 41
-
2025-05-29 53
-
2025-05-25 35
-
2025-06-05 55
热门评论