linux的dns被劫持,linuxdns被劫持
環境:ubuntu16.04
今天遇到dns被劫持的情況,在此記錄一下:
1.首先如何確定是否被劫持:
那麼查詢一個並不存在的網域名稱
nslookup notexit.comrrrr
如果返回了一個ip地址,說明dns被劫持了,假設此ip地址為:123.34.5.6
那麼用8.8.8.8網域名稱伺服器解析一下此錯誤網域名稱試試:
nslookup notexit.comrrrr 8.8.8.8
輸出的內容如下:
jello@jello:~$ nslookup notexit.comrrrr 8.8.8.8
Server:8.8.8.8
Address:8.8.8.8#53
** server can't find notexit.comrrrr: NXDOMAIN
提示內容說明此網域名稱並不存在
2.那麼如何處理這種情況呢?
由於ubuntu16.04使用dnsmasq對網域名稱進行解析,那麼修改dnsmasq的設定檔:
因為linux處理dns請求有個限制,在/etc/resolv.conf中只能配置三個dns地址,那麼乾脆在/etc/resolv.conf檔案中只保留localhost為網域名稱伺服器,然後建立一個儲存外部網域名稱伺服器地址的設定檔,並將該設定檔加入到dnsmasq的配置項resolv-file中,例如:
2.1建立設定檔/etc/resolv.my.conf,往其中填入內容如下:
nameserver 8.8.8.8
nameserver 8.8.4.4
2.2往/etc/dnsmasq.conf中添加以下內容:
resolv-file=/etc/resolv.my.conf
2.3重新啟動dnsmasq
systemctl restart dnsmasq
注意:如果重啟dnsmasq逾時,那麼就直接開啟/var/run/dnsmasq/resolv.conf
裡面有解析不存在網域名稱而得到的網域名稱伺服器地址,將存在該網域名稱伺服器地址的某行刪除即可,假設筆者的該檔案內容為:
nameserver 123.34.5.6
nameserver 231.43.5.45
那麼直接刪掉nameserver 123.34.5.6即可
最有效方案如下:
3.還可以直接使用其它的網域名稱伺服器地址替換掉這兩個ip,然後再重啟dnsmasq,筆者修改後/var/run/dnsmasq/resolv.conf設定檔內容如下:
nameserver 8.8.8.8
nameserver 8.8.4.4
重啟dnsmasq:
systemctl restart dnsmasq