配置Linux 内核并利用iptables 做端口映射
创始人
2024-07-23 09:01:46
0

配置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

  以上配置只为端口映射准备,如果需要其它功能,请根据需要增加相关的配置。

  编译安装内核步骤略过。

  iptabes

  iptables 规则如下:

  iptables -t nat -A PREROUTING -p tcp –dport 11101 -d 192.168.1.100 -j DNAT –to-destination 192.168.2.101:22

#p#

  查看 iptables 规则定义:

  # iptables -t nat -L

  Chain PREROUTING (policy ACCEPT)

  target prot opt source destination

  DNAT tcp – anywhere 192.168.1.100 tcp dpt:11101 to:192.168.2.101:22

  Chain POSTROUTING (policy ACCEPT)

  target prot opt source destination

  Chain OUTPUT (policy ACCEPT)

  target prot opt source destination

  ip_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 = 1

  net.ipv4.conf.all.rp_filter = 1

  测试

  ssh root@192.168.1.100 -p 11101

  看看是不是能够连接到 192.168.2.101 主机上了?

通过上文的描述,我们清楚的知道了配置Linux内核和利用iptables 做端口映射的具体过程,希望对大家有帮助!

【编辑推荐】

  • iptables映射端口具体操作
  • 用iptables做地址映射
  • linux下清空所有iptables规则
  • 如何清空其中一条iptables规则
  • 如何清空iptables规则
  • 如何保存iptables配置
  • 保存iptables的防火墙规则的方法

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...