尝试使用 dnsmasq 和网络管理器将 .local 解析为 127.0.0.1

尝试使用 dnsmasq 和网络管理器将 .local 解析为 127.0.0.1

我在 ubuntu 20.04 上,正在尝试在本地开发 php 项目。我想要的是能够批量虚拟托管本地项目,并且当我创建项目并将 添加到.local项目名称时,始终解析为 127.0.0.1。到目前为止,我尝试过:

  1. 禁用并停止systemd-resolved

    sudo systemctl stop systemd-resolved
    sudo systemctl disable systemd-resolved
    
  2. 删除符号链接resolv.conf

    sudo rm /etc/resolv.conf
    
  3. 在中创建一个新resolv.conf文件/etc/并添加nameserver 8.8.8.8

  4. 已安装dnsmasq

  5. /etc/NetworkManager/NetworkManager.conf有以下内容:

    [main]
    dns=dnsmasq
    plugins=ifupdown,keyfile
    
    [ifupdown]
    managed=false
    
    [device]
    wifi.scan-rand-mac-address=no
    
  6. /etc/NetworkManager/dnsmasq.d我创建了一个文件,其中local.conf包含:

    address=/local/127.0.0.1
    
  7. 重新加载网络管理器,但每次我这样做时,它都会将我的重置resolv.conf127.0.1.1,所以我必须将名称服务器重置为8.8.8.8

输出sudo netstat -anlp | grep 53

tcp        0      0 0.0.0.0:53              0.0.0.0:*               LISTEN      39737/dnsmasq       
tcp6       0      0 :::53                   :::*                    LISTEN      39737/dnsmasq       
tcp6       0      0 2600:8800:86ac:2::53030 2606:4700::6810:5f4:443 ESTABLISHED 4802/chrome --type= 
tcp6       0      0 2600:8800:86ac:2::53936 2a04:fa87:fffe::c00:443 ESTABLISHED 4802/chrome --type= 
tcp6       0      0 2600:8800:86ac:2::57530 2600:1f1c:a99:832c::443 ESTABLISHED 4802/chrome --type= 
udp        0      0 0.0.0.0:53              0.0.0.0:*                           39737/dnsmasq       
udp        0      0 224.0.0.251:5353        0.0.0.0:*                           4759/chrome         
udp        0      0 224.0.0.251:5353        0.0.0.0:*                           4802/chrome --type= 
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           1109/avahi-daemon:  
udp6       0      0 :::53                   :::*                                39737/dnsmasq       
udp6       0      0 2600:8800:86ac:2::35397 2607:f8b0:4007:803::443 ESTABLISHED 4802/chrome --type= 
udp6       0      0 2600:8800:86ac:2::53965 2607:f8b0:400e:c09::443 ESTABLISHED 4802/chrome --type= 
udp6       0      0 :::5353                 :::*                                1109/avahi-daemon:  
udp6       0      0 2600:8800:86ac:2::38534 2607:f8b0:4007:80f::443 ESTABLISHED 4802/chrome --type= 
unix  2      [ ACC ]     STREAM     LISTENING     75311    3037/Xorg            @/tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     75312    3037/Xorg            /tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     40653    1386/mysqld          /var/run/mysqld/mysqlx.sock
unix  3      [ ]         STREAM     CONNECTED     970535   44951/chrome --type  
unix  3      [ ]         STREAM     CONNECTED     21922    531/systemd-udevd    
unix  3      [ ]         STREAM     CONNECTED     805753   2900/pulseaudio      /run/user/1000/pulse/native
unix  3      [ ]         STREAM     CONNECTED     164653   7749/gdm-session-wo  
unix  3      [ ]         STREAM     CONNECTED     970534   4759/chrome          
unix  3      [ ]         STREAM     CONNECTED     84953    3037/Xorg            @/tmp/.X11-unix/X0
unix  3      [ ]         STREAM     CONNECTED     129536   4952/chrome --type=  
unix  3      [ ]         STREAM     CONNECTED     129538   4759/chrome          
unix  3      [ ]         STREAM     CONNECTED     129535   4759/chrome          
unix  3      [ ]         STREAM     CONNECTED     81853    3294/ibus-daemon     
unix  3      [ ]         STREAM     CONNECTED     109534   4073/gnome-terminal  
unix  2      [ ]         DGRAM                    21931    531/systemd-udevd    
unix  3      [ ]         STREAM     CONNECTED     88353    3434/dconf-service   
unix  3      [ ]         STREAM     CONNECTED     129539   4957/chrome --type=  
unix  3      [ ]         STREAM     CONNECTED     129153   1/init               /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     83553    3393/ibus-engine-si  
unix  3      [ ]         DGRAM                    21933    531/systemd-udevd    
unix  3      [ ]         STREAM     CONNECTED     97533    3622/evolution-sour  
unix  3      [ ]         STREAM     CONNECTED     89353    3600/gnome-shell-ca  
unix  3      [ ]         DGRAM                    21934    531/systemd-udevd    
unix  3      [ ]         SEQPACKET  CONNECTED     130853   4759/chrome          
unix  3      [ ]         STREAM     CONNECTED     83353    1/init               /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     132553   4611/update-notifie  
unix  3      [ ]         STREAM     CONNECTED     39532    1903/rtkit-daemon    
unix  3      [ ]         STREAM     CONNECTED     100532   2906/dbus-daemon     /run/user/1000/bus
unix  3      [ ]         STREAM     CONNECTED     868534   2900/pulseaudio      /run/user/1000/pulse/native
unix  3      [ ]         STREAM     CONNECTED     35395    1456/kerneloops      
unix  3      [ ]         STREAM     CONNECTED     28453    1114/dbus-daemon     /run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     100653   3660/gsd-media-keys  
unix  3      [ ]         STREAM     CONNECTED     35396    1458/kerneloops      
unix  3      [ ]         STREAM     CONNECTED     93535    3622/evolution-sour  
unix  3      [ ]         STREAM     CONNECTED     708953   4759/chrome          
unix  3      [ ]         STREAM     CONNECTED     100530   2906/dbus-daemon     /run/user/1000/bus
unix  3      [ ]         STREAM     CONNECTED     35394    1421/whoopsie        
unix  3      [ ]         STREAM     CONNECTED     968853   1989/master          

按照 Brian Turek 的说法,我运行了 dnsmasq,然后重新加载了 NetoworkManager.service,然后运行ps aux | grep dnsmasq,这是输出:

nobody    103697  0.0  0.0  14776  4264 ?        S    06:12   0:00 /usr/sbin/dnsmasq --no-resolv --keep-in-foreground --no-hosts --bind-interfaces --pid-file=/run/NetworkManager/dnsmasq.pid --listen-address=127.0.1.1 --cache-size=0 --clear-on-reload --conf-file=/dev/null --proxy-dnssec --enable-dbus=org.freedesktop.NetworkManager.dnsmasq --conf-dir=/etc/NetworkManager/dnsmasq.d
paul    103707  0.0  0.0   9028   676 pts/0    S+   06:12   0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox dnsmasq

答案1

我认为 NetworkManager 的行为符合预期。NetworkManager 可能正在启动 DNSmasq 实例,并将您的计算机设置为使用该实例来解析 DNS 地址(从而更改 /etc/resolv.conf)。

我认为您想将 添加server=8.8.8.8到您的/etc/NetworkManager/dnsmasq.d/local.conf以及no-resolv。这应该会导致 DNSmasq 使用 Google 作为您的上游 DNS,并让您的机器使用 DNSmasq 作为本地解析器。

答案2

这个问题的答案归功于 Brian Turek。结果我不得不禁用 dnsmasq sudo systemctl stop dnsmaq.service,然后重新启动 systemd resolve,因为 NetworkManager 正在运行它自己的 dnsmasq 实例。然后,为了获取文件夹中的所有本地项目/home/projects/www/,我不得不禁用sites-enabled在 中找到的所有当前站点/etc/apache2/sites-enabled000.default.conf阻止了任何其他站点的正常加载。

相关内容