国内dns服务商

  • dns.la
    泛解析 ttl=300s
  • zndns.com
    api接口不支持? ttl=10s
  • jd
    ttl=120s
  • wangsucloud.com
  • 万网
  • dnspod

都有一个特点:需要注册账号且绑定手机号

为什么要自建?

  • 修改记录的实时性
  • 高效,省去维护域名解析的重复劳动
  • 降本,节省泛解析的费用,有些服务商是收费的
  • 不想用手机号注册账号/实名认证,太麻烦

管理局域网的DNS、给手机App Store加速、纠正错误的DNS解析记录、保证上网更加安全、去掉网页讨厌的广告

选型

  1. bind9
    配置文件稍有门槛
    https://blog.oxida.cn/archives/103

  2. dnsmasq

    • 添加指定泛域名通过指定DNS服务器解析(防域名被劫持,或者转发指定域名解析)
    #增加配置 /etc/dnsmasp.conf
    server=/sohu.com/10.1.1.1
    
    • 添加指定泛域名解析成指定IP (可用来屏蔽特定的域名)
    #增加配置 /etc/dnsmasp.conf
    address=/baidu.com/2.2.2.2
    
    • 添加A记录
    #增加配置 /etc/dnsmasp.conf
    host-record=test13.test,10.10.10.13
    
    • 添加别名记录(需要先添加源地址解析记录,在添加别名记录)
        #增加配置 /data/dnsmasq/dnsmasq.d/test.test
        10.10.10.20   20.test.test
        #增加配置 /etc/dnsmasp.conf
        cname=10.test.test,20.test.test
    

    https://blog.csdn.net/qq_21127151/article/details/121203249
    https://blog.csdn.net/ethnicitybeta/article/details/122672861

  3. tankdns
    开箱即用,支持多平台,国产软件,简单易用,暂不支持泛解析

  4. coredns
    go语言实现的,插件化,也是不错的选择。http://t.zoukankan.com/flying607-p-12917376.html

最终选择了dnsmasq,因为系统自带,成本最低,同时配置简单易懂。

搭建dns服务

//停止之前的
kill -9 {PID}
// 重装下最新版
sudo apt install dnsmasq
// 修改/etc/dnsmasq.conf
address=/dev.honia.tk/2.2.2.2
host-record=honia.tk,2.2.2.2
//启动
dnsmasq -h

dns配置中ip自动更新

由于配置文件里的ip是写死的,所以我们需要实现一个功能:ip变化时,修改这里的ip。

update_dnsconf.sh脚本替换ip,文件缓存历史ip,重启服务

#!/usr/bin/env bash

base_dir='/var/log/freenom/'
out_file='dns_last_ip.cache'
ns1_file='freenom_ns1.honia.tk.ip4'
dns_conf_path='/etc/dnsmasq.conf'
# ns1的ip
currentIp=`cat "${base_dir}${ns1_file}" 2>/dev/null`
cacheIp=`cat "${base_dir}${out_file}" 2>/dev/null`
# echo "curr===> $currentIp"
# echo "cache===> $cacheIp"

if [[ "${currentIp}" == "" ]]; then
  exit 0
fi

if [[ "${currentIp}" != "${cacheIp}" ]]; then
  # echo "The two strings are different"
  oldIp=$(cat ${dns_conf_path} | egrep "address=/dev\.rchvip\.tk/.*" | sed -r "s/address=\/dev\.rchvip\.tk\/(.*)$/\1/g" | xargs echo -n)
  sed -i "s/${oldIp}/${currentIp}/g" "${dns_conf_path}"
  # update cache
  echo -n "$currentIp" > "${base_dir}${out_file}"
  # reload conf
  kill -9 "$(pgrep dnsmasq)"
  dnsmasq -h
fi

修改域名的nameserver

  1. 给自建的dns服务绑定域名
    点击顶部导航中的Services--->My Domains--->点击域名后的ManageDomain--->选择Manage Freenom DNS选项卡--->添加两个A记录,分别是ns1和ns2,ip是自建dns服务的公网ip
    因为填写nameserver必须得填域名且至少两个,所以要给我们的dns服务绑两个域名
  2. 修改域名的nameserver
    !注意!由于修改nameservers会导致freenom管理的域名解析记录失效,所以我们修改另一个域名的nameserver,也就是说修改解析ns1和ns2的域名的nameservers是不行的。
    点击另一个域名后的ManageDomain--->选择Management Tools--->Nameservers--->Use custom nameservers (enter below)--->填写ns1和ns1的域名,保存

当ns1能ping通的时候,也就是解析生效后,就实现泛解析了

dns开机自启动

编辑rc.localsudo vim /etc/rc.local,输入以下内容

#!/bin/bash
# rc.local config file created by use

# 把你需要执行的命令写在这里
dnsmasq -h

exit 0

赋予执行权限sudo chmod a+x /etc/rc.local

参考链接:https://wiki.deepin.org/zh/%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86/%E8%87%AA%E5%90%AF%E5%8A%A8%E7%A8%8B%E5%BA%8F

完犊子,我的公网ip只有家里网络和部分地区同运营商网络可以ping通,公司网络ping不通。

一般情况下,你不是直接访问公网的, 经过nat转换了,ping 公网ip的话也是ping到你出口路由ip, 并不会直接到你的pc。

可恶的宽带通运营商,最终只能先用 内网穿透 的方式了。
著名的内网穿透技术有frp,ngrok,可以自建。

列几个不错的免费的服务提供商

  • ngrok.cc 需实名
  • natfrp.com 无需实名
  • ngrok.io 无需实名,每次启动随机二级域名,80端口
  • natapp.cn 没试
  • freefrp.net 以前不需要,现在不知道
  • zerotier-one.com 组建局域网,需要安装客户端,严格来说这是另一种方案不属于内网穿透