ubuntu_最简单内网dns服务器搭建_最详细_最快的搭建

> dns服务器:ubuntu 16.04,ip地址为:192.168.1.102 > > 使用最傻瓜是的部署就是去 docker hub 拉一个 镜像下来,这里拉取的是 andyshinn/dnsmasq, [https://hub.docker.com/r/andyshinn/dnsmasq/](https://hub.docker.com/r/andyshinn/dnsmasq/) * * * ### 搭建dns服务器 1. docker 搜索 dnsmasq 镜像, 把 start 做多的拉下来 ``` root@ubuntu:~# docker search dnsmasq NAME DESCRIPTION STARS OFFICIAL AUTOMATED andyshinn/dnsmasq My dnsmasq brings all the boys to the yard... 100 [OK] devries/dnsmasq A dnsmasq server with support for a user d... 14 [OK] root@ubuntu:~# docker pull andyshinn/dnsmasq ``` 2. 创建 域名映射ip文件 和 dns文件,用来挂载到容器中,以后不用进入容器修改 域名映射,这里个文件的路径: `/home/wilker/Desktop/a_dns` 1. 域名映射ip文件 :my_dnsmasq_hosts ``` $ vi my_dnsmasq_hosts #加入一下内容 192.168.1.102 wilker.com 192.168.1.102 yun.com ``` 2. dns文件:my_resolv.dnsmasq ``` $ vi my_resolv.dnsmasq #加入一下内容 nameserver 114.114.114.114 nameserver 8.8.8.8 ``` 3. 可以用 run 方式 运行容器;也可以使用 yml 文件的方式启动,参照 [docker-compose.yml 启动](#docker-compose.yml 启动) ``` wilker@ubuntu:~/Desktop/a_dns$ docker run -d \ -p 192.168.1.102:53:53/tcp \ -p 192.168.1.102:53:53/udp \ -v /home/wilker/Desktop/a_dns/my_dnsmasq_hosts:/etc/my_dnsmasq_hosts \ -v /home/wilker/Desktop/a_dns/my_resolv.dnsmasq:/etc/my_resolv.dnsmasq \ --cap-add=NET_ADMIN \ --name my_dns_server \ andyshinn/dnsmasq wilker@ubuntu:~/Desktop/a_dns$ docker ps # 查看一下正在运行 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4d548b37b3a5 andyshinn/dnsmasq "dnsmasq -k" 20 minutes ago Up 10 minutes 192.168.1.102:53->53/tcp, 192.168.1.102:53->53/udp my_dns_server ``` * 这里挂载了刚才创建的两个文件 4. 进入容器中修改配置文件: ``` wilker@ubuntu:~/Desktop/a_dns$ docker exec -it my_dns_server /bin/sh / # vi /etc/dnsmasq.conf # 修改两处地方,都需要取消掉注释,并制定文件 addn-hosts=/etc/my_dnsmasq_hosts resolv-file=/etc/my_resolv.dnsmasq ``` 5. 退出 并 重启 容器 ``` / # exit wilker@ubuntu:~/Desktop/a_dns$ docker restart my_dns_server my_dns_server ``` 6. 修改路由上的dns设置,指定到这个 dns 服务器上,也就ip为 192.168.1.102,就不用内网每个机子都指定dns的ip地址了 > 我使用的路由器型号是 : TP-LINK-WR880N * 进入路由修改dns ![这里写图片描述](https://upload-images.jianshu.io/upload_images/5633283-694a9641937c9b41?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 7. 重启一下路由 和 网卡,查看一下dns,我这里在 window 机子上查看 ![这里写图片描述](https://upload-images.jianshu.io/upload_images/5633283-fe565baad095afae?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) * dns ok,然后访问下指定的两个域名 *wilker.com* 和 *yun.com* ,之前在 *192.168.1.102* 跑的 docker gitlab,可以参照这个 [docker_gitlab搭建.md](https://blog.csdn.net/yangxuan0261/article/details/docker_gitlab%E6%90%AD%E5%BB%BA.md) ![这里写图片描述](https://upload-images.jianshu.io/upload_images/5633283-e02e5182f8181b2a?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ​ > 后面如果需要添加域名预设,直接修改 */home/wilker/Desktop/a_dns/my_dnsmasq_hosts* 这个文件,然后重启 docker 容器即可 生效 > > ``` > wilker@ubuntu:~/Desktop/a_dns$vi /home/wilker/Desktop/a_dns/my_dnsmasq_hosts > wilker@ubuntu:~/Desktop/a_dns$ docker restart my_dns_server > ``` * * * ### 注意事项 * 貌似添加的域名不能有 **www.** 前缀,否则解析不到对应的 ip 地址 * window 刷新、查看 dns ``` C:\Users\Wilker>ipconfig /flushdns # 刷新 Windows IP 配置 已成功刷新 DNS 解析缓存。 C:\Users\Wilker>nslookup -q=ns www.wilker.com # 查看 服务器: wilker.com Address: 192.168.1.102 # 查看到的dns wilker.com primary name server = dns1.name-services.com responsible mail addr = info.name-services.com serial = 1474183225 refresh = 172800 (2 days) retry = 900 (15 mins) expire = 1814400 (21 days) default TTL = 3600 (1 hour) ``` * ubuntu 刷新、查看 dns ``` wilker@ubuntu:~/Desktop/a_dns$ nmcli dev show # 貌似 15.04 以上都用这个命令 GENERAL.DEVICE: ens33 GENERAL.TYPE: ethernet GENERAL.HWADDR: 00:0C:29:9C:F1:85 GENERAL.MTU: 1500 GENERAL.STATE: 100 (connected) GENERAL.CONNECTION: Wired connection 1 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/0 WIRED-PROPERTIES.CARRIER: on IP4.ADDRESS[1]: 192.168.1.102/24 IP4.GATEWAY: 192.168.1.1 IP4.ROUTE[1]: dst = 169.254.0.0/16, nh = 0.0.0.0, mt = 1000 IP4.DNS[1]: 192.168.1.102 # 查看到的dns IP4.DNS[2]: 114.114.114.114 IP4.DOMAIN[1]: DHCP IP4.DOMAIN[2]: HOST IP6.ADDRESS[1]: fe80::78a0:dcbe:df49:a11d/64 wilker@ubuntu:~/Desktop/a_dns$ sudo /etc/init.d/dns-clean # 刷新 wilker@ubuntu:~/Desktop/a_dns$ sudo /etc/init.d/networking restart # 最好重启一下网络 ``` * * * ### docker-compose.yml 启动 * 文件内容 ``` version: '2' services: registry: image: andyshinn/dnsmasq:latest container_name: my_dns_server tty: true cap_add: - NET_ADMIN ports: - 192.168.1.102:53:53/tcp - 192.168.1.102:53:53/udp volumes: - /home/wilker/Desktop/a_dns/my_dnsmasq_hosts:/etc/my_dnsmasq_hosts - /home/wilker/Desktop/a_dns/my_resolv.dnsmasq:/etc/my_resolv.dnsmasq ```

你可能感兴趣的:(ubuntu_最简单内网dns服务器搭建_最详细_最快的搭建)