iptables之路由网关共享上网

linux-A 主机配置eth0即可:

[[email protected] ~]# ifconfig eth0|sed -n ‘2p‘
inet addr:192.168.20.3 Bcast:192.168.20.255 Mask:255.255.255.0
[[email protected] ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.20.2 0.0.0.0 UG 0 0 0 eth0
[[email protected] ~]# /etc/init.d/iptables status
iptables: Firewall is not running.

linux-B网关服务器配置及步骤:

1.先配置好每个网卡的IP地址,并置零所有规则和计数器

[[email protected] ~]# ifconfig eth0|sed -n ‘2p‘
inet addr:10.0.0.4 Bcast:10.0.0.255 Mask:255.255.255.0
[[email protected] ~]# ifconfig eth1|sed -n ‘2p‘        #<===无需配置网关
inet addr:192.168.20.2 Bcast:192.168.20.255 Mask:255.255.255.0
[[email protected] ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0
[[email protected]~]# iptables -F
[[email protected]~]# iptables -X
[[email protected]~]# iptables -Z
[[email protected] ~]# iptables -P FORWARD ACCEPT
[[email protected] ~]# iptables -L -n      #<==实验时确保为空,然后关闭iptables防火墙测试

Chain INPUT (policy ACCEPT)

target     prot opt source               destination        

Chain FORWARD (policy ACCEPT)

target     prot opt source               destination        

Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination  

2.linux-B服务器主机开启 ip_forward 转发功能,并加载相应的模块

[[email protected] ~]# grep "ip_forward" /etc/sysctl.conf
net.ipv4.ip_forward = 1        #<==确保转发功能开启
[[email protected] ~]# sysctl -p
[[email protected]~]# modprobe ip_tables
[[email protected]~]# modprobe iptable_filter
[[email protected]~]# modprobe iptable_nat
[[email protected]~]# modprobe ip_conntrack
[[email protected]~]# modprobe ip_conntrack_ftp
[[email protected] ~]# modprobe ip_nat_ftp
[[email protected] ~]# lsmod |egrep ^ip      #<===确保模块加载,模块加载最好放在 /etc/rc.local 下
[[email protected] B-linux ~]# /etc/init.d/iptables stop    #<==先配置好网关,共享上网成功后再看情况开防火墙

3.配置地址转换实现共享上网(POSTROUTING链)

[[email protected] ~]# iptables -t nat -A POSTROUTING -s 192.168.20.0/24 -o eth0 -j SNAT --to-source 10.0.0.4  

4.配置目的地址转换/端口转换(PREROUTING链)

[[email protected] ~]# iptables -t nat -A PREROUTING -i eth0 -d 10.0.0.4 -p tcp --dport 80 -j DNAT --to-destination 192.168.20.3:8080

5.在linux-A主机上ping测试,或其他方法测试查看转换是否成功

[[email protected] ~]# traceroute www.baidu.com
traceroute to www.baidu.com (14.215.177.39), 30 hops max, 60 byte packets
1 192.168.20.2 (192.168.20.2) 0.421 ms 0.284 ms 0.362 ms
2 10.0.0.254 (10.0.0.254) 0.643 ms 0.734 ms 0.627 ms

[[email protected] ~]# curl -I www.baidu.com
HTTP/1.1 200 OK
[[email protected] ~]# iptables -L -n -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT all -- 192.168.20.0/24 0.0.0.0/0 to:10.0.0.4

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

提示:更好的方法是利用脚本进行管理,参考脚本如下(脚本来源:老男孩老师QQ49000448)

#!/bin/bash
#!/bin/bash
#this is a server firewall created by oldboy 17:03 2006-7-26
# e_mail:[email protected]
# qqinfo:49000448
# function: a server firewall
# version:1.1
################################################
# oldboy trainning info.
# QQ 1986787350 70271111
# site:http://www.etiantian.org
# blog:http://oldboy.blog.51cto.com
# oldboy trainning QQ group: 208160987 45039636
################################################
#define variable PATH
IPT=/sbin/iptables
LAN_GW_IP=192.168.0.15
WAN_GW_IP=10.0.0.15
LAN_SERVER=192.168.0.14

echo 1 > /proc/sys/net/ipv4/ip_forward
modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state

#Remove any existing rules
$IPT -F
$IPT -X

#setting default firewall policy
$IPT --policy OUTPUT ACCEPT
$IPT --policy FORWARD ACCEPT
$IPT -P INPUT DROP

#setting for loopback interface
$IPT -A INPUT -i lo -j ACCEPT
$IPT -A OUTPUT -o lo -j ACCEPT

$IPT -A INPUT -m state --state INVALID -j DROP
$IPT -A OUTPUT -m state --state INVALID -j DROP

# Source Address Spoofing and Other Bad Addresses
$IPT -A INPUT -i eth0 -s 172.16.0.0/12 -j DROP
$IPT -A INPUT -i eth0 -s 0.0.0.0/8 -j DROP
$IPT -A INPUT -i eth0 -s 169.254.0.0/16 -j DROP
$IPT -A INPUT -i eth0 -s 192.0.2.0/24 -j DROP

# prevent all Stealth Scans and TCP State Flags
$IPT -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
# All of the bits are cleared
$IPT -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
$IPT -A INPUT -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
#SYN and RST are both set
$IPT -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
# SYN and FIN are both set
$IPT -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
# FIN and RST are both set
$IPT -A INPUT -p tcp --tcp-flags FIN,RST FIN,RST -j DROP
# FIN is the only bit set, without the expected accompanying ACK
$IPT -A INPUT -p tcp --tcp-flags ACK,FIN FIN -j DROP
# PSH is the only bit set, without the expected accompanying ACK
$IPT -A INPUT -p tcp --tcp-flags ACK,PSH PSH -j DROP
# URG is the only bit set, without the expected accompanying ACK
$IPT -A INPUT -p tcp --tcp-flags ACK,URG URG -j DROP

#setting access rules
#one,ip access rules,allow all the ips of hudong.com
$IPT -A INPUT -s 202.81.17.0/24 -p all -j ACCEPT
$IPT -A INPUT -s 202.81.18.0/24 -p all -j ACCEPT
$IPT -A INPUT -s 124.43.62.96/27 -p all -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p all -j ACCEPT
$IPT -A INPUT -s 10.0.0.0/24 -p all -j ACCEPT

#second,port access rules
#nagios
$IPT -A INPUT -s 192.168.1.0/24 -p tcp --dport 5666 -j ACCEPT
$IPT -A INPUT -s 202.81.17.0/24 -p tcp --dport 5666 -j ACCEPT
$IPT -A INPUT -s 202.81.18.0/24 -p tcp --dport 5666 -j ACCEPT

#db
$IPT -A INPUT -s 192.168.1.0/24 -p tcp --dport 3306 -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p tcp --dport 3307 -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p tcp --dport 3308 -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p tcp --dport 1521 -j ACCEPT

#ssh difference from other servers here.........................................................>>
$IPT -A INPUT -s 202.81.17.0/24 -p tcp --dport 50718 -j ACCEPT
$IPT -A INPUT -s 202.81.18.0/24 -p tcp --dport 50718 -j ACCEPT
$IPT -A INPUT -s 124.43.62.96/27 -p tcp --dport 50718 -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p tcp --dport 50718 -j ACCEPT
$IPT -A INPUT -p tcp --dport 22 -j ACCEPT
#ftp
#$IPT -A INPUT -p tcp --dport 21 -j ACCEPT

#http
$IPT -A INPUT -p tcp --dport 80 -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p tcp -m multiport --dport 8080,8081,8082,8888,8010,8020,8030,8150 -j ACCEPT
$IPT -A INPUT -s 202.81.17.0/24 -p tcp -m multiport --dport 8080,8081,8082,8888,8010,8020,8030,8150 -j ACCEPT
$IPT -A INPUT -s 124.43.62.96/27 -p tcp -m multiport --dport 8080,8081,8082,8888,8010,8020,8030,8150 -j ACCEPT

#snmp
$IPT -A INPUT -s 192.168.1.0/24 -p UDP --dport 161 -j ACCEPT
$IPT -A INPUT -s 202.81.17.0/24 -p UDP --dport 161 -j ACCEPT
$IPT -A INPUT -s 202.81.18.0/24 -p UDP --dport 161 -j ACCEPT

#rsync
$IPT -A INPUT -s 192.168.1.0/24 -p tcp -m tcp --dport 873 -j ACCEPT
$IPT -A INPUT -s 202.81.17.0/24 -p tcp -m tcp --dport 873 -j ACCEPT
$IPT -A INPUT -s 202.81.18.0/24 -p tcp -m tcp --dport 873 -j ACCEPT
$IPT -A INPUT -s 124.43.62.96/27 -p tcp -m tcp --dport 873 -j ACCEPT

#nfs 2049,portmap 111
$IPT -A INPUT -s 192.168.1.0/24 -p udp -m multiport --dport 111,892,2049 -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p tcp -m multiport --dport 111,892,2049 -j ACCEPT

#others RELATED
#$IPT -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
$IPT -A INPUT -s 124.43.62.96/27 -p icmp -m icmp --icmp-type any -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p icmp -m icmp --icmp-type any -j ACCEPT

$IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

###############nat start##############################
#nat internet
iptables -t nat -A POSTROUTING -s 192.168.0.0/255.255.255.0 -o eth1 -j SNAT --to-source $LAN_GW_IP

#www server nat wan to lan
iptables -t nat -A PREROUTING -d $WAN_GW_IP -p tcp -m tcp --dport 80 -j DNAT --to-destination $$LAN_SERVER:80
iptables -t nat -A POSTROUTING -d $LAN_SERVER -p tcp --dport 80 -j SNAT --to LAN_GW_IP

  

原文地址:https://www.cnblogs.com/blog-tim/p/10291337.html

时间: 2024-08-07 01:11:31

iptables之路由网关共享上网的相关文章

iptables 做网关共享上网IP多对多映射

iptables做网关共享上网IP多对多映射 iptables -t nat -A POSTROUTING -s 10.0.0.0/255.255.240 -o eth0 -j SNAT --to-source 124.42.60.118-124.42.60.162    #这样的话就有了五个外网IP ,是负载均衡的 如果全映射成一个外网IP,容易被封且一个IP的端口是有限的,65535个端口

用iptables做软路由实现共享上网

我们平时使用的大多数家用路由器都是通过NAT(Network Address Translation,网络地址转换)功能实现共享上网的,iptables是linux内核里整合的一个ip信息包过滤系统,使用iptables配置nat就可以实现和家用路由器一样的上网效果. 环境:两台电脑,都是centos6的系统,其中一台能上网,一台不能上网,两台电脑通过内网相连. A电脑:外网(eth0) ip为192.168.1.1,内网(eth1) ip为10.1.1.1.1 B电脑:内网(eth0) ip为

ubuntu12.04单卡server(mentohust认证)再加上交换机做路由软件共享上网

最近成立了实验室的网络环境中,通过交换机连接的所有主机实验室.想要一个通过该server(单卡)做网关,使用mentohust认证外网,然后内网中的其它主机通过此网关来连接外网. 1.首先在server上利用mentohust连接外网,然后在终端输入ifconfig命令来查看获得的外网ip: eth0 Link encap:以太网 硬件地址 b8:ac:6f:d8:8f:a2 inet 地址:115.156.236.116 广播:115.156.236.255 掩码:255.255.255.0

iptables 共享上网 (NAT表的使用)

机房内网服务器无外网IP上不了网怎么办? 比如,机房有A B 两台服务器,A有外网IP地址,B没有外网IP地址,那B如何上网呢? 思路就是,把B的网关指向A服务器的内网地址,通过A服务器代理上网 iptables的高级应用 共享上网 举例如下: 网关服务器B: ech0 10.0.0.51    外网地址   配上级网关 ech1 172.16.1.51  内网地址   不配网关 内网服务器C: ech1 172.16.1.52 网关172.16.1.51 网关服务器B需要具备的条件 1 物理条

ubuntu12.04单网卡服务器(mentohust认证)加交换机做软路由共享上网

最近在搭建实验室的内网环境,实验室的所有主机通过一台交换机连接起来,想通过其中的一台服务器(单网卡)做网关,利用mentohust认证外网,然后内网中的其他主机通过此网关来连接外网. 1.首先在服务器上利用mentohust连接外网,然后在终端输入ifconfig命令来查看获得的外网ip: eth0 Link encap:以太网 硬件地址 b8:ac:6f:d8:8f:a2 inet 地址:115.156.236.116 广播:115.156.236.255 掩码:255.255.255.0 i

Linux服务器集群架构部署搭建(二)linux防火墙iptables使用及NAT共享

第一章 外网防火墙部署企业应用 1.1 生产中iptables的实际应用 ①iptables是基于内核的防火墙,功能非常强大,基于数据包的过滤!特别是可以在一台非常低的硬件配置下跑的非常好.iptables主要工作在OSI七层的2.3.4层.七层的控制可以使用squid代理+iptables. ②iptabes:生产中根据具体情况,一般,内网关闭,外网打开.大并发的情况不能开iptables,影响性能,iptables是要消耗CPU的,所以大并发的情况下,我们使用硬件防火墙的各方面做的很仔细.s

老男孩教育每日一题-第68天-实现172.16.1.0/24段所有主机通过124.32.54.26外网IP共享上网

题目 实现172.16.1.0/24段所有主机通过124.32.54.26外网IP共享上网 解答: echo 1 > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j SNAT --to-source 124.32.54.26 iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j MASQUERADE 注释说明: 一.参数说明: -t:指

通过服务器系统的软ROUTE服务+NAT提供拨号共享上网(校园版)

这是以前在学校时破解共享上网所写的批处理代码,这个可能是我找的旧版本的了,当时是保密配方,刚找到EXE版本反编译出代码来,没时间检查它了,我记得我改过,新版的找不到了,毕业后根本没弄过了,代码更不熟悉了,但是这个比共享卫士好用.而且现在那个无线WIFI的发射器这么流行,还有金山那些软件商都纷纷提供软件版的共享上网的了,用不着这个老方法了吧.不过这个方法还是有不可比拟的地方,就是比WIFI的距离远,内网透传,这就是技术达到落地的效果,哈哈~~ @echo off mode con cols=84

VirtualBox中CentOS通过Host-Only方式实现虚拟机主机互相访问、共享上网

VirtualBox常用的网络配置如下: 连接方式 主机访问虚拟机 虚拟机访问主机 虚拟机访问虚拟机 虚拟机访问外网 说明 网络地址转换(NAT) 不支持 支持 不支持 支持 默认连接方式,虚拟IP,VirtualBox内部进行网络转换 桥接网卡 支持 支持 支持 支持 此方式相当于真实电脑,真实IP,会占用真实的网络IP资源 仅主机(Host-Only)网络 支持 支持 支持 支持 此方式使用VirtualBox的虚拟网卡,半真实IP,通过共享上网可以实现外网访问 1.默认的NAT方式,比较简