Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6941028
  • 博文数量: 701
  • 博客积分: 10821
  • 博客等级: 上将
  • 技术积分: 12021
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-02 10:41
个人简介

中科院架构师,专注企业数字化各个方面,MES/ERP/CRM/OA、物联网、传感器、大数据、ML、AI、云计算openstack、Linux、SpringCloud。

文章分类

全部博文(701)

分类: LINUX

2015-03-04 20:05:13



用Debian可以架设一个全能的网关,它可以承担NAT、mail、DHCP、DNS cache、HTTP proxy cache、CVS、NFS的工作以及提供面向家庭LAN系统的Samba services。有关这方面网络配置的讨论可参阅。


10.1 网络配置


10.1.1 网关的主机设置

LAN按下面方法分段使用IP地址以避免Internet上的IP地址冲突。

     Class A: 10.0.0.0                    with mask 255.0.0.0
     Class B: 172.16.0.0 - 172.31.0.0     with mask 255.255.0.0
     Class C: 192.168.0.0 - 192.168.255.0 with mask 255.255.255.0

在Debian中/etc/network/interfaces文件用于IP设置。

举个例子,假如某台机器的eth0使用DHCP动态IP地址连接Internet,eth1连接LAN,则其/etc/network/interfaces的设置如下(适用于Woody及后继版本)

     auto lo
     iface lo inet loopback
     
     auto eth0
     iface eth0 inet dhcp
     
     auto eth1
     iface eth1 inet static
     address 192.168.1.1
     network 192.168.1.0
     netmask 255.255.255.0
     broadcast 192.168.1.255

修改了/etc/network/interfaces后,可执行下面的命令使其生效:

     # /etc/init.d/networking restart

注意:在Woody及其后继版本中的/etc/network/interfaces文件不适用于Potato。(同样的差异会出现在Sarte和Woody之间。)

如果系统使用PCMCIA NIC,则在Potato系统中需要设置/etc/pcmcia/network.opts而不是interfaces,在Woody就不必了,系统已解决了这个问题。

想检查设置结果可查看下列命令输出:

     # ifconfig
     # cat /proc/pci
     # cat /proc/interrupts
     # dmesg | more

有时,DSL(PPPoE)连接存在MTU问题,参阅。如果存在无法访问某些站点的问题,参阅。


10.1.2 网络设置检查

典型的软件集合:

     # apt-get install nfs samba dhcpd dhcp-client bind squid procmail fetchmail 
     # apt-get install ssh cvs

检查下列文件:

     /etc/init.d/dhcpd       (edit to serve only LAN = eth1)
     /etc/host.allow         (ALL: 192.168.0.0/16 127.0.0.0/8) for NFS
     /etc/exports            (Need this for NFS)
     /etc/bind/db.192.168.1  (add)
     /etc/bind/db.lan        (add)
     /etc/bind/named.conf    (edit)
     /etc/resolv.conf        (edit)
     /etc/hosts
     /etc/dhcpd.conf         (edit for LAN = eth1)
     /etc/dhclient.conf      (edit to force local DNS)
     /etc/samba/smb.conf
     /etc/exim/exim.conf
     /etc/mailname
     /etc/aliases
     /etc/squid.conf         (add all LAN host IPs as allowed)

bind创建一个本地的cache DNS server并且changes DNS to localhost。检查/etc/resolv.conf:

     nameserver 127.0.0.1
     search lan.aokiconsulting.com

10.2 Netfilter设置

在Linux 2.4及其后继版本中加入了netfilter/iptables项目,作为一个防火墙子系统。参阅,那儿有许多有关其配置的讨论和解释。


10.2.1 netfilter基础

Netfilter内建了5条链路来处理数据包,它们分别是:PREROUTING、INPUT、FORWARD、OUTPUT和POSTROUTING:

                     routing
                     decision
     IN ------> PRE ---> ------> FORWARD -----> ----> POST -----> OUT
     interface  ROUTING  \       filter       /       ROUTING     interface
                DNAT     |       tracking     ^       SNAT
                REDIRECT |                    |       MASQUERADE
                         v                    |
                       INPUT                OUTPUT
                         | filter             ^ filter,DNAT 
                         v                    |
                         \--> Local Process --/
                              user-space programs

10.2.2 过滤表(Netfilter table)

数据包在每条内建的链路中传输时按如下过滤表中的规则进行处理。

  • filter(数据包过滤器,链路中默认的过滤器)
    • INPUT(作用于进入本机的数据包)
    • FORWARD(作用于路由到本机的数据包)
    • OUTPUT(作用于本地产生的数据包)
  • nat(网络地址翻译)
    • PREROUTING(作用于刚进入的待转换数据包)
    • OUTPUT(作用于在路由之前待转换的本地产生的数据包)
    • POSTROUTING(作用于待发出的已转换的数据包)
  • mangle (network address mangling, good only after 2.4.18)
    • 适用于所有5条链路。

10.2.3 过滤目标(Netfilter target)

Firewall rules have several targets:

  • 4个基本目标:
    • ACCEPT 允许数据包通过。
    • DROP 阻拦数据包。
    • QUEUE 允许数据包进入用户空间(userspace)(如果内核支持的话)。
    • RETURN means stop traversing this chain and resume at the next rule in the previous (calling) chain.
  • 扩展目标:
    • LOG 打开内核日志。
    • REJECT 回送错误数据包并阻拦该数据包。
    • SNAT 修改数据包源地址,仅作用于POSTROUTING链路。(仅适用于nat过滤表)
           --to-source ipaddr[-ipaddr][:port-port]
      
    • MASQUERADE 作用和SNAT一样,但面向使用动态IP请求建立的连接(拔号连接)。(仅适用于nat过滤表)
           --to-ports port[-port]
      
    • DNAT 修改数据包目的地址,仅作用于PREROUTING、OUTPUT链路以及由它们调用的用户自定义链路。(仅适用于nat过滤表)
           --to-destination ipaddr[-ipaddr][:port-port]
      
    • REDIRECT 修改数据包目标地址使其发送给本机。
           --to-ports port[-port]
      

10.2.4 网络过滤器命令

iptables的基本命令有:

     iptables -N chain # create a chain iptables -A chain \                 # add rule to chain -t table \                 # use table (filter, nat, mangle)
              -p protocol \              # tcp, udp, icmp, or all,
              -s source-address[/mask] \
              --sport port[:port] \      # source port if -p is tcp or udp
              -d destination-address[/mask] \
              --dport port[:port] \      # dest. port if -p is tcp or udp
              -j target \                # what to do if match
              -i in-interface-name \     # for INPUT,  FORWARD, PREROUTING
              -o out-interface-name # for FORWARD, OUTPUT, POSTROUTING

10.2.5 IP伪装(IP-masquerade)

一个运用IP伪装(NAT)的网关可实现LAN内的机器通过共享一个单独的可访问外网的IP地址来访问Internet资源。

     # apt-get install ipmasq

执行样例规则来加强ipmasq的保护机制。 参阅/usr/share/doc/ipmasq/examples/stronger/README。对于使用2.4版内核镜像的Debian,请确认加载了相应的模块。有关的必要设置参阅。

对于使用2.2版内核镜像的Debian,可按下面的方法编辑/etc/masq/rules中的Z92timeouts.rul文件,以保证可长时间连接远程站点(如发送大容量的email,等):

     # tcp, tcp-fin, udp
     # 2hr, 10 sec, 160 sec - default
     # 1 day, 10 min, 10 min - longer example
     $IPCHAINS -M -S 86400 600 600

同样,如果是通过PCMCIA NIC访问网络,ipmasq需要从/etc/pcmcia/network.opts启动。参阅/usr/share/doc/ipmasq/ipmasq.txt.gz.


10.2.6 重定向SMTP联接(2.4版内核)

假设你将一台笔记本电脑重新配置成可连入其它的LAN环境,而你不想再重新配置用户邮件代理,即:想直接用原来的配置收发邮件。

使用iptables命令向网关机器中加入下面的规则,就可以实现重定向与网关机器的SMTP连接。

     # iptables -t nat -A PREROUTING -s 192.168.1.0/24 -j REDIRECT \
                -p tcp --dport smtp --to-port 25 # smtp=25, INPUT is open

想使用更完备的重定向规则集,建议安装ipmasq软件包,并在/etc/ipmasq/rules/目录中添加文件。


10.3 管理多重网络联接

[FIXME] 路由策略(by Phil Brutsche ): 详情参阅。Traffic control (tc) 也很有趣。

Environment:

     eth0: 192.168.1.2/24; gateway 192.168.1.1
     eth1: 10.0.0.2/24; gateway 10.0.0.1
     No masquerading on this machine.

Special magic:

  1. ip rule add from 192.168.1.2 lookup 1
  2. ip rule add from 10.0.0.2 lookup 2
  3. ip route add to default via 10.0.0.1 metric 0
  4. ip route add to default via 192.168.1.1 metric 1
  5. ip route add table 1 to 192.168.1.0/24 via eth0
  6. ip route add table 1 to 10.0.0.2/24 via eth1
  7. ip route add table 1 to default via 192.168.1.1
  8. ip route add table 2 to 192.168.1.0/24 via eth0
  9. ip route add table 2 to 10.0.0.2/24 via eth1
  10. ip route add table 2 to default via 10.0.0.2

[FIXME] 我没亲自做过。如何利用自动拔号特性使拔号连接保持高速?如果你知道请发补丁我:)

阅读(2871) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~