Debian
Resolv.conf 修改破壞了 DNS
這是一個奇怪的問題,但我似乎無法解決它。
我正在創建一個視覺化使用者界面來修改 Debian Wheezy(以及其他偶然兼容的 debian 版本/衍生產品)上的網際網路設置。
我希望能夠根據使用者插入的內容修改 DNS。
按下“保存”後,將執行此程式碼:
void SaveDNSButton_event(object obj, ButtonPressEventArgs args) { //save dns settings string s1 = ""; string s2 = ""; string toWrite = s1 + s2; Console.WriteLine ("============="); Console.WriteLine ("Reading from resolv.conf before writing..."); using (StreamReader confReader = File.OpenText ("/etc/resolv.conf")) { StringReader sr = new StringReader (confReader.ReadToEnd ()); string line; toWrite = ""; while (null != (line = sr.ReadLine ())) { if (line.Contains ("nameserver")) { Console.WriteLine (line); } else { toWrite += line + Environment.NewLine; } } confReader.Dispose (); confReader.Close (); } s1 = "nameserver " + DNSentry1.Text + ";" + Environment.NewLine; s2 = "nameserver " + DNSentry2.Text + ";" + Environment.NewLine; Console.WriteLine ("============="); Console.WriteLine ("Writing to resolv.conf"); Console.WriteLine ("To write: " + toWrite + s1 + s2); using (StreamWriter confWriter = new StreamWriter ("/etc/resolv.conf", false)) { Console.WriteLine ("Writing..."); confWriter.Write (toWrite + s1 + s2); Console.WriteLine ("Closing file stream..."); confWriter.Dispose (); confWriter.Close (); } Console.WriteLine ("============="); Console.WriteLine ("Opening conf to confirm if it worked"); if (IsLinux) { Console.WriteLine ("Trying to open conf"); StreamReader file = File.OpenText ("/etc/resolv.conf"); string s = file.ReadToEnd (); Console.WriteLine(s); file.Dispose (); file.Close (); } }
相關部分是
using (StreamWriter confWriter = new StreamWriter ("/etc/resolv.conf", false)) { Console.WriteLine ("Writing..."); confWriter.Write (toWrite + s1 + s2); Console.WriteLine ("Closing file stream..."); confWriter.Dispose (); confWriter.Close (); }
resolv.conf
其中我用填寫的 DNS 資訊覆蓋。對此的範例輸入將類似於第一個對話框中的“8.8.8.8”和第二個對話框中的“8.8.4.4”。輸出將是…============= Reading from resolv.conf before writing... nameserver 192.168.2.101 nameserver 8.8.8.8 ============= Writing to resolv.conf To write: # Generated by NetworkManager domain trin-it.local search trin-it.local nameserver 8.8.8.8; nameserver 8.8.4.4; Writing... Closing file stream... ============= Opening conf to confirm if it worked 4 Trying to open conf # Generated by NetworkManager domain trin-it.local search trin-it.local nameserver 8.8.8.8; nameserver 8.8.4.4;
如果我在此之後 ping google,它只會顯示:“未知主機 google”
但是,如果我手動去
resolv.conf
那裡更改名稱伺服器,它實際上解決得很好。那是怎麼回事?/etc/resolv.conf
唯一的變化是,據我所知,我是通過程式碼而不是 nano來完成的。任何人都可以對此有所了解嗎?TL; 博士
為什麼使用程式碼編輯 resolv.conf 而不是手動編輯它?不會是一樣的吧?
在我看來,您的程式碼在名稱伺服器行的末尾添加了一個分號;不要那樣做。