LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1183|回复: 2

使用debian的iptables同时访问Linux(ssh)和局域网中的Windows(remote desktop)

[复制链接]
发表于 2008-3-19 13:59:44 | 显示全部楼层 |阅读模式
在装有双网卡的机器上安装Debian linux 4.0 (etch)。
用eth0连接到WAN,用eth1连接到LAN。

两个网卡的IP设置在/etc/network/interfaces文件中:
  1. # The loopback network interface
  2. auto lo
  3. iface lo inet loopback
  4. # The primary network interface
  5. allow-hotplug eth0
  6. iface eth0 inet dhcp
  7. # The secondary network interface
  8. auto eth1
  9. iface eth1 inet static
  10. address 192.168.45.1
  11. netmask 255.255.255.0
  12. up route add -host 255.255.255.255 dev eth1
复制代码
说明:我的WAN用dhcp分配IP地址。

安装dhcp服务器:sudo aptitude install dnsmasq
我想dhcpd也一样工作,好像dnsmasq配置简单一些:
/etc/dsnmasq.conf:
  1. dhcp-range=192.168.45.100,192.168.45.250,72h
  2. interface=eth1
复制代码
重启dhcp服务器:sudo /etc/init.d/dnsmasq restart
如果网络启动有问题请检查log日志文件监视安装情况:tail -f/var/log/daemon.log

这样你就可以看到LAN中的机器了。LAN中的机器可以看到192.168.45.1,
但是不能访问WAN。为了这个目的,需要使用iptables:

产生一个使用iptables前的备份:(好像是一个空文件)
  1. iptables-save > /root/iptables.backup/origin.itables.conf
  2. cp /etc/sysctl.conf /root/iptables.backup/_etc_sysctl.conf
复制代码
产生一个还原iptables的bash script代码/root/iptables.backup/reset_iptables:
  1. #! reset iptables sch that it looks originally
  2. iptables-restore /root/iptables.backup/origin.iptables.conf
  3. echo 0 > /proc/sys/net/ipv4/ip_forward
  4. for f in /proc/sys/net/ipv4/conf/*/rp_filter ; do echo 0 > $f ; done
  5. cp /root/iptables.backup/_etc_sysctl.conf /etc/sysctl.conf
复制代码

使用iptables设置规则如下:
  1. iptables -F
  2. iptables -t nat -F
  3. iptables -P INPUT ACCEPT
  4. iptables -P OUTPUT ACCEPT
  5. iptables -P FORWARD DROP
  6. export LAN=eth1
  7. export WAN=eth0
  8. iptables -I INPUT 1 -i ${LAN} -j ACCEPT
  9. iptables -I INPUT 1 -i lo -j ACCEPT
  10. iptables -A INPUT -p UDP --dport bootps -i ! ${LAN} -j REJECT
  11. iptables -A INPUT -p UDP --dport domain -i ! ${LAN} -j REJECT
  12. # for WAN ssh server
  13. iptables -A INPUT -p TCP --dport ssh -i ${WAN} -j ACCEPT
  14. iptables -A INPUT -p TCP -i ! ${LAN} -d 0/0 --dport 0:1023 -j DROP
  15. iptables -A INPUT -p UDP -i ! ${LAN} -d 0/0 --dport 0:1023 -j DROP
  16. iptables -I FORWARD -i ${LAN} -d 192.168.1.0/255.255.0.0 -j DROP
  17. iptables -A FORWARD -i ${LAN} -s 192.168.1.0/255.255.0.0 -j ACCEPT
  18. iptables -A FORWARD -i ${WAN} -d 192.168.1.0/255.255.0.0 -j ACCEPT
  19. iptables -t nat -A POSTROUTING -o ${WAN} -j MASQUERADE
  20. # for remote desktop
  21. iptables -A PREROUTING -t nat -i ${WAN} -p tcp --dport 3389 -j DNAT --to 192.168.45.181
  22. iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
  23. iptables -A FORWARD -i ${WAN} -p tcp --dport 3389 -j ACCEPT
  24. # iptables -A FORWARD -i eth0 -s a.b.c.d -p tcp --dport 3389 -j ACCEPT
  25. # where a.b.c.d is the address of your office machine - otherwise anyone will
  26. # be able to access your xp desktop :-)
复制代码

说明:以上命令用root执行。以#开始的是注释。192.168.45.181是局域网Windows的IP。
IP的转换(ip forwarding):
  1. echo 1 > /proc/sys/net/ipv4/ip_forward
  2. for f in /proc/sys/net/ipv4/conf/*/rp_filter ; do echo 1 > $f ; done
复制代码

这时LAN里的机器就可以访问internet了。

编辑/etc/sysctl.conf文件:
  1. net.ipv4.ip_forward = 1
  2. net.ipv4.conf.default.rp_filter=1
  3. net.ipv4.ip_dynaddr = 1
复制代码

保存iptables规则:
  1. iptables-save > /root/iptables.conf
复制代码
产生一个设置iptables的bash script代码/root/set_iptables:
  1. #! set iptables such that both ssh and rdesktop works
  2. iptables-restore /root/iptables.conf
  3. echo 1 > /proc/sys/net/ipv4/ip_forward
  4. for f in /proc/sys/net/ipv4/conf/*/rp_filter ; do echo 1 > $f ; done
  5. echo net.ipv4.ip_forward = 1           >  /etc/sysctl.conf
  6. echo net.ipv4.conf.default.rp_filter=1 >> /etc/sysctl.conf
  7. echo net.ipv4.ip_dynaddr = 1           >> /etc/sysctl.conf
复制代码

以后如果想还原iptables就执行
  1. sh /root/iptables.backup/reset_iptables
复制代码
如果想设置iptables使得ssh到Linux和remote desktop到Windows都能实现:
  1. sh /root/set_iptables
复制代码

参考文档:
http://www.gentoo.org/doc/en/home-router-howto.xml
http://www.debian-administration.org/articles/445
发表于 2008-3-19 19:57:47 | 显示全部楼层
安装shorewall软件就可以了,修改几个配置文件就可以了,手动设置iptables规则实在是太麻烦了。访问windows机子,windows机子都要设置一下才行,否则rdesk还是不行的,其中密码是必设的,否则登录不了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-3-22 14:25:26 | 显示全部楼层
favoyun兄好建议!欧要学学shorewall。现在一个小问题,不影响使用。当我ssh登陆时,多出一个确认反馈:
  1. Warning: the RSA host key for 'my.machine.com' differs from the key for the IP address '123.45.67.89'
  2. Offending key for IP in /home/me/.ssh/known_hosts:3
  3. Matching host key in /home/me/.ssh/known_hosts:7
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. me@my.machine.com's password:
复制代码
那位大虾有经验?
============================
解决了。本机的编码没有更新。删除.ssh/known_hosts文件就可以了。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表