English 简体中文 繁體中文 한국 사람 日本語 Deutsch русский بالعربية TÜRKÇE português คนไทย french
查看: 19|回复: 0

linux端口转发-网络侧

[复制链接]
查看: 19|回复: 0

linux端口转发-网络侧

[复制链接]
查看: 19|回复: 0

24

主题

2

回帖

223

积分

管理员

积分
223
admin

24

主题

2

回帖

223

积分

管理员

积分
223
2025-1-10 14:51:10 | 显示全部楼层 |阅读模式
1、端口转发软件 redir   
        Linux 下的端口转发软件很多,像 ssh、iptables、socat 等等,但论简单易用,据我所知,还得算这个小小的 redir。它可以非常方便的把本机的TCP 端口转发给本机或其它机器的特定端口。如:redir :2022 :22,把本机的 2022 端口转发到本机的 22 端口。


例如:
1、把 A 的 2022 端口转发到 A 的 22 端口
redir :2022 :22
这样,在任何能够访问 A 的 2022 端口电脑上就能够访问到 A 的 22 端口了。
如果转发端口是1023 及以下,需要使用 root 权限,如 sudo redir :1023 :22
这是非常方便简单的。

2、把 A 的 8088 转发到 D 的 80 端口
命令如下:
redir -I centoshttp -n -l debug -s :8088 192.168.1.12:80
这时,用 sudo netstat -utpln 查看,就可以看到 redir 生成了一个 8088 的监听端口。这样,在任何能够访问 A 的 8088 端口电脑上就能够访问到 D 的 80 端口了。
其中参数含义如下:
-n 表示前台执行,一般调试的时候用,不加默认就是后台运行
-I、-l、-s 这3个参数都跟日志有关,其中 -s 表示把日志写入系统 /var/log/syslog 中,-l 表示日志等级,-I 是在 syslog 信息中用于区别进程日志的标识。
2、iptables

1、打开IP转发功能。

          linux的IP转发功能是默认关闭的,而且根据很多安全加固策略以及安全基线的要求,IP转发功能必须关闭,所以需要先打开IP转发。需要长期使用的话则写入到sysctl的配置文件中。
echo 1 >/proc/sys/net/ipv4/ip_forward
sysctl -w /etc/sysctl.conf


2、配置iptables

        在PERROUTING链中做DNAT。需要长期使用的话需要注意保存配置。

将与 80 端口的 TCP 连接转接到本地的 8080 端口上。使用 DNAT (Destination Network Address Translation) 技术可以满足这一要求。因为 iptables 在处理本地连接和远程连接的方法不同,所以需要分开处理。下面假设本机的 IP 是 192.168.4.177。
远程连接
远程连接指的是由另外一台机器连接到这台机器上。这种连接的数据包在 iptables 会首先经过 PREROUTING 链,所以只需在 PREROUTING 链中作 DNAT。


# iptables -t nat -A PREROUTING -p tcp -i eth0 -d 192.168.4.177 --dport 80 -j DNAT --to 192.168.4.177:8080

service iptables save

3、实例操作
        这里将本地接口IP 61.144.a.b 的3389端口 转发到 116.6.c.d的3389      (主要访问到61.144.a.b的3389端口,就会跳转到116.6.c.d的3389)
        1、 首先应该做的是/etc/sysctl.conf配置文件的 net.ipv4.ip_forward = 1 默认是0    这样允许iptalbes FORWARD。
        2、 service iptables stop  关闭防火墙
        3、 重新配置规则


iptables -t nat -A PREROUTING --dst 61.144.a.b -p tcp --dport 3389 -j DNAT --to-destination 116.
6.c.d:3389
iptables -t nat -A POSTROUTING --dst 116.6.c.d -p tcp --dport 3389 -j SNAT --to-source 61.144.a.b
service iptables save   
        将当前规则保存到 /etc/sysconfig/iptables
        若你对这个文件很熟悉直接修改这里的内容也等于命令行方式输入规则。
启动iptables 服务
service iptables start
  4、可以写进脚本,设备启动自动运行


# vi /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
</p>touch /var/lock/subsys/local

sh /root/myshipin.log
---------------------------------------------------------------------
vi myshipin.log
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

iptables -F -t nat
iptables -t nat -A PREROUTING --dst 61.144.a.b -p tcp --dport 3389 -j DNAT --to-destination 116.6.c.d:3389
iptables -t nat -A POSTROUTING --dst 116.6.a.b -p tcp --dport 3389 -j SNAT --to-source 61.144.c.d
~
----------------------------------------------------------------
TCP

iptables -t nat -A PREROUTING --dst 61.144.a.b -p tcp --dport 9304 -j DNAT --to-destination 10.94.a.b:9304
iptables -t nat -A POSTROUTING --dst 10.94.a.b -p tcp --dport 9304 -j SNAT --to-source 61.144.a.b


UDP
iptables -t nat -A PREROUTING --dst 61.144.a.b -p udp --dport 9305 -j DNAT --to-destination 10.94.a.b:9305
iptables -t nat -A POSTROUTING --dst 10.94.a.b -p udp --dport 9305 -j SNAT --to-source 61.144.a.b



3、firewall

1、打开端口转发功能

firewall-cmd --zone=public --add-forward-port=port=22222:proto=tcp:toport=22
在不增加toaddr的情况下,默认就是转发到自己的IP上。

如果需要长期使用则增加--permanent加入到永久规则即可。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|智能设备 | 粤ICP备2024353841号-1

GMT+8, 2025-3-10 15:18 , Processed in 1.183299 second(s), 26 queries .

Powered by 智能设备

©2025

|网站地图