Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1228091
  • 博文数量: 389
  • 博客积分: 2874
  • 博客等级: 少校
  • 技术积分: 3577
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 10:34
文章分类

全部博文(389)

文章存档

2020年(2)

2018年(39)

2017年(27)

2016年(3)

2015年(55)

2014年(92)

2013年(54)

2012年(53)

2011年(64)

分类: 网络与安全

2015-02-10 17:39:46

bridge方式
云教室离线方式有时需要禁止虚拟机上网,宿主机centos7,使用桥接(普通桥接,不是openvswitch),以前使用centos6.5时解决方法是直接添加iptable规则禁止,现在使用这个方法无效了,所以要找新的方法。
最后看到libvirt本身支持防火墙,可以控制guestos的网络传输。下面是禁止上外网的解决方法。
解决步骤:
1、在终端定义过滤规则
#cat dstipfilter.xml

点击(此处)折叠或打开

  1. <filter name="no-ip-out" chain="ipv4">
  2. <uuid>fce8ae34-e69e-83bf-262e-30786c1f8072</uuid>
  3.  <rule action="drop" direction="out" priority="500">
  4. <ip match="no" dstipaddr="192.168.5.0" dstipmask="255.255.255.0"/>
  5. </rule>
  6. </filter>


上面的xml文档定义虚拟机只可以访问192.168.5.0/24段的网络

2、向libvirt添加过滤规则

 #virsh nwfilter-define dstipfilter.xml
 定义后可以通过 nwfilter-list命令查看可用的过滤规则
 #virsh nwfilter-list

UUID                                  名称               
----------------------------------------------------------------
……   
fce8ae34-e69e-83bf-262e-30786c1f8072  no-ip-out           
……

no-ip-out就是刚才添加的规则

3、向虚拟机添加规则

修改虚拟机的xml文件,在interface部分添加:


添加完后虚拟机的interface部分如下:

点击(此处)折叠或打开

  1. <interface type="bridge">
  2.  <source bridge="virbr0"/>
  3.  <model type="rtl8139"/>
  4.  <filterref filter="no-ip-out"/>
  5.  <mac address="00:16:3e:77:b3:71"/>
  6.  <rom bar="off"/>
  7. </interface>

4、启动虚拟机

上面的方法经本人测试通过

5、支持dhcp
后来又有了新的需求,虚拟机需要通过dhcp获取ip,添加该规则后dhcp获取不到ip。规则修改如下:

点击(此处)折叠或打开

  1. <filter name="no-ip-out" chain="ipv4">
  2.   <uuid>fce8ae34-e69e-83bf-262e-30786c1f8072</uuid>
  3.   <rule action="accept" direction="out" priority="100">
  4.  <ip srcipaddr="0.0.0.0" dstipaddr="255.255.255.255" protocol="udp" srcportstart="68" dstportstart="67"/>
  5.   </rule>
  6.   <rule action="accept" direction="in" priority="100">
  7.  <ip protocol="udp" srcportstart="67" dstportstart="68"/>
  8.   </rule>
  9.   <rule action="drop" direction="out" priority="200">
  10.  <ip match="no" dstipaddr="192.168.5.0" dstipmask="255.255.255.0">
  11.   </rule>
  12. </filter>


在以前的基础上添加了两个rule,在测试一下
多个rule的匹配顺序需要用到 priority 选项,下面是对该选项的解释,重点是那句 priority较低的rule先与priority高的rule被匹配。
 priority is optional. The priority of the rule controls the order in which the rule will be instantiated relative to other rules. Rules with lower values will be instantiated before rules with higher values. Valid values are in the range of -1000 to 1000. If this attribute is not provided, priority 500 will be assigned by default. Note that filtering rules in the root chain are sorted with filters connected to the root chain following their priorities. This allows to interleave filtering rules with access to filter chains.

6 . openvswitch方式

ovs-ofctl add-flow ovsbr0 dl_dst=58:bf:ea:22:97:c9,action=drop
dl_dst 指定 网关 ip
阅读(1927) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~