MAC OS 10.x 转发80(http)端口的方法

2018-09-03  本文已影响0人  猪猪学上树

MAC OS 本质上还是 Unix 系统, Unix 系统大多默认情况下非root用户是无法使用小于1024的常用端口的.这时候如果你开发中需要在普通用户下用到80端口, 比如 tomcat, 比如 vitualbox 下构建了一个 http 服务, 若你想直接通过 浏览器的 localhost 访问的话(比用加上莫名其妙的”:端口”的话)你就需要做一些系统端口转发的工作.

MAC OS 10.10 上 pfctl 就可以做这一件事情, 详情请参见
man pfctl
或者
man pf.conf

具体操作如下:
首先在 /etc/pf.anchors/ 新建一个 http 文件内容如下:

rdr pass on lo0 inet proto tcp from any to any port 80 -> 127.0.0.1 port 8080
rdr pass on lo0 inet proto tcp from any to any port 443 -> 127.0.0.1 port 4443
rdr pass on en0 inet proto tcp from any to any port 80 -> 127.0.0.1 port 8080
rdr pass on en0 inet proto tcp from any to any port 443 -> 127.0.0.1 port 4443

检查其正确性:

$ sudo pfctl -vnf /etc/pf.anchors/http

修改 pf 的主配置文件 /etc/pf.conf 开启我们添加的锚点 http

pf.conf 对指令的顺序有严格要求,相同的指令需要放在一起,否则会报错 Rules must be in order: options, normalization, queueing, translation, filtering.

# 在 rdr-anchor "com.apple/*" 下添加
rdr-anchor "http-forwarding"

# 在 load anchor "com.apple" from "/etc/pf.anchors/com.apple" 下添加
load anchor "http-forwarding" from "/etc/pf.anchors/http"

最后导入并允许运行:

$ sudo pfctl -ef /etc/pf.conf

使用 -e 命令启用 pf 服务。使用 -E 命令强制重启 pf 服务:

$ sudo pfctl -E

使用 -d 命令关闭 pf:

$ sudo pfctl -d

从 Mavericks 起 pf 服务不再默认开机自启。如需开机启动 pf 服务,请往下看。

新版 Mac OS 10.11 EI Captian 加入了系统完整性保护机制,需重启到安全模式执行下述命令关闭文件系统保护。

$ csrutil enable --without fs

然后才能修改 /System/Library/LaunchDaemons/com.apple.pfctl.plist 文件实现开机自启用配置。

向 plist 文件中添加 -e 行,如下所示:

<string>pfctl</string>
<string>-e</string>
<string>-f</string>
<string>/etc/pf.conf</string>

参考

pfctl命令 http://man.linuxde.net/pfctl

上一篇 下一篇

猜你喜欢

热点阅读