Blog Email GitHub

05 Jul 2010
memcached与iptables

在客户端去连接memcached的服务端,很简单,一个tcp或者udp连接,不需要user和password。貌似很简单,很容易维护,没有了管理帐户的麻烦,但是很不安全。因为如果不加限制,任何一台计算机通过telnet ip port都可以访问memcached服务器,访问和修改数据。这是,就需要iptables对访问加以限制。

什么是iptables(官网)?iptables is the userspace command line program used to configure the Linux 2.4.x and 2.6.x IPv4 packet filtering ruleset. It is targeted towards system administrators. 我认为iptables就是计算机网络数据进出的一个负责人或者说一个门卫,对任何一个数据包,根据数据包的执行阶段(chain)和相关操作(table),执行相关的策略(target),当然需要对数据包进行一定的筛选,可以根据协议(protocal)、端口(port)、地址(ip)、数据包(state)状态等等去判断。

至于这些筛选规则和相关策略都可以设置,但是首先你必须是root。可以通过iptables或者iptables-restore去设置,也可以通过iptables-save或者iptables -L去查看当前的设置。(PS: 如果当前没有设置过规则,使用iptables-save是会报错的,iptables-save v1.4.2: Unable to open /proc/net/ip_tables_names: No such file or directory)

通过这次也终于搞明白了unix中经常提到的bind是何意?一台电脑是可以有多个ip的,比如123.0.0.1和x.x.x.x,在启动memcached的时候,可以bind到其中一个ip。客户端访问时,就必须访问服务器端绑定的ip,不然就会访问出错。

Resoures & References:

  1. netfiler
  2. iptables-save与iptables-restore
  3. iptables指南