Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / 配置Linux 内核并利用iptables 做端口映射

主机 IP:192.168.1.100目标机 IP:192.168.2.101要求将到主机 192.168.1.100:11101 的请求映射到内部网目标机的 sshd 服务端口上,即:192.168.2.101:22。配置Linux内核(以 2.6.18 为例)如果执行 iptable -L 出现以下信息,那么就需要重新配置和编译内核:iptables v1.4.2: can"t initialize iptables table `filter": Table does not exist (do you need to insmod?)Perhaps iptables or your kernel needs to be upgraded.配置选项:Networking —->Networking options —->[*] Network packet filtering (replaces ipchains)  —>Core Netfilter Configuration  —><*> Netfilter Xtables support (required for ip_tables)IP: Netfilter Configuration —><*> Connection tracking (required for masq/NAT)<*> IP tables support (required for filtering/masq/NAT)<*>   IP range match support<*>   Packet filtering<*>     REJECT target support<*>   Full NAT以上配置只为端口映射准备,如果需要其它功能,请根据需要增加相关的配置。编译安装内核步骤略过。iptabesiptables 规则如下:iptables -t nat -A PREROUTING -p tcp –dport 11101 -d 192.168.1.100 -j DNAT –to-destination 192.168.2.101:22查看 iptables 规则定义:# iptables -t nat -LChain PREROUTING (policy ACCEPT)target     prot opt source               destinationDNAT       tcp  –  anywhere             192.168.1.100       tcp dpt:11101 to:192.168.2.101:22Chain POSTROUTING (policy ACCEPT)target     prot opt source               destinationChain OUTPUT (policy ACCEPT)target     prot opt source               destinationip_forward除此之外,需要在主机上打开 ip 转发以保持连接通道。查看是否已打开 ip 转发(1 表示打开):cat /proc/sys/net/ipv4/ip_forward如果未打开,则用以下命令打开:echo 1 > /proc/sys/net/ipv4/ip_forward保存设置以上 iptables 设置和 ip 转发设置在重启系统之后就会消失,因此如果有需要,请将设置保存。保存 iptables 设置:/etc/init.d/iptables save设置系统启动时自动加载 iptables 设置(以 gentoo 为例):rc-update add iptables default保存 ip_forward 设置(在 /etc/sysctl.conf 中设置):net.ipv4.ip_forward = 1安全隐患在打开了 ip_forward 后,一般要同时打开 rp_filter (Reverse Path filter),对数据包的源地址进行检查。如果在没有打开这个设置,就很容易受到来自内部网的 IP 欺骗。打开 rp_filter:for f in /proc/sys/net/ipv4/conf/*/rp_filter ; do echo 1 > $f ; done保存设置(在 /etc/sysctl.conf 中设置):net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.all.rp_filter = 1测试ssh root@192.168.1.100 -p 11101看看是不是能够连接到 192.168.2.101 主机上了?作者:Hily 原始链接:http://hily.me/blog/2009/02/linux-kernel-iptables-port-mapping/版权声明:可以转载,转载时务必以超链接形式标明文章原始出处和作者信息及版权声明红帽企业Linux 4防火墙配置Linux更安全“猫癣”瞄准Windows新漏洞相关资讯      Linux内核 
  • IT人员必须了解的六项Linux内核变  (今 12:05)
  • Linux 内核更新:3.10.98、3.14.62  (02月26日)
  • Linux:让手机运行主线内核  (11/26/2015 22:16:17)
  • Linux内核自防护项目  (05月24日)
  • Linux 内核架构的理解  (12/09/2015 09:01:01)
  • Linux内核被指缺乏安全性  (11/07/2015 08:28:47)
本文评论 查看全部评论 (1)
表情: 姓名: 字数


评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款