make menuconfig
Networking support -> Networking Options -> Network packet filtering framework -> Core Netfilter Configuration
<M> Netfilter connection tracking support
<M> "lawyer7" match support
<M> "string" match support
<M> "time" match support
<M> "iprange" match support
<M> "connlimit" match support
<M> "state" match support
<M> "conntrack" connection match support
<M> "mac" address match support
<M> "multiport" Multiple port match support
Networking support -> Networign options -> Network packet filtering framework -> IP:Netfiltr Configuration
<M> IPv4 connection tracking support (required for NAT)
<M> Full NAT
<M> MASQUERADE target support
<M> NETMAP target support
<M> REDIRECT target support
tar -jsvf iptables-1.4.6.tar.bz2 -C /usr/src
cd /usr/src/netfilter-layer7-v2.23
cd iptables-1.4.3forward-for-kernel-2.6.20forward
cp * /usr/src/iptables-1.4.6/extensions/
cd /usr/src/iptables-1.4.6/
./configure --prefix=/usr --with-ksource=/usr/src/linux
make
make install
查看安装后的iptables的文件
代码如下:
ls /usr/sbin |grep iptables
ls /usr/libexec/xtables
#在eth0网卡上创建一个根队列规则,队列规则的算法使用htb,default 2表示指定一个默认类别编号,默认的流量控制策略,如果ip没有在后面的filter中被匹配到就都是有这个策略
tc qdisc add dev eth0 root handle 1:0 htb default 2
#在eth0网卡上定义一个类,prant 1:0中的1对应根队列规则中的handle 1:0,classid 1:2表示当前这个类的标识,用于应用在后面的得到filter中,rate 200kbsp表示带宽为200KB/s,ceil 200kbps表示最大带宽也为200KB/s,prio 2是优先级
tc class add dev eth0 parent 1:0 classid 1:2 htb rate 200kbps ceil 200kbps prio 2
tc class add dev eth0 parent 1:0 classid 1:3 htb rate 500kbps ceil 500kbps prio 2
#将两个类的默认的fifq队列规则改为sfq
tc qdisc add dev eth0 parent 1:2 handle 20 sfq
tc qdisc add dev eth0 parent 1:3 handle 30 sfq
#在网卡eth0上的1:0节点(对应qdisc中的handle 1:0)添加一个u32过滤规则,优先级为1,凡是目标地址是10.0.0.100的数据包都使用1:2类(对应classid为1:2的类)
tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 match ip dst 10.0.0.100 flowid 1:2
tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 match ip dst 10.0.0.101 flowid 1:3
如果还有其他用户例如用户C和D的ip是102、103,要求的下载带宽也要求500那么在加入
代码如下:
tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 match ip dst 10.0.0.102 flowid 1:3
tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 match ip dst 10.0.0.103 flowid 1:3