美文网首页
Mac端口转发

Mac端口转发

作者: 我真不是大牛 | 来源:发表于2018-07-04 17:58 被阅读0次

1. 问题

Mac 与 linux一样,1024以下端口为特权端口,只有root用户才有权监听。因此,要在mac上使用web服务软件监听80或433端口,要么以root用户启动应用程序,要么使用端口转发。

2. 使用ipfw(Internet Protocol Firewall)设置端口转发

//不过,ipfw工具在高版本mac里面已经不存在了
ipfw add 100 fwd 127.0.0.1,8080 tcp from any to any 80 in

3. 使用pf(packet filter)

1.创建anchor文件

anchor文件定义了我们想要转发的端口。

//文件位置
/etc/pf.anchors/<CUSTOM NAME>
//文件内容,可以添加多行以下格式内容
rdr pass on lo0 inet proto tcp from any to any port <source port> -> 127.0.0.1 port <destination port>
//如:8080 转发到80
//rdr pass on lo0 inet proto tcp from any to any port 80 -> 127.0.0.1 port 8080
2. 测试anchor文件
//测试命令
sudo pfctl -vnf /etc/pf.anchors/<CUSTOM NAME>

这是时候端口转发并未生效,只是检查anchor文件是否合法。如果看到以下输出结果,证明是有效的。

fctl: Use of -f option, could result in flushing of rules
present in the main ruleset added by the system at startup.
See /etc/pf.conf for further details.

rdr pass on lo0 inet proto tcp from any to any port = <SOURCE PORT> -> 127.0.0.1 port <DESTINATION PORT>
3. 创建pfctl config文件

anchor文件验证后,需要创建pfctl config文件。

//文件位置
/etc/pf-<CUSTOM NAME>.conf
//配置内容
rdr-anchor "forwarding"
load anchor "forwarding" from "/etc/pf.anchors/<CUSTOM NAME>"
4. 测试配置文件

可通过以下命令启动,停止pfctl

//启动
sudo pfctl -ef /etc/pf-<CUSTOM NAME>.conf
//停止 
sudo pfctl -df /etc/pf-<CUSTOM NAME>.conf

4. 端口转发启动生效

上面的命令可以根据需求启动和停止端口转发。另外如果想要机器启动自动开启端口转发,可以通过launchctl plist file。

  //文件位置
  /Library/LaunchDaemons/com.apple.pfctl-<CUSTOM NAME>.plist
  //文件内容
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
         <key>Label</key>
         <string>com.apple.pfctl-<CUSTOM NAME></string>
         <key>Program</key>
         <string>/sbin/pfctl</string>
         <key>ProgramArguments</key>
         <array>
              <string>pfctl</string>
              <string>-e</string>
              <string>-f</string>
              <string>/etc/pf-<CUSTOM NAME>.conf</string>
         </array>
         <key>RunAtLoad</key>
         <true/>
         <key>KeepAlive</key>
         <false/>
    </dict>
    </plist>
//添加到启动运行列表
sudo launchctl load -w /Library/LaunchDaemons/com.apple.pfctl-<CUSTOM NAME>.plist

相关文章

  • mac 下端口转发

    Mac下的端口转发 最近开始使用Mac来作为开发机了,还是有很多不习惯,比如端口转发这件小事,windows上可以...

  • mac 端口转发

    https://blog.csdn.net/u013771277/article/details/48104667...

  • Mac端口转发

    1. 问题 Mac 与 linux一样,1024以下端口为特权端口,只有root用户才有权监听。因此,要在mac...

  • mac设置端口转发

    前言 macos对于开发者已经相当友好了,使用brew可以很方便地搭建开发环境,可终究与线上有所不同,于是在本地用...

  • 【java】常用资源

    1.Mac下Tomcat如何将8080端口转发到80端口https://www.cnblogs.com/kaffe...

  • 5. 调试利器 - 端口转发 - ssh隧道技术

    说明 使用技术:** SSH隧道**端口转发分为 本地端口转发 和 远程端口转发。本地端口转发:将远程的端口映射到...

  • Docker-10 端口转发、容器卷 、网络、数据存储

    端口转发 使用端口转发解决容器端口访问问题 mysql应用端口转发: 查看本地地址: 运行容器:使用-p作端口转发...

  • Firewall端口转发

    添加端口转发 删除端口转发

  • SSH 端口转发

    SSH端口转发分为三种情况,分别为本地端口转发,远程端口转发以及动态端口转发.本文只介绍前两种. 什么是端口转发 ...

  • Linux下iptables的使用

    查看端口转发规则(80): 新增端口转发规则(80转发到8080): 删除端口转发规则(80转发到8080): 添...

网友评论

      本文标题:Mac端口转发

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