linux使用iptable做網關


首先在能上外網的機器上增加一塊網卡

我這里兩塊網卡配置如下

[root@muban1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0  外網卡

DEVICE=eth0

TYPE=Ethernet

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=none

IPADDR=192.168.56.100

NETMASK=255.255.255.0

GATEWAY=192.168.56.2

DNS1=192.168.56.2

DNS2=223.5.5.5

IPV6INIT=no

USERCTL=no

 

[root@muban1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1 內網卡

DEVICE=eth1

BOOTPROTO=none

ONBOOT=yes

NETMASK=255.255.255.0

TYPE=Ethernet

IPADDR=172.16.1.1

重啟網絡服務

service network restart

編輯內核配置文件,開啟轉發

vim /etc/sysctl.conf

使net.ipv4.ip_forward = 1

使內核生效

sysctl -p

清空防火牆的filter表

iptables -F

添加轉發規則

iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j MASQUERADE  這里172.16.1.0/24與內網網段對應

其他機器不能上網的服務器的網卡,需要與上面的內網網卡,在同一局域網

網卡配置如下

[root@muban2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1

DEVICE=eth0

TYPE=Ethernet

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=172.16.1.10

NETMASK=255.255.255.0

GATEWAY=172.16.1.1

DNS1=192.168.56.2

DNS2=223.5.5.5

重啟網絡服務

service network restart

測試

[root@muban2 ~]# ping 172.16.1.1 -c4

PING 172.16.1.1 (172.16.1.1) 56(84) bytes of data.

64 bytes from 172.16.1.1: icmp_seq=1 ttl=64 time=0.329 ms

64 bytes from 172.16.1.1: icmp_seq=2 ttl=64 time=0.190 ms

64 bytes from 172.16.1.1: icmp_seq=3 ttl=64 time=0.189 ms

64 bytes from 172.16.1.1: icmp_seq=4 ttl=64 time=0.182 ms

 

--- 172.16.1.1 ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 3002ms

rtt min/avg/max/mdev = 0.182/0.222/0.329/0.063 ms

測試2

[root@muban2 ~]# ping www.baidu.com -c4

PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.

64 bytes from 61.135.169.121: icmp_seq=1 ttl=127 time=11.7 ms

64 bytes from 61.135.169.121: icmp_seq=2 ttl=127 time=32.7 ms

64 bytes from 61.135.169.121: icmp_seq=3 ttl=127 time=9.70 ms

64 bytes from 61.135.169.121: icmp_seq=4 ttl=127 time=9.99 ms

 

--- www.a.shifen.com ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 3015ms

rtt min/avg/max/mdev = 9.700/16.060/32.761/9.676 ms

到這里,完成!


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com