ÔõÑùÔÚ Linux ÉÏÉó²éºÍˢРDNS »º´æ
ͨ¹ýË¢ÐÂÍâµØ dns »º´æ£¬Äú¿ÉÒÔ½â¾ö http ¹ýʧ²¢±£»¤×Ô¼ºÃâÊÜ dns ÓÕƵÄÓ°Ïì¡£ÒÔÏÂÊÇÔÚ linux ÉÏÖ´Ðд˲Ù×÷µÄÒªÁì¡£
µ±ÄúʹÓÃÓòÃû»á¼ûÍøվʱ£¬ÄúµÄϵͳ»áÏò DNS ЧÀÍÆ÷·¢ËÍÇëÇóÒÔ»ñÈ¡¸ÃÓòµÄ IP µØµã¡£´ËÓò IP µØµã¶ÔÉúÑÄÔÚ DNS »º´æÖй©ÒÔºóʹÓã¬Òò´ËÄú²»±Øÿ´Î¶¼Ïò DNS ЧÀÍÆ÷·¢ËÍÇëÇóÒÔ½¨ÉèÅþÁ¬¡£
µ«ÓÐʱ£¬ÍâµØ DNS »º´æ»áË𻵲¢µ¼Ö HTTP ¹ýʧ¡£ÖµµÃÇìÐÒµÄÊÇ£¬ÔÚ Linux ²Ù×÷ϵͳÖÐˢкÍÖØÐÞ DNS »º´æºÜÊÇÈÝÒס£ÕâÊÇÔõÑù×öµ½µÄ¡£
ΪʲôҪÔÚ Linux ÉÏˢРDNS »º´æ£¿
Äú¿ÉÄÜÏëÒªÖØÐÞ´æ´¢ÔÚϵͳÉ쵀 DNS »º´æµÄÔµ¹ÊÔÓÉÓжàÖÖ¡£ÈôÊÇÄúµÄDNS¼Í¼¹ýʱÁË£¬Äú¿ÉÄÜÏëÒª´ÓDNSЧÀÍÖØÊÓлñÈ¡Ëü¡£ÁíÍ⣬ÈôÊÇÄúµ£ÐÄϵͳÊܵ½ÍþвÁË£¬Äú¿ÉÄÜÐèҪȷ±£DNS»º´æδ±»¸Ä¶¯£¬Ò²±»³ÆΪDNSÓÕÆ¡£
µ±ÄúˢРDNS »º´æʱ£¬ÏµÍ³±ØÐèÔÙ´Î ping DNS ЧÀÍÆ÷²¢´ÓÖлñȡеÄÓò IP µØµã¼Í¼£¬´Ó¶øÔÚ´ËÀú³ÌÖÐɾ³ýÈκιýʱ»òÊÜËðµÄÊý¾Ý¡£
ÔõÑùÔÚ Linux ÉÏÉó²éÍâµØ DNS »º´æ
ÔÚsystemd֮ǰ£¬´ó´ó¶¼Linux¿¯Ðа涼ûÓÐϵͳ¹æÄ£µÄDNS»º´æ£¬³ý·ÇÏñdnsmasq»ònscdÕâÑùµÄ³ÌÐòÊÇÊÖ¶¯ÉèÖõġ£systemd ¸½´ø systemd-solved£¬ÕâÊÇÒ»ÏÓòÃûÆÊÎöΪ IP µØµã²¢»º´æ DNS ÌõÄ¿µÄЧÀÍ¡£
ÒÔϸ÷½Ú½«Ö¸µ¼ÄúÔõÑùÉó²éÓÉ systemd ÆÊÎö¡¢nscd ºÍ dnsmasq ÌìÉúµÄ DNS »º´æÄÚÈÝ£¬ÒÔ±ãÄúÔÚ¾öÒéˢлº´æÊý¾Ý֮ǰÏàʶ»º´æµÄÊý¾Ý¡£
Éó²éϵͳÆÊÎöµÄ DNS »º´æ
ÒªÉó²é systemd ÆÊÎöµÄ»º´æ¼Í¼£¬ÄúÐèÒªÏÈÔÝʱÖÕֹЧÀÍ£¬È»ºó½«ÆäÈÕÖ¾µ¼³öµ½ÎļþÖС£
Ê×ÏÈ·¢ËÍ SIGUSR1 ÐźÅÒÔÖÕÖ¹ systemd ÆÊÎöµÄЧÀÍ£º
linuxmi@linuxmi ~/www.linuxmi.com % sudo killall -USR1 systemd-resolved[sudo] linuxmi µÄÃÜÂ룺
µÇ¼ºó¸´ÖÆ
ʹÓà journalctl ÏÂÁîºÍ±ê×¼Êä³öÔËËã·û½«Êä³öÉúÑĵ½Îı¾Îļþ£º
linuxmi@linuxmi ~/www.linuxmi.com % sudo journalctl -u systemd-resolved > ~/cache.txtlinuxmi@linuxmi ~/www.linuxmi.com
µÇ¼ºó¸´ÖÆ
È»ºó£¬Äú¿ÉÒÔʹÓà Vim µÈÎı¾±à¼Æ÷Éó²éÎļþµÄÄÚÈÝ£º
ÔÚÎļþÖУ¬ËÑË÷¡°CACHE£º¡±£¬ÒªÁìÊÇ°´ Escape£¬¼üÈë¡°/CACHE£º¡±£¬È»ºó°´ Enter ¼ü¡£ÔÚ¡°CACHE£º¡±Ï·½ÁгöµÄËùÓÐ DNS ¼Í¼¶¼°üÀ¨ÔÚÍâµØ DNS »º´æÖС£ÈôÄúÔÚʹÓà Vim£¬Çë°´Ï n ¼üÒÔÌøתÖÁÏÂÒ»×é DNS ÌõÄ¿¡£
Éó²é nscd µÄÍâµØ DNS »º´æ
ÒªÉó²é nscd ÌìÉúµÄÍâµØ»º´æ£¬ÄúÐèҪʹÓà string ÏÂÁî¶ÁÈ¡ nscd Ö÷»úÊý¾Ý¿âµÄÄÚÈÝ¡£
ÔÚ»ùÓÚ Debian ºÍ Ubuntu µÄ¿¯ÐаæÉÏ£¬´ËÎļþλÓÚ /var/cache/nscd/hosts¡£ÔËÐÐÒÔÏÂÏÂÁîÒÔÉó²éÎļþ£º
linuxmi@linuxmi ~/www.linuxmi.com % sudo strings /var/cache/nscd/hosts | uniq
µÇ¼ºó¸´ÖÆ
ÒªÉó²éÓÐ¹Ø nscd DNS ¸ßËÙ»º´æµÄÒ»Ñùƽ³£Í³¼ÆÐÅÏ¢£¬ÇëʹÓà -g ±ê¼Ç£º
linuxmi@linuxmi ~/www.linuxmi.com % sudo nscd -g
µÇ¼ºó¸´ÖÆ
ÏÔʾÓÉ dnsmasq ÌìÉúµÄ DNS »º´æ
»ñÈ¡È·Çеļͼ²¢½ûÖ¹Ò×£¬ÓÉÓÚ dnsmasq ÔÚÄÚ´æÖд洢 DNS »º´æ¡£Äú¿ÉÒÔ·¢ËÍÖÕÖ¹ÐźŸø dnsmasq ²¢¼Í¼ÆäÊä³ö£¬ÒÔ»ñÈ¡´¦Öóͷ£µÄ DNS ÅÌÎÊÊýÄ¿¡£
Ϊ´Ë£¬Ê×ÏÈ£¬Ê¹Óà systemctl ÏÂÁîÈ·±£ dnsmasq ÒÑÆô¶¯²¢ÕýÔÚÔËÐУº
linuxmi@linuxmi ~/www.linuxmi.com % sudo systemctl status dnsmasq
µÇ¼ºó¸´ÖÆ
ÈôÊÇ״̬ÏÔʾ¡°Ô˶¯ Active¡±£¬ÇëÔËÐÐÒÔÏÂÏÂÁîÒÔÖÕÖ¹¸ÃЧÀÍ£º
linuxmi@linuxmi ~/www.linuxmi.com % sudo pkill -USR1 dnsmasq
µÇ¼ºó¸´ÖÆ
ʹÓà journalctl ÏÂÁÌáÈ¡ dnsmasq ÈÕÖ¾²¢½«ËüÃÇÉúÑĵ½Îı¾ÎļþÖУº
linuxmi@linuxmi ~/www.linuxmi.com % sudo journalctl -u dnsmasq > ~/cache.txt
µÇ¼ºó¸´ÖÆ
×îºó£¬Ê¹ÓÃÎļþÉó²éÊÊÓóÌÐò£¨Èç cat »ò¸üÉÙ£©Éó²éÎļþµÄÄÚÈÝ£º
linuxmi@linuxmi ~/www.linuxmi.com % cat ~/cache.txt
µÇ¼ºó¸´ÖÆ
ÔõÑùÔÚ Linux ÉÏˢРDNS »º´æ
ˢРDNS »º´æÒâζ×Å´ÓÅÌËã»úÖÐɾ³ý»º´æµÄ DNS ¼Í¼¡£Õ⽫ÆÈʹËüÏòDNSЧÀÍÆ÷·¢ËÍÇëÇó£¬ÒÔ»ñȡеÄDNSÌõÄ¿¡£
ÒÔÏÂÊÇÔÚ Linux ÉÏˢРDNS »º´æµÄÒªÁ죺
ʹÓà systemd ÆÊÎö
Äú¿ÉÒÔʹÓà resolvectl ÏÂÁîˢРsystemd ÆÊÎö´æ´¢µÄ DNS »º´æ£º
linuxmi@linuxmi ~/www.linuxmi.com % sudo resolvectl flush-caches
µÇ¼ºó¸´ÖÆ
ÈôÊÇÄúÔËÐеÄÊÇ Ubuntu 17.04 »ò 18.04£¬ÇëʹÓà systemd ÆÊÎöµÄÏÂÁîˢлº´æ£º
sudo systemd-resolved --flush-caches
µÇ¼ºó¸´ÖÆ
ÔÚ Linux ÉÏˢРnscd DNS »º´æ
ɾ³ý nscd µÄ DNS »º´æµÄ×î±ã½ÝÒªÁìÊÇÖØÐÂÆô¶¯Ð§ÀÍ¡£Äú¿ÉÒÔͨ¹ýÔËÐÐÒÔÏÂÏÂÁîÀ´Ö´Ðд˲Ù×÷£º
linuxmi@linuxmi ~/www.linuxmi.com % sudo /etc/init.d/nscd restart
µÇ¼ºó¸´ÖÆ
ÈôÊÇÕâ²»Æð×÷Óã¬Ê×ÏÈ£¬¼ì²é´æ´¢ÔÚPCÉϵÄÍâµØ»º´æÊÇ·ñ³¤ÆÚ¡£Äú¿ÉÒÔʹÓà -g ±ê¼Ç¾ÙÐÐÑéÖ¤£º
sudo nscd -g
µÇ¼ºó¸´ÖÆ
ÈôÊÇÊÇÕâÖÖÇéÐΣ¬ÇëʹÓôøÓÐ nscd ÏÂÁîµÄ ¨Ci ±ê¼ÇÀ´É¨³ý¼Í¼£¨i ´ú±í invalidate£©£º
linuxmi@linuxmi ~/www.linuxmi.com % sudo nscd -i hosts
µÇ¼ºó¸´ÖÆ
ɾ³ý dnsmasq DNS »º´æ
ˢРdnsmasq ÌìÉúµÄ DNS »º´æºÜ¼òÆÓ¡£ÓÉÓÚ»º´æ´æ´¢ÔÚÄÚ´æÖУ¬Òò´ËÖØÐÂÆô¶¯Ð§ÀÍ»áɾ³ýËùÓд洢µÄÌõÄ¿¡£
ÒªÖØÐÂÆô¶¯ dnsmasq£¬ÇëÔËÐÐÒÔÏ systemctl ÏÂÁ
sudo systemctl restart dnsmasq
µÇ¼ºó¸´ÖÆ
»òÕߣ¬·¢³öÒÔÏÂÏÂÁ
service dnsmasq restart
µÇ¼ºó¸´ÖÆ
ÈôÊÇ·ºÆðÌáÐÑ£¬ÇëÊäÈëÖÎÀíÔ±ÃÜÂë¡£ÏÖÔÚÒªÖØÐÂÆô¶¯ dnsmasq£¬ËùÓлº´æÖб£´æµÄ DNS ÌõÄ¿½«±»É¨³ý¡£
½¨ÒéˢРDNS »º´æºó£¬¼ì²éÍâµØ»º´æÌõÄ¿ÒÔÈ·ÈÏÊý¾ÝÒѾÀÖ³Éɾ³ý¡£Äú¿ÉÒÔʹÓà Linux ÏÂÁîÖ®Ò»µÄ dig À´¾ÙÐÐÍøÂç¹ÊÕÏɨ³ý£¬²¢Éó²éÊä³öÖеġ°ÅÌÎÊʱ¼ä¡±Öµ¡£ÈôÊÇÁè¼Ý 0 ºÁÃ룬Ôò˵Ã÷»º´æÒѾÀÖ³Éɨ³ý£¨ÈôΪ 0 ºÁÃ룬ÔòÌåÏÖÓò¼Í¼ÈÔ±£´æÓÚ»º´æÖУ©¡£
dig google.com
µÇ¼ºó¸´ÖÆ
ɨ³ý¹È¸èä¯ÀÀÆ÷µÄDNS»º´æ
Äú¾³£Ê¹ÓÃµÄ Web ä¯ÀÀÆ÷Ò²»á»º´æ DNS ¼Í¼¡£ÊäÈë URL ʱ£¬ÏµÍ³»áÔÚÍâµØä¯ÀÀÆ÷»º´æÖÐËÑË÷»º´æÌõÄ¿¡£ÈôÊÇδÕÒµ½£¬Ëü½«¼ì²éÍâµØϵͳ»º´æÖеļͼ¡£É¨³ý Web ä¯ÀÀÆ÷µÄ DNS »º´æºÜÊÇÖ÷Òª£¬ÓÉÓÚËüÓÅÏÈÓÚϵͳ¹æÄ£µÄ»º´æ¡£
ΪÁËÑÝʾ£¬ÈÃÎÒÃÇˢйȸèä¯ÀÀÆ÷ÖеÄDNS»º´æ¡£ÔÚÆäËûä¯ÀÀÆ÷ÉÏÒ²ÓÐһЩҪÁì¿ÉÒÔ×öµ½ÕâÒ»µã£¬ÒÔÊÇ×îºÃ Google Ò»ÏÂÔõÑùʹÓÃÄãʹÓõÄä¯ÀÀÆ÷×öµ½ÕâÒ»µã¡£
Ê×ÏÈ£¬ÔÚURLÀ¸ÖмüÈë¡°chrome://net-internals/#dns¡±²¢°´Enter¼ü£º
µã»÷¡°É¨³ýÖ÷»ú»º´æ¡±°´Å¥À´É¨³ý¹È¸èä¯ÀÀÆ÷Öд洢µÄDNSÌõÄ¿¡£
LinuxÊÇѧϰÍøÂçµÄ×î¼Ñ²Ù×÷ϵͳ
Linux Õ§Ò»¿´¿ÉÄÜ¿´ÆðÀ´ºÜÖش󣬵«ÈôÊÇÄ㻨һЩʱ¼äÀ´Ñ§Ï°ËüÊÇÔõÑùÊÂÇéµÄ£¬ÄãºÜ¿ì¾Í»áÒâʶµ½ËüºÜ°ô£¬ÉõÖÁ¿ÉÄÜ±È Windows »ò macOS ¸üºÃ¡£
´ó´ó¶¼ÔÚÏßЧÀÍÆ÷¶¼ÔËÐÐ Linux£¬ÈôÊÇÄúÏëѧϰÍøÂç»òÏëÖªµÀÅÌËã»úͨ³£ÔõÑùÊÂÇ飬ÕâÒ²ÊÇ Linux ÀíÏëÑ¡ÔñµÄÔµ¹ÊÔÓÉÖ®Ò»¡£
ÒÔÉϾÍÊÇÔõÑùÔÚ Linux ÉÏÉó²éºÍˢРDNS »º´æµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡