本节中将重点介绍下iptables的配置,这可是保证网络安全的利器,iptables是基于内核的防火墙,内置了filter,nat和mangle三张表,filter负责过滤数据包,nat则涉及到网络地址转换;mangle表则主要应用在修改数据包内容上,一般很少使用它;默认的规则链有:INPUT,OUTPUT,NAT,POSTROUTING,PREROUTING;下面的两张图片很好的说明了iptables防火墙的工作机制,关于详细的使用和介绍可以参考man文档
一:服务器同客户端网络的设定
[root@server ~]# ifconfig |grep 'inet addr' |cut -d ':' -f 2 |cut -d ' ' -f 1 //查看服务ip和mac地址
10.0.0.200
192.168.100.254
127.0.0.1
[root@server ~]# ifconfig eth1 |grep HWaddr
eth1 Link encap:Ethernet HWaddr 00:0C:29:0C:7C:4E
[root@server ~]# grep 'ip_forward' /etc/sysctl.conf //开启服务器端路由功能并使其生效
net.ipv4.ip_forward = 1
[root@server ~]# sysctl -p
[root@server ~]# ping www.idcfree.com -c 2 //测试同公网的连接
PING www.6688.cc (119.75.213.51) 56(84) bytes of data.
64 bytes from 119.75.213.51: icmp_seq=1 ttl=53 time=71.6 ms
64 bytes from 119.75.213.51: icmp_seq=2 ttl=53 time=67.2 ms
[root@client ~]# ifconfig |grep 'inet addr' |cut -d ':' -f 2 |cut -d ' ' -f 1 //客户端的ip和路由设定
192.168.100.20
127.0.0.1
[root@client ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 192.168.100.254 0.0.0.0 UG 0 0 0 eth0
[root@server ~]# ping 192.168.100.20 -c 2 //在服务器端测试同客户端的连接
PING 192.168.100.20 (192.168.100.20) 56(84) bytes of data.
64 bytes from 192.168.100.20: icmp_seq=1 ttl=64 time=5.92 ms
64 bytes from 192.168.100.20: icmp_seq=2 ttl=64 time=1.12 ms