在 Mac 系统上使用低于 1024 的端口
Mac 与 linux 一样,1024 以下的端口为特权端口,只有 root 用户才有权监听。因此,要在 mac 上使用 Web 服务软件监听 80 或 443 端口,要么以 root 用户启动应用程序,要么使用端口转发。
使用ipfw(Internet Protocol Firewall)设置端口转发
ipfw add 100 fwd 127.0.0.1,8080 tcp from any to any 80 in
不过,ipfw 工具在 mac 高版本里面已经不存在了。
使用pf(packet filter)设置端口转发
创建 anchor 文件
示例一:
/etc/pf.anchors/tomcat
rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port 80 -> 127.0.0.1 port 8080
示例二:
/etc/pf.anchors/virtualbox
rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port 80 -> 127.0.0.1 port 8080
rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port 443 -> 127.0.0.1 port 8443
测试 anchor 文件
pfctl -vnf /etc/pf.anchors/tomcat
pfctl -vnf /etc/pf.anchors/virtualbox
添加到主配置文件
pf启动时会自动装载/etc/pf.conf文件,因此将anchor文件链接到/etc/pf.conf,转发规则就会自动建立了。
rdr-anchor "tomcat-forwarding"
load anchor "tomcat-forwarding" from "/etc/pf.anchors/tomcat"
rdr-anchor "virtualbox-forwarding"
load anchor "virtualbox-forwarding" from "/etc/pf.anchors/virtualbox"
注意 要紧随文件中现有的 rdr-anchor 后面添加;最好按照原文内容分类添加较好
打开 pf
pf默认是关闭的。可以使用以下命令启动pf:
sudo pfctl -ef /etc/pf.conf
设置 pf 自启动
修改 LaunchDaemons 来使 pf 开机自动打开
<key>ProgramArguments</key>
<array>
<string>pfctl</string>
<string>-e</string>
<string>-f</string>
<string>/etc/pf.conf</string>
</array>
添加的为-e参数,即enable。 有一点一定要注意,-f和/etc/pf.conf这两个参数不能被打断,因为-f必须紧跟一个文件参数,所以说添加-e参数时不要打断-f参数,否则开机不会自动启动pf,切记。
跨接口转发
如果需要跨接口转发,则需设置系统参数: /etc/sysctl.conf
net.inet.ip.forwarding=1
net.inet6.ip6.forwarding=1
这与linux一致。