Mac電腦如何設置DHCP


通過設置MAC電腦的DHCP服務器可以為局域網內的其它設備自動分配IP地址,還能把物理地址和IP地址綁定起來,限制IP地址的獲取。

 
OS X 10.11或以上版本已內置DHCP服務器軟件,即bootpd。通過man bootpd命令即可查看這個命令的用法。
bootpd啟動時可提供2種模式,即DHCP和bootp,但默認情況不開啟DHCP而是開啟了Bootp服務,所以需要通過bootp -D把DHCP服務打開。
DHCP的配置包含以下步驟:
1.准備活動
2.設置開機啟動。通過LaunchDaemon程序設置開機啟動。
3.設置bootpd的運行方式。配置bootpd的2個配置文件bootps.plist和bootptab。bootps.plist用於設置dhcp或者bootp協議的運行方式,而bootptab用於設置地址池。
 
准備活動
配置DHCP服務前最重要的事情,就是把本機的DHCP客戶端設置成手動獲取IP地址的方式,因為DHCP客戶端無法從本機的DHCP服務端獲取IP地址。配置方法:系統偏好設定->網絡->以太網連接->在右邊下拉按鈕選擇手動,再填寫ip地址,注意不要與網關地址相同。
 
設置開機啟動
1)編輯啟動腳本。由於OS X10.11(OS X EI Capitan)上首次啟用了SIP(System Integrity Protection )模式,所有啟動腳本文件都設置了restricted位(通過ls -lO可查看restricted位),這導致即使是root用戶也沒有權限對這些文件進行任何修改,包括修改內容和文件權限。
為了修改啟動腳本,可先通過以下步驟關閉sip模式:
a)重啟 Mac,聽到啟動聲音后迅速按住 Command+R 鍵直到 Apple logo 出現,進入 Recovery Mode
b)點擊 Utilities > Terminal
c)在 Terminal 中輸入 csrutil disable,之后回車
d)重啟 Mac
2)成功關閉sip模式后就可以利用下面的XML代碼覆蓋系統目錄中的啟動腳本(/System/Library/LaunchDaemons/bootps.plist).

 

 

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3.  
    <plist version="1.0">
  4.  
    <dict>
  5.  
    <key>Disabled</key>
  6.  
    <true/>
  7.  
    <key>Label</key>
  8.  
    <string>com.apple.bootpd</string>
  9.  
    <key>Program</key>
  10.  
    <string>/usr/libexec/bootpd</string>
  11.  
    <key>ProgramArguments</key>
  12.  
    <array>
  13.  
    <string>/usr/libexec/bootpd</string>
  14.  
    <string>-Dv</string>
  15.  
    </array>
  16.  
    <key>Sockets</key>
  17.  
    <dict>
  18.  
    <key>Listeners</key>
  19.  
    <dict>
  20.  
    <key>SockFamily</key>
  21.  
    <string>IPv4</string>
  22.  
    <key>SockServiceName</key>
  23.  
    <string>bootps</string>
  24.  
    <key>SockType</key>
  25.  
    <string>dgram</string>
  26.  
    </dict>
  27.  
    </dict>
  28.  
    <key>inetdCompatibility</key>
  29.  
    <dict>
  30.  
    <key>Wait</key>
  31.  
    <true/>
  32.  
    </dict>
  33.  
    </dict>
  34.  
    </plist>

 

3)加載啟動腳本命令:sudo /bin/launchctl load -w /System/Library/LaunchDaemons/bootps.plist

4)去除啟動腳本命令:sudo /bin/launchctl unload -w /System/Library/LaunchDaemons/bootps.plist


設置bootpd的運行方式

1)bootpd有兩個配置文件,一個是bootps.plist用於指導dhcp和bootp服務的工作方式,另一個是bootptab,用於設置地址池。(注意,此處的bootps.plist與上一個步驟的plist文件同名,但作用完全不同,前者的目的是設置開機啟動bootpd程序,本文件的目地是指導bootpd程序的運行方式)

2)bootps.plist的路徑:/etc/bootps.plist

3)bootps.plist配置文件內容:

 

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3.  
    <plist version="1.0">
  4.  
    <dict>
  5.  
    <key>Subnets</key>
  6.  
    <array>
  7.  
    <dict>
  8.  
    <key>allocate</key>
  9.  
    <true/>
  10.  
    <key>allow</key>
  11.  
    <array>
  12.  
    <string>8C:FA:BA:01:E6:F3</string>
  13.  
    <string>6C:FD:B9:C8:7E:2B</string>
  14.  
    <string>B8:09:8A:83:22:A3</string>
  15.  
    <string>10:dd:b1:c6:b4:37</string>
  16.  
    <string>60:92:17:A2:C2:65</string>
  17.  
    <string>E0:06:E6:1B:AE:A0</string>
  18.  
    <string>E8:8D:28:DF:13:0C</string>
  19.  
    <string>64:09:80:D8:5B:98</string>
  20.  
    <string>b8:98:f7:11:a4:79</string>
  21.  
    <string>70:72:3C:53:CE:BA</string>
  22.  
    <string>00:0a:f5:10:fb:2c</string>
  23.  
    <string>b8:98:f7:aa:1e:0d</string>
  24.  
    <string>28:b2:bd:af:07:62</string>
  25.  
    <string>68:3e:34:4c:ca:cb</string>
  26.  
    <string>9C:44:3D:1E:68:F4</string>
  27.  
    <string>A8:15:4D:F5:4A:AC</string>
  28.  
    <string>68:3e:34:4c:ca:cb</string>
  29.  
    <string>54:EE:75:27:6C:D4</string>
  30.  
    </array>
  31.  
    <key>dhcp_domain_name</key>
  32.  
    <string>ns1.drugsales.com</string>
  33.  
    <key>dhcp_domain_name_server</key>
  34.  
    <string>192.168.1.1</string>
  35.  
    <key>dhcp_router</key>
  36.  
    <string>192.168.1.1</string>
  37.  
    <key>lease_max</key>
  38.  
    <integer>86400</integer>
  39.  
    <key>lease_min</key>
  40.  
    <integer>86400</integer>
  41.  
    <key>name</key>
  42.  
    <string>vulcan</string>
  43.  
    <key>net_address</key>
  44.  
    <string>192.168.1.0</string>
  45.  
    <key>net_mask</key>
  46.  
    <string>255.255.255.0</string>
  47.  
    <key>net_range</key>
  48.  
    <array>
  49.  
    <string>192.168.1.100</string>
  50.  
    <string>192.168.1.200</string>
  51.  
    </array>
  52.  
    </dict>
  53.  
    </array>
  54.  
    <key>bootp_enabled</key>
  55.  
    <false/>
  56.  
    <key>detect_other_dhcp_server</key>
  57.  
    <integer>1</integer>
  58.  
    <key>dhcp_enabled</key>
  59.  
    <array>
  60.  
    <string>en0</string>
  61.  
    </array>
  62.  
    <key>reply_threshold_seconds</key>
  63.  
    <integer>0</integer>
  64.  
    </dict>
  65.  
    </plist>
4)bootptab文件內容(路徑也是/etc/bootptab):

 

 

  1.  
    %%
  2.  
    # machine entries have the following format:
  3.  
    #
  4.  
    # hostname hwtype hwaddr ipaddr bootfile
  5.  
    vulcan-macmini 1 10:dd:b1:c6:b4:37 192.168.1.2
  6.  
    vulcan-iphone5s 1 60:92:17:A2:C2:65 192.168.1.100
  7.  
    tangling-pc 1 E0:06:E6:1B:AE:A0 192.168.1.101
  8.  
    tangling-iphone5 1 E8:8D:28:DF:13:0C 192.168.1.102
  9.  
    tangMum-xiaomi 1 64:09:80:D8:5B:98 192.168.1.103
  10.  
    iPad3 1 8C:FA:BA:01:E6:F3 192.168.1.104
  11.  
    vulcan-huawei 1 70:72:3C:53:CE:BA 192.168.1.105
  12.  
    vulcan-u2 1 00:0a:f5:10:fb:2c 192.168.1.106
  13.  
    vulcan-u4 1 b8:98:f7:aa:1e:0d 192.168.1.107
  14.  
    vulcan-win8-wifi 1 28:b2:bd:af:07:62 192.168.1.108
  15.  
    vulcan-win8-eth 1 54:EE:75:27:6C:D4 192.168.1.109
  16.  
    mum-mobile 1 d0:2d:b3:8e:48:4c 192.168.1.110
  17.  
    vulcan-meilan 1 68:3e:34:4c:ca:cb 192.168.1.111
  18.  
    android-tvbox 1 9C:44:3D:1E:68:F4 192.168.1.112
  19.  
    yangzhenxue-iphone5s 1 B8:09:8A:83:22:A3 192.168.1.113
  20.  
    yangzhenxue-hasee 1 6C:FD:B9:C8:7E:2B 192.168.1.114
5)手動啟動bootpd的命令:bootpd -Ddv,D表示要啟動DHCP模式,d表示前台運行並把調試信息重定向到標准輸出,v表示輸出更加全面的調試信息。

 

6)使用蘋果系統自帶的“控制台”工具程序可以查看bootpd的運行日志。

7)bootpd的日志文件輸出到/var/log/system.log文件中。

 

如果輸出以下調試信息,恭喜你,這說明bootpd的啟動是沒有問題的:

 

Jan  4 00:05:16 bogon bootpd[3185]: server starting

Jan  4 00:05:16 bogon bootpd[3185]: re-reading /etc/bootptab

Jan  4 00:05:16 bogon bootpd[3185]: Loaded 16 entries from bootptab (0 bad)

Jan  4 00:05:16 bogon bootpd[3185]: server name bogon

Jan  4 00:05:16 bogon bootpd[3185]: interface en0: ip 192.168.1.2 mask 255.255.255.0

 

常見錯誤信息:

1.bootpd[1091]: bind call failed: Permission denied

2.bootpd[1290]: bind call failed: Address already in use,說明地址已經被綁定,很可能有bootpd的另一個實例在運行。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM