iptables nat表应用

1.1.准备两台虚拟机A和B。

A:两块网卡。

网卡ens33:192.168.231.128,可以联通外网

网卡ens37:192.168.100.1,只可以访问内网

B:一块网卡

网卡ens37:192.168.100.100,只可以访问内网

1.2.打开A机器的网络转发功能

[[email protected]_01 ~]# cat /proc/sys/net/ipv4/ip_forward0[[email protected]_01 ~]# echo "1" > !$echo "1" > /proc/sys/net/ipv4/ip_forward

/proc/sys/net/ipv4/ip_forward文件默认值为0,表示未开启

1.3.在A机器添加如下iptables规则来通过A转发流量

[[email protected]_01 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE

1.4.查看规则内容

[[email protected]_01 ~]# iptables -t nat -nvLChain PREROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target     prot opt in     out     source               destination   11   892 PREROUTING_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0   11   892 PREROUTING_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0   11   892 PREROUTING_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target     prot opt in     out     source               destination   94  7094 OUTPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target     prot opt in     out     source               destination   94  7094 POSTROUTING_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0   94  7094 POSTROUTING_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0   94  7094 POSTROUTING_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0    0     0 MASQUERADE  all  --  *      ens33   192.168.100.0/24     0.0.0.0/0

1.5.在B及其设置A的IP未默认网关

default add default gw 192.168.100.1

1.6.在B机器上设置DNS服务器

编辑/etc/resolv.conf文件添加dns服务器地址

1.7设置端口转发规则

[[email protected]_01 ~]# iptables -t nat -A PREROUTING -d 192.168.231.128 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22
[[email protected]_01 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.231.128
时间: 2024-08-30 15:06:14

iptables nat表应用的相关文章

10.15 iptables filter表案例 10.16/10.17/10.18 iptables nat表应用

10.15 iptables filter表案例 10.16/10.17/10.18 iptables nat表应用 扩展 iptables应用在一个网段 http://www.aminglinux.com/bbs/thread-177-1-1.html sant,dnat,masquerade http://www.aminglinux.com/bbs/thread-7255-1-1.html iptables限制syn速率 http://www.aminglinux.com/bbs/thre

10.15-10.18 iptables filter表案例 iptables nat表应用

七周四次课(3月22日) 10.15 iptables filter表案例 10.16/10.17/10.18 iptables nat表应用 扩展1. iptables应用在一个网段  http://www.aminglinux.com/bbs/thread-177-1-1.html2. sant,dnat,masquerade   http://www.aminglinux.com/bbs/thread-7255-1-1.html3. iptables限制syn速率  http://www.

七周四次课 iptables filter表案例以及iptables nat表应用

iptables小案例将80端口,22端口和21端口放行,22端口需要指定IP段,只有指定IP段访问才可以.其他段一概拒绝.我们可以用一个脚本来实现. #!/bin/bashipt="/usr/sbin/iptables"ipt是定义了一个变量,如果要执行命令,要写全局绝对路径,这样在脚本当中才不会因为环境变量问题导致命令无法执行.所以以后路写shell脚本时一定要写全局绝对路径.我们来定义一个变量,目的就是后面有许多的地方要加载它,如果写很长一段命令会很繁琐.所以我们要定义一个变量,

Linux学习笔记(三十二)iptables filter表案例、 iptables nat表应用

一.iptables filter表案例 需求:将80.20.21端口放行,对22端口指定特定的ip才放行 以下为操作方法: vim  /usr/local/sbin/iptables.sh    //加入如下内容 #! /bin/bashipt="/usr/sbin/iptables"   //定义一个变量,写iptables的绝对路径 $ipt -F                //清空规则 $ipt -P INPUT DROP        //添加默认的INPUT规则 $ip

三十二、iptables filter表小案例、iptables nat表应用

一.iptables filter表小案例 需求:只针对filter表,预设策略INPUT链DROP,其他两个链ACCEPT,然后针对192.168.188.0/24开通22端口,对所有网段开放80端口,对所有网段开放21端口. 这个需求写一个脚本,内容如下: # vi /usr/local/sbin/iptables.sh #! /bin/bash ipt="/usr/sbin/iptables"      //ipt:定义了一个变量,应写绝对路径. $ipt -F $ipt -P

iptables nat 表应用

1.iptables nat 表应用 上图的操作,说白了就是要做一个路由器,路由器可以上网,电脑就可以上网.先操作一下前期准备,打开虚拟机,将两个Linux窗口打开,然后分别拍一张快照,命名为"iptables 小实验前".此举是方便实验过程出现不可挽回的错误,可以通过快照恢复,然后重新做实验.lyslinux-02 是之前克隆的虚拟机.右击上图红色框框 "编辑虚拟机设置" 的位置 ,跳出弹框,选择"添加",网络适配器,下一步,见下图,保持默认,

iptables filter表案例及iptables nat表应用

iptables小案例: 只针对filter表,预设策略INPUT链DROP,其他两个链ACCEPT,然后针对192.168.1.0/24开通22端口,对所有网段开放80端口,对所有网段开放21端口. 由于这个需求有多条规则,所以最好写成脚本的形式,操作示例如下: vi /usr/local/sbin/iptables.sh 加入如下内容,保存退出. 脚本内容: ipt="/usr/sbin/iptables" $ipt -F $ipt -P INPUT DROP $ipt -P OU

Linux centos7iptables filter表案例、iptables nat表应用

一.iptables filter表案例 vim /usr/local/sbin/iptables.sh 加入如下内容 #! /bin/bash ipt="/usr/sbin/iptables" $ipt -F $ipt -P INPUT DROP $ipt -P OUTPUT ACCEPT $ipt -P FORWARD ACCEPT $ipt -A INPUT -m state --state ERLATED,ESTABLISHED -j ACCEPT $!ipt -A INPUT

十(4)iptables语法、iptables filter表小案例、iptables nat表应用

                                    iptables语法 filter表: INPUT链:作用于进入本机的包 OUTPUT链:作用于送出本机的包 FORWARD链:作用于和本机无关的包 nat表: PREROUTING链:作用是包在刚刚到达防火墙时改变包的目标地址 OUTPUT链:改变本地产生的包的目标地址 POSTROUTING链:作用是在包将离开防火墙时改变包源地址 1.查看iptables规则 iptables -nvL  (此时默认查看filter表,

iptables nat表转发

1.上外网 环境 nat表具有转发的功能,让服务器具有路由器的功能. 两台机器A和B. A有两块网卡,网卡a:192.168.88.130   网卡b:192.168.100.2 B有一块网卡,网卡c:192.168.100.2 192.168.88.130是可以上外网的,网卡c只能连通网卡b,并不能上网. 1.1 目标: 让机器B通过机器A转发,达到可以上网的功能. 1.2  nat转发 上外网 1.给两台机器都添加1块网卡,自定义lan段 2.设置机器A新网卡的IP地址 # ifconfig