请稍侯

手机连接Mac共享的WiFi的hosts配置问题

27 August 2017

手机连接Mac共享的WiFi的hosts配置问题

安装Homebrew

  1. Get Xcode;
  2. Get Command Line Tools. xcode-select --install;
  3. Install Homebrew. ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)";
  4. 其他辅助命令:

check install issue : brew doctor;

To search for an application:brew search ;

To install : brew install <application-name>;

To list all apps installed by Homebrew:brew list;

To remove an installed application:brew remove <application-name>;

To update Homebrew:brew update;

To see what else you can do: man brew;

Where does Homebrew install stuff …. in the Cellar:/usr/local/Cellar/;

  1. Updating from previous OSX Version

If you are updating from a previous OSX version you may run into file permission issues with El Capitan’s new SIP process .

To get around this you can change ownership on the Homebrew directory sudo chown -R $(whoami):admin /usr/local.

安装dnsmasq

brew install dnsmasq

安装完成后,在/usr/local/文件下新建一个etc文件夹。

把/usr/local/opt/dnsmasq/dnsmasq.conf.example文件拷贝至并重命名为/usr/local/etc/dnsmasq.conf。

编辑dnsmasq.conf,内容如下:

resolv-file=/etc/resolv.conf
strict-order
addn-hosts=/etc/hosts
cache-size=32768
listen-address=127.0.0.1,192.168.xx.154

其中,listen-address后面的可以是多个IP用英文逗号隔开。这里以我自己的IP地址为例,192.168.xx.154是我的Mac mini的内网IP,这样就可以实现同一个局域网内的设备,通过设置DNS为这个IP,来实现都通过我的Mac mini上的dnsmasq来查询dns,即局域网范围内的DNS泛解析。

重启dnsmasq应用

sudo launchctl stop homebrew.mxcl.dnsmasq
sudo launchctl start homebrew.mxcl.dnsmasq
sudo killall -HUP mDNSResponder

可通过以下代码设置开机自动运行dnsmasq:

sudo cp -fv /usr/local/opt/dnsmasq/*.plist /Library/LaunchDaemons
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

最后,使用iPhone连接Mac mini共享的wifi,同时设置dns为我的Mac mini的内网IP地址,即192.168.xx.154。这样再ping以下,就能看到效果

参考

Installing Homebrew on OS X El Capitan 10.11

在Mac OS X上安装dnsmasq来支持hosts泛解析