Joh*_*ler 6 linux php debian iptables linux-networking
直到今天,我使用了一个便宜的路由器,这样我就可以在使用 NAT 的同时共享我的互联网连接并保持网络服务器在线。用户 IP ($_SERVER['REMOTE_ADDR']) 很好,我看到的是用户的 A 类 IP。
但是随着流量每天都在增长,我不得不安装一个 Linux 服务器 (Debian) 来共享我的 Internet 连接,因为我的旧路由器无法再保持流量了。我使用 NAT 通过 IPTABLES 共享互联网,但是现在,在将端口 80 转发到我的网络服务器后,现在我看到的不是真正的用户 IP,而是将我的网关 IP(Linux 内部 IP)视为任何用户 IP 地址。
如何解决这个问题?
我编辑了我的帖子,所以我可以粘贴我当前使用的规则。
#!/bin/sh
#I made a script to set the rules
#I flush everything here.
iptables --flush
iptables --table nat --flush
iptables --delete-chain
iptables --table nat --delete-chain
iptables -F
iptables -X
# I drop everything as a general rule, but this is disabled under testing
# iptables -P INPUT DROP
# iptables -P OUTPUT DROP
# these are the loopback rules
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# here I set the SSH port rules, so I can connect to my server
iptables -A INPUT -p tcp --sport 513:65535 --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 --dport 513:65535 -m state --state ESTABLISHED -j ACCEPT
# These are the forwards for 80 port
iptables -t nat -A PREROUTING -p tcp -s 0/0 -d xx.xx.xx.xx --dport 80 -j DNAT --to 192.168.42.3:80
iptables -t nat -A POSTROUTING -o eth0 -d xx.xx.xx.xx -j SNAT --to-source 192.168.42.3
iptables -A FORWARD -p tcp -s 192.168.42.3 --sport 80 -j ACCEPT
# These are the forwards for bind/dns
iptables -t nat -A PREROUTING -p udp -s 0/0 -d xx.xx.xx.xx --dport 53 -j DNAT --to 192.168.42.3:53
iptables -t nat -A POSTROUTING -o eth0 -d xx.xx.xx.xx -j SNAT --to-source 192.168.42.3
iptables -A FORWARD -p udp -s 192.168.42.3 --sport 53 -j ACCEPT
# And these are the rules so I can share my internet connection
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0:1 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)
如果我删除 MASQUERADE 部分,我会在用 PHP 回显时看到我的真实 IP,但我没有互联网。怎么办,在转发端口的同时拥有互联网并查看我的真实IP?
** xx.xx.xx.xx - 是我的公共 IP。我出于安全原因隐藏了它。
解决了我自己的谜团,但感谢那些帮助到现在的人。研究了更多 iptables 手册页,并找到了一个似乎可以如我所愿的解决方案:
将包含MASQUERADE (iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE)的行替换为以下行:
iptables -t nat -A POSTROUTING -s 192.168.42.0/24 -o eth0 -j SNAT --to-source XX.XX.XX.XX
Run Code Online (Sandbox Code Playgroud)
现在我可以看到我的真实 IP 地址并且也可以上网了。
*XX.XX.XX.XX = 公共 IP
| 归档时间: |
|
| 查看次数: |
17153 次 |
| 最近记录: |