Mac에서 NAT로 인터넷 공유하기

IP 대역은 192.168.2.0/24 로 가정하고, Gateway는 192.168.2.1로 한다. 모든 작업은 root 권한으로 진행한다.

인터넷이 연결된 회선 확인

netstat -nr | grep default | awk '{ print $6 }' | head -1

Bridge 하나 만들고, 여기에 연결 시키고자 하는 장비를 추가한다.

ifconfig bridge1 create ## 이름은 자유롭게 할 수 있다.
ifconfig bridge1 addm en7 # 외부 장비와 연결하고자 하는 Ethernet Device
ifconfig bridge1 up
ifconfig bridge1 192.168.2.1 # Gateway IP
route add default -interface bridge1 -ifscope bridge1 -cloning

IP Forwarding을 활성화 시키기

sysctl -w net.inet.ip.forwarding=1

/etc/pf.conf 수정

여기서 en4는 인터넷이 연결된 장비임

...
rdr-anchor "com.apple/*"
nat on {en4} proto {tcp, udp, icmp} from bridge1:network to any -> {en4}
pass from {lo0, bridge1:network} to any keep state
dummynet-anchor "com.apple/*"
...

pf.conf를 적용

pfctl -vvv -f /etc/pf.conf -E

재부팅 후에도 적용 시키고 싶다면?

/usr/libexec/PlistBuddy -c 'add :ProgramArguments:3 string -e' /System/Library/LaunchDaemons/com.apple.pfctl.plist

(optional) Mac에 내장된 DHCP를 이용해 동적으로 IP를 할당하기

bootpd.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>bootp_enabled</key>
	<false/>
	<key>detect_other_dhcp_server</key>
	<false/>
	<key>dhcp_enabled</key>
	<array>
		<string>bridge1</string>
	</array>
	<key>reply_threshold_seconds</key>
	<integer>0</integer>
	<key>Subnets</key>
	<array>
		<dict>
			<key>allocate</key>
			<true/>
			<key>lease_max</key>
			<integer>86400</integer>
			<key>lease_min</key>
			<integer>86400</integer>
			<key>name</key>
			<string>192.168.2</string>
			<key>net_address</key>
			<string>192.168.2.0</string>
			<key>net_mask</key>
			<string>255.255.255.0</string>
			<key>net_range</key>
			<array>
				<string>192.168.2.2</string>
				<string>192.168.2.254</string>
			</array>
			<key>dhcp_router</key>
			<string>192.168.2.1</string>
			<key>dhcp_domain_name_server</key>
			<array>
				<string>8.8.8.8</string>
			</array>
		</dict>
	</array>

</dict>
</plist>

아래 명령으로 DHCP를 활성화 시킨다.

/bin/launchctl unload -w /System/Library/LaunchDaemons/bootps.plist
/bin/launchctl load -w /System/Library/LaunchDaemons/bootps.plist

접속 장비 확인하기

$ arp -i bridge1 -l -a
Neighbor                Linklayer Address Expire(O) Expire(I)    Netif Refs Prbs
192.168.2.1             ae:87:a3:d0:b:1   (none)    (none)     bridge1
192.168.2.2             c:fe:45:bd:6:bf   26s       26s        bridge1    1
192.168.2.255           (incomplete)      (none)    (none)     bridge1

참고 사이트

Back