天天看點

iptables filter表案例、iptables nat表應用

iptables filter表案例

[root@test-7 shell]# cat iptables.sh 

#! /bin/bash

ipt="/usr/sbin/iptables"  #iptables使用絕對路徑

$ipt -F  # 清空iptables

$ipt -P INPUT DROP #将所有進來的先drop

$ipt -P OUTPUT ACCEPT #将所有出去的接受

$ipt -P FORWARD ACCEPT #将轉發到外部機器的接受

$ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT #接受含有狀态的(RELATED,ESTABLISHED)

$ipt -A INPUT -s 192.168.100.0/24 -p tcp --dport 22 -j ACCEPT #接受192.168.100.0/24 的22端口

$ipt -A INPUT -p tcp --dport 80 -j ACCEPT #80/21端口開發

$ipt -A INPUT -p tcp --dport 21 -j ACCEPT

2、[root@test-7 shell]# sh iptables.sh

3、

[root@test-7 shell]# iptables -nvL

Chain INPUT (policy DROP 9 packets, 702 bytes)

 pkts bytes target     prot opt in     out     source               destination         

   38  2664 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED

    0     0 ACCEPT     tcp  --  *      *       192.168.100.0/24     0.0.0.0/0            tcp dpt:22

    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80

    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:21

4、[root@test-7 shell]# service iptables save

5、禁ping

[root@test-7 shell]# iptables -D INPUT -p icmp --icmp-type 8 -j DROP

iptables nat表應用

A機器兩塊網卡eno16777736(192.168.100.102)、eno33554984(192.168.133.1),eno16777736可以上外網,eno33554984僅僅是内部網絡,B機器隻有eth1(192.168.133.2),和A機器eno33554984可以通信互聯。

 需求1:可以讓B機器連接配接外網,就是讓A機器做路由器,B機器就是手機,電腦

解決:

1、需要在A、B機器上都添加一個網卡,選擇自定義的區段

<a href="https://s5.51cto.com/oss/201711/01/65e626a85127fb24d57ee1e7e169860d.png-wh_500x0-wm_3-wmp_4-s_368856959.png" target="_blank"></a>

<a href="https://s2.51cto.com/oss/201711/01/b817cf26e24f2d846dea73e4d5dee438.png-wh_500x0-wm_3-wmp_4-s_1340263074.png" target="_blank"></a>

<a href="https://s2.51cto.com/oss/201711/01/7ca26cefe4a9cc3a7188d01863fee8c3.png-wh_500x0-wm_3-wmp_4-s_970358877.png" target="_blank"></a>

2、在A機器上設定IP [root@test-7 ~]# ifconfig eno33554984 192.168.133.1/24

   在B機器上設定IP  [root@test-2 ~]# ifconfig eth1 192.168.133.2/24

3、保證192.168.133.1 、192.168.133.2互通(ping的通)

4、A機器上打開路由轉發 echo "1"&gt;/proc/sys/net/ipv4/ip_forward

5、在A機器上添加:iptables -t nat -A POSTROUTING -s 192.168.133.0/24 -o eno16777736 -j MASQUERADE

6、service iptables save

7、在B機器上設定網關(這個是A機器的IP):route add default gw 192.168.133.1

8、在B機器上ping 192.168.133.1、192.168.100.1(可以通路外網的網關)

9、在B機器上設定DNS:114.114.114.114

 需求2:C機器隻能和A通信,讓C機器可以直接連通B機器的22端口

C機器就是window電腦 192.168.100.100

A機器就是路由器     192.168.100.102

B機器               192.168.133.2

1、A上打開路由轉發echo "1"&gt;/ proc/sys/net/ipv4/ip_forward

2、端口映射,通路192.168.100.102:1122 跳轉到192.168.133.2:22

先清空防火牆設定:iptables -t nat -F

A上執行:iptables -t nat -A PREROUTING -d 192.168.100.102 -p tcp --dport 1122 -j DNAT --to 192.168.133.2:22

         iptables -t nat -A POSTROUTING -s 192.168.133.2 -j SNAT --to 192.168.100.102

3、 B上設定網關為192.168.133.1

4、在C機器上連接配接192.168.100.102:1122;可以連接配接

<a href="https://s4.51cto.com/oss/201711/01/25e97e1f519c32c31637c030f32fb469.png-wh_500x0-wm_3-wmp_4-s_3040913738.png" target="_blank"></a>

本文轉自方向對了,就不怕路遠了!51CTO部落格,原文連結:http://blog.51cto.com/jacksoner/1978215 ,如需轉載請自行聯系原作者