Skip to content

Instantly share code, notes, and snippets.

@DavydLiu
Last active January 17, 2019 16:13
Show Gist options
  • Save DavydLiu/ba2e2f7f01dd13902f47 to your computer and use it in GitHub Desktop.
Save DavydLiu/ba2e2f7f01dd13902f47 to your computer and use it in GitHub Desktop.
Mac Port forwarding on OS X Yosemite 10.10 and above

##1. Create the anchor file:

sodu vim /etc/pf.anchors/com.liuxingruo

Inside the anchor file, enter:

rdr pass on lo0 inet proto tcp from any to self port 80 -> 127.0.0.1 port 9191
rdr pass on en0 inet proto tcp from any to any port 80 -> 127.0.0.1 port 9191
rdr pass on en1 inet proto tcp from any to any port 80 -> 127.0.0.1 port 9191

Make sure to add a newline at the end of this file.

##2. Test the anchor file:

sudo pfctl -vnf /etc/pf.anchors/com.jenkins

##3. Add the anchor file to the pf.conf file:

sudo vim /etc/pf.conf

Load the anchor file we previously created, make sure to add these entries to the appropriate spot.

#
# com.apple anchor point
#
scrub-anchor "com.apple/*"
nat-anchor "com.apple/*"
rdr-anchor "com.apple/*"
rdr-anchor "com.liuxingruo" // new line
dummynet-anchor "com.apple/*"
anchor "com.apple/*"
load anchor "com.apple" from "/etc/pf.anchors/com.apple"
load anchor "com.liuxingruo" from "/etc/pf.anchors/com.liuxingruo" // new line

##4. Load and enabling pf

  • To manually enable it (no restart required)

sudo pfctl -ef /etc/pf.conf

  • To enable it automatically after each restart

sudo vim /System/Library/LaunchDaemons/com.apple.pfctl.plist

<key>ProgramArguments</key>
<array>
    <string>pfctl</string>
    <string>-e</string> <!-- new line -->
    <string>-f</string>
    <string>/etc/pf.conf</string>
</array>
@yanxurui
Copy link

yanxurui commented Dec 3, 2016

👍 It works thank you very much. But there are still 2 insignificant errors:

  1. typo:the first sodu
  2. add rdr rule to com.liuxingruo but test com.jenkins

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment