美文网首页
Linux使用iptable做网关

Linux使用iptable做网关

作者: zwb_jianshu | 来源:发表于2019-07-17 14:41 被阅读0次

首先在能上外网的机器上增加一块网卡
我这里两块网卡配置如下

[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

到这里,完成!

相关文章

  • Linux使用iptable做网关

    首先在能上外网的机器上增加一块网卡我这里两块网卡配置如下 重启网络服务 编辑内核配置文件,开启转发 使内核生效 清...

  • 为docker容器暴露端口

    1.使用iptable 获得容器IP//[container_name]为docker容器名称 iptable转发...

  • </3>啥叫网关

    网关 linux网络配置内容

  • linux网络实现粗解

    学习k8s的过程中,用到了iptable,发现自己对linux操作系统网络实现的只是并不是很清晰。 linux协议...

  • 使用Surge mac做网关

    使用Surge mac做网关 优点,主要是处理加密解密的工作,所以效率会比小路由器要快 不足,安装surge的主机...

  • 使用OpenWRT做辅助网关

    1.准备工作 一个安装了的OpenWRT系统的设备(可以是路由器,电脑,或者虚拟机)以下简称辅助网关 主路由可以不...

  • Ubuntu UFW 防火墙

    LInux原始的防火墙工具iptables由于过于繁琐,所以ubuntu系统默认提供了一个基于iptable之上的...

  • Archlinux 下用 create_ap 创建 wifi 热

    在 Archlinux 下可以使用 create_ap 脚本将 hostapd、dnsmasq 和 iptable...

  • iptable

    IPtables分为2部分,一部分位于内核中,用来存放规则,称为NetFilter。还有一段在用户空间中,用来定义...

  • linux 查看网关

    192.168.19.2 gateway

网友评论

      本文标题:Linux使用iptable做网关

      本文链接:https://www.haomeiwen.com/subject/drbqlctx.html