美文网首页
linux 端口转发:iptables

linux 端口转发:iptables

作者: 晓得为_ | 来源:发表于2018-01-23 17:40 被阅读0次

    端口转发

    iptables 是一个配置 Linux 内核 防火墙 的命令行工具,是 netfilter 项目的一部分。
    术语 iptables 也经常代指该内核级防火墙。
    iptables 用于 ipv4,ip6tables 用于 ipv6。
    需要root账户执行以下操作

    应用场景

    外网通过端口转发的方式;访问仅内网有访问权限的mySql;

    1.开启配置 iptables

    临时修改:

    $ echo 1 >/proc/sys/net/ipv4/ip_forward
    

    默认值0是禁止ip转发,修改为1即开启ip转发功能。

    2.简单转发

    #-- 把访问本机 8091 端口的请求转发到 8090端口
    $ iptables -t nat -A PREROUTING -p tcp --dport 8091 -j REDIRECT --to-ports 8090
    #-- 把访问本机 8093 端口的请求转发到 192.168.1.3 的 8090端口
    $ iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 8093 -j DNAT --to 192.168.1.3:8090
    
    $ service iptables save
    $ service iptables restart
    

    3.配置转发mySql

    mySql:服务器 : 192.168.1.3:3306
    配置转发的机器:有mySql访问权限,外网IP(193.168.1.2),内网IP(192.168.1.2),可用端口(3307)

    $ echo 1 >/proc/sys/net/ipv4/ip_forward
    $ iptables -t nat -A PREROUTING -p tcp --dport 3307 -j DNAT --to-destination 192.168.1.3:3306
    $ iptables -t nat -A POSTROUTING -d 192.168.1.3 -p tcp --dport 3306 -j SNAT --to 192.168.1.2
    $ service iptables save
    $ service iptables restart
    

    访问mySql

    -- 外网连接mysql 也可通过 navicat 等工具链接
    mysql -h193.168.1.2 -uroot -proot -P 3307
    -- 其它没有mySql访问权限的机器 内网访问
    mysql -h192.168.1.2 -uroot -proot -P 3307
    

    相关文章

      网友评论

          本文标题:linux 端口转发:iptables

          本文链接:https://www.haomeiwen.com/subject/somlmxtx.html