zoukankan      html  css  js  c++  java
  • 本地配置DNS服务器(MAC版)

     转自
    https://www.cnblogs.com/skylor/p/7483959.html
     

    作为一个前端开发者,会遇到使用cookie的情况,常见的如:登录,权限控制,视频播放,图形验证码等,这时候本地开发者在PC上会使用修改hosts的方式添加指向本地的域名,来获取cookie的同域名。如:

    127.0.0.1 local.smartstudy.com  

    但是在移动端的时候,这一招就不好使了,苹果手机在没越狱的情况下是没法修改hosts的,难道为了这个让自己的爱机越个狱?答案应该是否定的。那么怎么处理这个问题呢?完美解决方案就是配置本地局域网的DNS服务。于是我就查询了一系列的文档文章,总结出这篇文章,踩过些许坑,希望后来人别在重复踩了,由于本人使用的是Mac,所以下面的一些命令亲和Mac党,其他系统也类似,只有写系统命令可能不同,废话不多说,君且详看:

    安装

    brew update; brew install dnsmasq;  

    启动

    sudo brew services start dnsmasq  

    重启

    sudo brew services restart dnsmasq  

    停止

    sudo brew services stop dnsmasq  

    查看dnsmasq的运行

    ps aux | grep dnsmasq  

    找到运行参数

    /usr/local/opt/dnsmasq/sbin/dnsmasq --keep-in-foreground -C /usr/local/etc/dnsmasq.conf

    找到运行配置文件:

    /usr/local/etc/dnsmasq.conf

    编辑他,这里用vim:

    sudo vim /usr/local/etc/dnsmasq.conf  

    干下面这些事情,去掉以下注释,并修改,具体配置是干啥的,后面会有讲到:

    no-resolv  
    no-poll  
    listen-address=127.0.0.1,172.17.7.115  
    addn-hosts=/etc/dnsmasq.hosts  

    添加自定义hosts文件dnsmasq.hosts

    sudo vim /etc/dnsmasq.hosts  

    里面添加你需要填写的域名对应关系,如:

    172.17.7.115 yongle.smartstudy.com  

    重启下你的DNS服务器,启动代码在文章上面有说到,截至目前,你的Mac本地的DNS配置就OK了,但是让同局域网的其他设备使用的话,还需要一些操作。

    修改路由器的DHCP

    配置DNS

    172.17.7.115  

    备用DNS配置为一般常用DNS服务器就好了。

    局域网内设备需重连WIFI,而后就可以使用你本地的DNS服务了,检测是否成功,可以使用dig命令,如你本机上可这么来:

    dig yongle.smartstudy.com @127.0.0.1  

    当你看到

    yongle.smartstudy.com.    0   IN  A   172.17.7.115  

    说明成功了,这时候应该就结束了,但若还有局域网内的设备连不上的话,可以查看下该设备的DNS是否含有172.17.7.115,若没有,重启wifi连接,就OK了。

    MAC配置DNS服务器

     

    1、brew install dnsmasq

    2、cp /usr/local/opt/dnsmasq/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf

    3、新建/usr/local/etc/resolv.dnsmasq.conf 内容为DNS列表

    4、修改dnsmasq.conf文件

    //配置上行DNS,对应no-resolv

    resolv-file=/usr/local/etc/resolv.dnsmasq.conf

    //resolv.dnsmasq.conf内的DNS寻址严格按照从上到下顺序执行,直到成功为止
    strict-order

    //缓冲大小
    cache-size=1024

    //192.168.x.x表示本机的ip地址,只有127.0.0.1的时候表示只有本机可以访问
    listen-address=127.0.0.1,192.168.x.x

    //DNS解析hosts时对应的hosts文件,对应no-hosts
    addn-hosts=/etc/hosts

    //表示不使用本机的hosts文件
    no-hosts

    //需要被解析的域名/期望解析结果
    address=/example.com/10.11.33.55

    5、brew services restart dnsmasq 重启dnsmasq服务(start启动)

    6、sudo killall -HUP mDNSResponder 刷新缓冲DNS

    说明:把本机DNS配置成127.0.0.1即可让本机也走此DNS服务器。

    第五步brew操作可以分解为:

    1、开机自运行

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

    2、命令重启

    sudo launchctl stop homebrew.mxcl.dnsmasq
    sudo launchctl start homebrew.mxcl.dnsmasq

  • 相关阅读:
    cookie,sessionStorage,localStorage
    存储方式与传输方式
    为什么css放在顶部而js写在后面
    常见的web安全及防护原理
    web缓存
    http协议理解
    http与https
    get/post的区别
    JZOJ 3571. 【GDKOI2014】内存分配
    JZOJ 3570. 【GDKOI2014】壕壕的寒假作业
  • 原文地址:https://www.cnblogs.com/it-tsz/p/9515478.html
Copyright © 2011-2022 走看看