iptables基于域名的访问控制

说明

测试环境中的机器需要控制只能请求某些地址,其他的都屏蔽掉,如果在每台机器的iptables中配置,重复工作太多。 只需将局域网中的默认网关指向一台机器,然后再这台机器上控制iptables即可。

iptables一般只能做对IP的访问控制,如果做域名的控制,iptables会自动解析成IP写入iptables规则中。所以需要使用dnsmasq+ipset+iptables

  • dnsmasq:dns服务器,这里有两个作用,一个是做dns转发,本身不提供dns解析功能。二是将域名解析到的ip自动添加到ipset的表中(高版本的dnsmasq才支持ipset)
  • ipset:作为iptables和dnsmasq的中转,一个用来记录dnsmasq解析出来的IP,二是iptables使用ipset的表来控制访问权限

配置

主机名 IP
route-a 192.168.111.100
work-b 192.168.111.101

网关服务器配置

  • 安装dnsmasq

    [root@route-a ~]# wget http://www.thekelleys.org.uk/dnsmasq/dnsmasq-2.70.tar.gz
    [root@route-a ~]# tar zxvf dnsmasq-2.70.tar.gz
    [root@route-a ~]# cd dnsmasq-2.70
    [root@route-a ~]# make install
    
  • 查看dnsmasq是否支持ipset

    [root@route-a ~]# dnsmasq -v
    Dnsmasq version 2.70  Copyright (c) 2000-2014 Simon Kelley
    Compile time options: IPv6 GNU-getopt no-DBus no-i18n no-IDN DHCP DHCPv6 no-Lua TFTP no-
    conntrack ipset auth no-DNSSEC
    
    This software comes with ABSOLUTELY NO WARRANTY.
    Dnsmasq is free software, and you are welcome to redistribute it
    under the terms of the GNU General Public License, version 2 or 3.
    
  • 配置dnsmasq

    [root@route-a ~]# vim /etc/dnsmasq.conf
    ...
    server=/.com/114.114.114.114
    conf-dir=/etc/dnsmasq.d
    
    [root@route-a ~]# vim /etc/dnsmasq.d/domain.conf
    ipset=/.baidu.com/bdlist
    

    将.com结尾的域名转发到114.114.114.114 DNS服务器解析,将baidu.com的解析的ip记录到dblist的ipset表中

  • ipset配置

    创建dblist表

    [root@route-a ~]# ipset create dblist hash:ip
    [root@route-a ~]# ipset list
    Name: bdlist
    Type: hash:ip
    Revision: 1
    Header: family inet hashsize 1024 maxelem 65536
    Size in memory: 16560
    References: 1
    Members:
    115.239.211.112
    115.239.210.27
    

    members 对应的IP就是域名解析的IP,如果members为空是因为还没有通过dnsmasq请求过baidu.com

  • 启动服务

    [root@route-a ~]# dnsmasq -C /etc/dnsmasq.conf
    
  • 配置转发

    [root@route-a ~]# sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/' /etc/sysctl.conf
    [root@route-a ~]# sysctl -p
    
    
  • iptables删除默认规则

    [root@route-a ~]# iptables -F
    
  • iptables配置默认拒绝所有转发

    [root@route-a ~]# iptables -P FORWARD DROP
    
  • iptables开放白名单

    [root@route-a ~]# iptables -A FORWARD -s 192.168.111.101 -m set --match-set bdlist dst -j ACCEPT
    

    允许111.101服务器访问dblist中的地址,而dblist中存放的是baidu.com的地址,所以允许该服务器访问baidu

节点服务器配置

  • 配置默认网关到网关服务器

    [root@work-b ~]# sed -i 's/^GATEWAY.*/GATEWAY=192.168.111.100/' /etc/sysconfig/network-scripts/ifcfg-eth0
    [root@work-b ~]# service network restart
    
  • 配置dns

    [root@work-b ~]# vim /etc/resolv.conf
    nameserver 192.168.111.110
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,936评论 0 0
  • dnsmasq是什么我就不说了,请自行百度。 目前我需要使用的用途是:1.dhcp(分配一个或者多个内网ip地址)...
    dnaEMx阅读 16,333评论 0 6
  • 1. 概述 在网络环境中一般用户只需要在浏览器中输入url如www.sunny.com就可以到对应服务器获取相应的...
    ghbsunny阅读 2,985评论 0 7
  • 一、实现从根,com,rj.com 模拟互联网的DNS架构 DNS(Domain Name System,域名系统...
    ssjinyao阅读 1,538评论 0 7
  • 1、第八章 Samba服务器2、第八章 NFS服务器3、第十章 Linux下DNS服务器配站点,域名解析概念命令:...
    哈熝少主阅读 3,779评论 0 10