iptables filter表 nat表 应用实例

iptables基础实例

iptables -nvL 查看规则

[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   33  2412 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 22 packets, 2016 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   38  2784 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

iptables -F 清空规则

[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 5 packets, 388 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

service iptables restart 重新加载规则

[root@localhost ~]# service iptables restart
Redirecting to /bin/systemctl restart  iptables.service

iptables -Z 清空计数器

[root@localhost ~]# iptables -Z
[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    5   388 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 4 packets, 448 bytes)
 pkts bytes target     prot opt in     out     source               destination    

service iptables save 将规则保存到配置文件中/etc/sysconfig/iptables

iptables -t 指定表,不加-t默认是filter表

[root@localhost ~]# iptables -A INPUT -s 192.168.1.1 -p tcp --sport 8080 -d 192.168.100.100 --dport 80 -j DROP
[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination         
 224 15912 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
   0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
   0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
   0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
   6  1129 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
   0     0 DROP       tcp  --  *      *       192.168.1.1          192.168.100.100      tcp spt:8080 dpt:80

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination         
   0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 15 packets, 1428 bytes)
pkts bytes target     prot opt in     out     source               destination  

iptables -A 添加一条规则,添加规则到尾行 iptables -A INPUT -s 192.168.1.1 -p tcp --sport 8080 -d 192.168.100.100 --dport 80 -j DROP这段代码的意识是添加一条规则在-s表示来源 -p是协议 -sport 是源端口,-d目的地址 -dport是目的端口 -j 是选择的操作 DROP是丢弃的意思。

iptables -I是插入一条规则,会插入到首行,数据通信匹配iptables中的规则会从上向下匹配,当匹配到合适的就不会再继续向下匹配。

[root@localhost ~]# iptables -I INPUT -s 192.168.1.1 -p tcp --sport 8080 -d 192.168.100.100 --dport 80 -j REJECT
[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     tcp  --  *      *       192.168.1.1          192.168.100.100      tcp spt:8080 dpt:80 reject-with icmp-port-unreachable
  438 37040 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
   11  1920 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
    0     0 DROP       tcp  --  *      *       192.168.1.1          192.168.100.100      tcp spt:8080 dpt:80

iptables -nvL --line-number 显示规则编号

[root@localhost ~]# iptables -nvL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     tcp  --  *      *       192.168.1.1          192.168.100.100      tcp spt:8080 dpt:80 reject-with icmp-port-unreachable
2      489 40692 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
3        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
4        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
5        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
6       11  1920 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
7        0     0 DROP       tcp  --  *      *       192.168.1.1          192.168.100.100      tcp spt:8080 dpt:80

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 40 packets, 5000 bytes)
num   pkts bytes target     prot opt in     out     source               destination     

iptables -D 删除规则,可根据行号删除 iptables -D INPUT 7 删除第7条规则

[root@localhost ~]# iptables -D INPUT 7
[root@localhost ~]# iptables -nvL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     tcp  --  *      *       192.168.1.1          192.168.100.100      tcp spt:8080 dpt:80 reject-with icmp-port-unreachable
2      534 43876 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
3        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
4        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
5        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
6       16  2810 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 4 packets, 480 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

netfilter nat表应用

实例1:A机器两块网卡一块连接外网,一块在内网环境,B机器只有一块网卡在内网环境,两台机器在同一内网中。通过A机器能让B机器上网。

  1. A机器上打开路由转发功能 echo "1" >/proc/sys/net/ipv4/ip_forward
[root@localhost ~]# echo "1">/proc/sys/net/ipv4/ip_forward 
[root@localhost ~]# cat /proc/sys/net/ipv4/ip_forward 
1
  1. A机器上执行iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o eno16777736 -j MASQUERADE在外网口开启端口转发
  2. 再给B机器配置好网关,就是A机器的内网口的地址 route add default gw 192.168.100.1

4、B机器就可以ping通外网了。

实例2 端口映射,可以让外部机器访问B机器

  1. A机器同样打开路由转发功能echo "1" >/proc/sys/net/ipv4/ip_forward
  2. A机器执行iptables -t nat -A PREROUTING -d 192.168.254.100 (模拟外网ip) -p tcp --dport 3000(借用端口) -j DNAT --to 192.168.100.100:22(C机器地址和端口)
  3. A 机器执行iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.254.100
  4. B机器同样要设置网关
Connecting to 192.168.254.100:3000...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Thu Jul 19 21:29:24 2018
[root@localhost ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.100  netmask 255.255.255.0  broadcast 192.168.100.255
        inet6 fe80::20c:29ff:fe50:6d26  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:50:6d:26  txqueuelen 1000  (Ethernet)
        RX packets 62  bytes 9270 (9.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 78  bytes 11214 (10.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 6  bytes 504 (504.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 504 (504.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

可以看到使用C机器的Xshell连接A机器的ip指定做过映射的端口3000 ,成功的连接到了B机器。

转载于:https://my.oschina.net/u/3731306/blog/1859386

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值