openvpn dns推送后NetworkManager不会更改/etc/resolv.conf


22

我遇到了一个问题,即“ /etc/resolv.conf在配置了dns push的openvpn连接后NetworkManager没有更新”。

这是我的openvpn服务器配置:(出于安全原因,我已将域名更改为ABC.COM;)

########################################
# Sample OpenVPN config file for
# 2.0-style multi-client udp server
#
# Adapted from http://openvpn.sourceforge.net/20notes.html
#
# tun-style tunnel

port 1194
dev tun

# Use "local" to set the source address on multi-homed hosts
#local [IP address]

# TLS parms
tls-server 
ca keys/ca.crt
cert keys/static.crt
key keys/static.key
dh keys/dh1024.pem
proto tcp-server

# Tell OpenVPN to be a multi-client udp server
mode server

# The server's virtual endpoints
ifconfig 10.8.0.1 10.8.0.2

# Pool of /30 subnets to be allocated to clients.
# When a client connects, an --ifconfig command
# will be automatically generated and pushed back to
# the client.
ifconfig-pool 10.8.0.4 10.8.0.255

# Push route to client to bind it to our local
# virtual endpoint.
push "route 10.8.0.1 255.255.255.255"

push "dhcp-option DNS 10.8.0.1"

# Push any routes the client needs to get in
# to the local network.
#push "route 192.168.0.0 255.255.255.0"

# Push DHCP options to Windows clients.
push "dhcp-option DOMAIN ABC.COM"
#push "dhcp-option DNS 192.168.0.1"
#push "dhcp-option WINS 192.168.0.1"

# Client should attempt reconnection on link
# failure.
keepalive 10 60

# Delete client instances after some period
# of inactivity.
inactive 600

# Route the --ifconfig pool range into the
# OpenVPN server.
route 10.8.0.0 255.255.255.0

# The server doesn't need privileges
user openvpn
group openvpn

# Keep TUN devices and keys open across restarts.
persist-tun
persist-key

verb 4

如您所见,它基本上是样本配置,几乎没有调整。

现在..

在我的机器(openvpn客户端)上,我可以看到dns正常:

{17:12}/etc/NetworkManager ➭ nslookup git.ABC.COM 10.8.0.1
Server:     10.8.0.1
Address:    10.8.0.1#53

Name:   git.ABC.COM
Address: 10.8.0.1

{17:18}/etc/NetworkManager ➭ nslookup ABC.COM 10.8.0.1   
Server:     10.8.0.1
Address:    10.8.0.1#53

Name:   ABC.COM
Address: 18X.XX.XX.71

服务器端的openvpn登录说(如果我理解正确的话),DNS已被推送:

openvpn[13257]: TCPv4_SERVER link remote: [AF_INET]83.30.135.214:37658
openvpn[13257]: 83.30.135.214:37658 TLS: Initial packet from [AF_INET]83.30.135.214:37658, sid=3251df51 915772f3
openvpn[13257]: 83.30.135.214:37658 VERIFY OK: depth=1, C=XX, ST=XX, L=XXX, O=XXX, OU=XXX, CN=XXX, name=XXX, emailAddress=mail@ABC.COM
openvpn[13257]: 83.30.135.214:37658 VERIFY OK: depth=0, C=XX, ST=XX, L=XXX, O=XXX, OU=XXX, CN=XXX, name=XXX, emailAddress=mail@ABC.COM
openvpn[13257]: 83.30.135.214:37658 Data Channel Encrypt: Cipher 'BF-CBC' initialized with 128 bit key
openvpn[13257]: 83.30.135.214:37658 Data Channel Encrypt: Using 160 bit message hash 'SHA1' for HMAC authentication
openvpn[13257]: 83.30.135.214:37658 Data Channel Decrypt: Cipher 'BF-CBC' initialized with 128 bit key
openvpn[13257]: 83.30.135.214:37658 Data Channel Decrypt: Using 160 bit message hash 'SHA1' for HMAC authentication
openvpn[13257]: 83.30.135.214:37658 Control Channel: TLSv1, cipher TLSv1/SSLv3 DHE-RSA-AES256-SHA, 1024 bit RSA
openvpn[13257]: 83.30.135.214:37658 [jacek] Peer Connection Initiated with [AF_INET]83.30.135.214:37658
openvpn[13257]: jacek/83.30.135.214:37658 MULTI_sva: pool returned IPv4=10.8.0.10, IPv6=(Not enabled)
openvpn[13257]: jacek/83.30.135.214:37658 MULTI: Learn: 10.8.0.10 -> jacek/83.30.135.214:37658
openvpn[13257]: jacek/83.30.135.214:37658 MULTI: primary virtual IP for jacek/83.30.135.214:37658: 10.8.0.10
openvpn[13257]: jacek/83.30.135.214:37658 PUSH: Received control message: 'PUSH_REQUEST'
openvpn[13257]: jacek/83.30.135.214:37658 send_push_reply(): safe_cap=940
openvpn[13257]: jacek/83.30.135.214:37658 SENT CONTROL [jacek]: 'PUSH_REPLY,route 10.8.0.1 255.255.255.255,dhcp-option DNS 10.8.0.1,dhcp-option DOMAIN ABC.COM,ping 10,ping-restart 60,ifconfig 10.8.0.10 10.8.0.9' (status=1)

openvp站在我这一边:

Aug 05 17:13:55 localhost.localdomain openvpn[1198]: TCPv4_CLIENT link remote: [AF_INET]XXX.XX.37.71:1194
Aug 05 17:13:55 localhost.localdomain openvpn[1198]: TLS: Initial packet from [AF_INET]XXX.XX.37.71:1194, sid=89cc981c d57dd826
Aug 05 17:13:56 localhost.localdomain openvpn[1198]: VERIFY OK: depth=1, C=XX, ST=XX, L=XXX, O=XXX, OU=XXX, CN=XXX, name=XXX, emailAddress=mail@ABC.COM
Aug 05 17:13:56 localhost.localdomain openvpn[1198]: VERIFY OK: depth=0, C=XX, ST=XX, L=XXX, O=XXX, OU=XXX, CN=XXX, name=XXX, emailAddress=mail@ABC.COM
Aug 05 17:13:58 localhost.localdomain openvpn[1198]: Data Channel Encrypt: Cipher 'BF-CBC' initialized with 128 bit key
Aug 05 17:13:58 localhost.localdomain openvpn[1198]: Data Channel Encrypt: Using 160 bit message hash 'SHA1' for HMAC authentication
Aug 05 17:13:58 localhost.localdomain openvpn[1198]: Data Channel Decrypt: Cipher 'BF-CBC' initialized with 128 bit key
Aug 05 17:13:58 localhost.localdomain openvpn[1198]: Data Channel Decrypt: Using 160 bit message hash 'SHA1' for HMAC authentication
Aug 05 17:13:58 localhost.localdomain openvpn[1198]: Control Channel: TLSv1, cipher TLSv1/SSLv3 DHE-RSA-AES256-SHA, 1024 bit RSA
Aug 05 17:13:58 localhost.localdomain openvpn[1198]: [static] Peer Connection Initiated with [AF_INET]XXX.XX.37.71:1194
Aug 05 17:14:00 localhost.localdomain openvpn[1198]: SENT CONTROL [static]: 'PUSH_REQUEST' (status=1)
Aug 05 17:14:01 localhost.localdomain openvpn[1198]: PUSH: Received control message: 'PUSH_REPLY,route 10.8.0.1 255.255.255.255,dhcp-option DNS 10.8.0.1,dhcp-option DOMAIN ABC.COM,ping 10,ping-restart 60,ifconfig 10.8.0.10 10.8.0.9'
Aug 05 17:14:01 localhost.localdomain openvpn[1198]: OPTIONS IMPORT: timers and/or timeouts modified
Aug 05 17:14:01 localhost.localdomain openvpn[1198]: OPTIONS IMPORT: --ifconfig/up options modified
Aug 05 17:14:01 localhost.localdomain openvpn[1198]: OPTIONS IMPORT: route options modified
Aug 05 17:14:01 localhost.localdomain openvpn[1198]: OPTIONS IMPORT: --ip-win32 and/or --dhcp-option options modified
Aug 05 17:14:01 localhost.localdomain openvpn[1198]: ROUTE_GATEWAY 10.123.123.1/255.255.255.0 IFACE=wlan0 HWADDR=44:6d:57:32:81:2e
Aug 05 17:14:01 localhost.localdomain openvpn[1198]: TUN/TAP device tun0 opened
Aug 05 17:14:01 localhost.localdomain openvpn[1198]: TUN/TAP TX queue length set to 100
Aug 05 17:14:01 localhost.localdomain openvpn[1198]: do_ifconfig, tt->ipv6=0, tt->did_ifconfig_ipv6_setup=0
Aug 05 17:14:01 localhost.localdomain openvpn[1198]: /usr/sbin/ip link set dev tun0 up mtu 1500
Aug 05 17:14:01 localhost.localdomain openvpn[1198]: /usr/sbin/ip addr add dev tun0 local 10.8.0.10 peer 10.8.0.9
Aug 05 17:14:01 localhost.localdomain openvpn[1198]: /usr/sbin/ip route add 10.8.0.1/32 via 10.8.0.9
Aug 05 17:14:01 localhost.localdomain openvpn[1198]: Initialization Sequence Completed

看起来一切都很好。

但。我/var/log/messages也检查了...,发现那条线:

Aug  5 17:14:01 localhost NetworkManager[761]: <warn> /sys/devices/virtual/net/tun0: couldn't determine device driver; ignoring...

ip a 返回:

5: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 100
    link/none 
    inet 10.8.0.10 peer 10.8.0.9/32 scope global tun0
       valid_lft forever preferred_lft forever

route -n 返回:

# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.123.123.1    0.0.0.0         UG    0      0        0 wlan0
10.8.0.1        10.8.0.9        255.255.255.255 UGH   0      0        0 tun0
10.8.0.9        0.0.0.0         255.255.255.255 UH    0      0        0 tun0
10.123.123.0    0.0.0.0         255.255.255.0   U     0      0        0 wlan0

因此,基本上所有的东西都可以正常工作,除了DNS被推送...哦!是的,我的/etc/resolv.conf

# Generated by NetworkManager
domain home
search home
nameserver 10.123.123.1

问题在哪里?

(我收到Windows用户使用openvpn客户端的回复,在他这一边,DNS可以正常工作,所以这是我这方面的问题。

好的,现在我有另一个响应(在服务器端重新启动openvpn服务之后)-它不起作用。

我必须说它昨天也可以在我的机器上工作..所以我在服务器上搞砸了吗?会是什么呢?)

编辑: 好的,我还有另一个Windows用户响应(与以前相同的用户)-现在正在工作。所以..我想这是由openvpn restart和它的一些延迟引起的。从那以后我什么都没做。因此,我们回到了我的机器上。

我还追踪到,这个奇怪的tun0消息也在昨天出现了,并且在昨天也起作用了。还是我resolv.conf自己添加了条目?我不记得了..(该死)


我已经看到在启用了selinux且其resolv.conf文件具有错误的selinux安全上下文的系统上发生了这种情况。运行restorecon以在该文件上还原安全上下文解决了该问题。PS:这是resolv.conf,而不是
resolve.conf

要特别注意/etc/NetworkManager/NetworkManager.conf:保持评论dns=dnsmasq和拥有managed=true。另外,您可能会受到Bug#1294899的影响,尽管报告了“已建立”的VPN连接,但导入保存的VPN连接最近已中断。检查您的VPN设置:在字段中输入协议名称(:tcp:udpGateway。检查高级设置,尤其是Port numberLZO compression。还要检查日志。完成DNS泄漏测试
KrisWebDev

Answers:


23

这对我有用:http : //www.softwarepassion.com/solving-dns-problems-with-openvpn-on-ubuntu-box/

重要的步骤是将以下两行配置添加到客户端 openvpn配置文件中:

up /etc/openvpn/update-resolv-conf
down /etc/openvpn/update-resolv-conf

还要确保该resolvconf软件包已安装在客户端上,因为该update-resolv-conf脚本取决于该脚本。

它与openvpn客户端服务或命令一起使用以手动启动它。

但是,Ubuntu Network Manager并非如此。到目前为止这是一个问题:https : //bugs.launchpad.net/ubuntu/+source/openvpn/+bug/1211110


4
不要忘了使用--script-security 2运行openvpn
kol

2
或也放入script-security 2您的客户端openvpn配置文件。
KrisWebDev '16

我不建议您不通过网络管理器直接使用OpenVPN,否则您可能会遇到Bug#691723 OpenVPN Client忽略没有解决方案的DNS。在我的情况下,up启动脚本后,网络管理器将覆盖resolvconf 。# echo "nameserver 208.67.220.220" | /sbin/resolvconf -a "tun0.openvpn"运行openvpn 的脏程序之后可以完成工作……直到再次覆盖它。同样,不要直接使用OpenVPN。
KrisWebDev

up找不到相关命令 !!
Pardeep Jain

@KrisWebDev:是的,但是通过NetworkManager使用OpenVPN允许用户禁用(打开Off)管理员可能不希望的连接。
palswim

12

在禁用NetworkManager自己的dnsmasq之后,对我有用。

编辑 /etc/NetworkManager/NetworkManager.conf

 #dns=dnsmasq

并重新启动NetworkManager

sudo restart network-manager

我已经在客户端配置中从李小龙那里得到了改变。进行此更改还解决了该问题[Ubuntu 15.10]。
TheDauthi 2015年

1
这是什么魔法?dnsmasq在做什么?
GuySoft'3

这在不同版本的Ubuntu中为我工作。我不太了解dnsmasq的功能,但是注释掉NetworkManager.conf中的那行神奇地解决了VPN连接和Wi-Fi连接的问题。
西蒙

尽管我必须重新启动机器,因为sudo restart network-manager失败并出现错误,这对运行Linux Mint 18的我仍然有效。接受的答案对我不起作用。
trebormf

在restarte命令抛出错误时restart: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused
Pardeep Jain

2

终于可以使用了(使用标准的NetworkManager和OVPN插件)

nmcli -p connection modify MY_VPN_CONNECTION ipv4.never-default no
nmcli -p connection modify MY_VPN_CONNECTION ipv4.ignore-auto-dns no
nmcli -p connection modify MY_VPN_CONNECTION ipv4.dns-priority -42

在这种情况下,一旦建立了VPN连接,所有DNS请求都将直接定向到VPN提供的DNS服务器,而无需使用dnsmasq,上/下/分发帮助程序脚本进行任何操作。


这行得通,10x
Roman M

1

可以在OpenVPN中推送DNS设置。就像您在配置中一样,这是在服务器配置中使用以下行完成的:

push "dhcp-option DNS 10.20.30.40"

使用Windows GUI对我来说这几乎是不可能的,但对于Linux系统,则需要进行一些调整。为了连接到我的家庭网络(目前使用Fedora 18),我使用了gronke在GitHub(https://github.com/gronke/OpenVPN-linux-push)上的脚本来自动化更新过程。

要使用这些脚本,我在OpenVPN客户端文件中添加了以下内容:

up /home/gadgeteering/tools/vpn/up.sh
down /home/gadgeteering/tools/vpn/down.sh

up.sh:

#! /bin/bash
DEV=$1

if [ ! -d /tmp/openvpn ]; then
mkdir /tmp/openvpn
fi
CACHE_NAMESERVER="/tmp/openvpn/$DEV.nameserver"
echo -n "" > $CACHE_NAMESERVER

dns=dns
for opt in ${!foreign_option_*}
do
eval "dns=\${$opt#dhcp-option DNS }"
if [[ $dns =~ [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} ]]; then
if [ ! -f /etc/resolv.conf.default ]; then
cp /etc/resolv.conf /etc/resolv.conf.default
fi

cat /etc/resolv.conf | grep -v ^# | grep -v ^nameserver > /tmp/resolv.conf
echo "nameserver $dns" >> /tmp/resolv.conf
echo $dns >> $CACHE_NAMESERVER
cat /etc/resolv.conf | grep -v ^# | grep -v "nameserver $dns" | grep nameserver >> /tmp/resolv.conf
mv /tmp/resolv.conf /etc/resolv.conf

fi
done

down.sh:

#! /bin/bash
DEV=$1
CACHE_NAMESERVER="/tmp/openvpn/$DEV.nameserver"
echo $CACHE_NAMESERVER

if [ -f $CACHE_NAMESERVER ]; then
for ns in `cat $CACHE_NAMESERVER`; do
echo "Removing $ns from /etc/resolv.conf"
cat /etc/resolv.conf | grep -v "nameserver $ns" > /tmp/resolv.conf
mv /tmp/resolv.conf /etc/resolv.conf

done
fi

为什么需要dns=dns

对于格隆克,这将是一个问题,我认为这也有些奇怪。自发表评论以来,我一直在继续使用此脚本的改编版本,该脚本完全不使用'dns'变量。由于遗漏,我没有观察到任何行为上的变化。
Gadgeteering,

1

可以通过手动替换使NetworkManager运行/etc/resolv.conf。请注意,这确实是一个hack,不能视其为每种情况的有效解决方案。

#!/bin/bash
case "$2" in
    vpn-up)
    tmp=$(mktemp)
    func=$(mktemp)
    echo 'ping -c 1 -w 1 -q $1 > /dev/null ;
          if [ 0 -eq $? ]; then echo $1; fi' > $func
    grep -v "^#" /etc/resolv.conf > $tmp
    grep -rl type=vpn /etc/NetworkManager/system-connections \
        | xargs -n 1 sed -rne 's|dns=||p' \
        | sed -re 's|;|\n|g' \
        | grep -v "^\s*$" \
        | xargs -n 1 bash $func \
        | sed -re "s|(.*)|nameserver \1|" \
        | cat - $tmp \
        > /etc/resolv.conf
    rm -f $tmp $func;;
    vpn-down) resolvconf -u;;
esac

这个脚本应该被放在/etc/NetworkManager/dispatcher.d; 应该是可执行文件并由root拥有。它读取它可以找到的所有NetworkManager vpn配置,并/etc/resolv.conf使用在那里找到的可访问名称服务器进行重写。它不写入domainsearch线路; 但它可以忘记令人讨厌的NetworkManager错误。

我使用Ubuntu 16.04,它可以工作。


0

OpenVPN当前无法推送DNS设置。您将必须手动更改/etc/resolv.conf以匹配您(受保护的)DNS服务器。我只是在与Access Server相同的计算机上运行BIND9服务,然后通过隧道指向该服务。使用该机器的本地IP地址,例如192.168.1.110

祝好运!

碧玉


有了李小龙的回答,
etc /

0

我有一个不使用resolvconf或的OpenSUSE客户端systemd-networkd,但是我能够修改该通用update-resolv-conf脚本以与NetworkManager的nmcli命令一起使用:

#!/usr/bin/env bash
#
# Parses DHCP options from openvpn to update resolv.conf
# To use set as 'up' and 'down' script in your openvpn *.conf:
# up /etc/openvpn/update-resolv-conf
# down /etc/openvpn/update-resolv-conf
#
# Example envs set from openvpn:
# foreign_option_1='dhcp-option DNS 193.43.27.132'
# foreign_option_2='dhcp-option DNS 193.43.27.133'
# foreign_option_3='dhcp-option DOMAIN be.bnc.ch'
# foreign_option_4='dhcp-option DOMAIN-SEARCH bnc.local'

case $script_type in

up)
    for optionname in ${!foreign_option_*} ; do
        option="${!optionname}"
        echo $option
        part1=$(echo "$option" | cut -d " " -f 1)
        if [ "$part1" == "dhcp-option" ] ; then
            part2=$(echo "$option" | cut -d " " -f 2)
            part3=$(echo "$option" | cut -d " " -f 3)
            if [ "$part2" == "DNS" ] ; then
                IF_DNS_NAMESERVERS="$IF_DNS_NAMESERVERS $part3"
            fi
            if [[ "$part2" == "DOMAIN" || "$part2" == "DOMAIN-SEARCH" ]] ; then
                IF_DNS_SEARCH="$IF_DNS_SEARCH $part3"
            fi
        fi
    done
    if [ -n "$IF_DNS_SEARCH" ]; then
        nmcli connection modify "${dev}" ipv4.dns-search "$IF_DNS_SEARCH"
    fi
    if [ -n "$IF_DNS_NAMESERVERS" ]; then
        nmcli connection modify "${dev}" ipv4.dns "$IF_DNS_NAMESERVERS"
    fi
    nmcli connection up "${dev}" # Force NM to reevaluate the properties
    ;;
esac

# Workaround / jm@epiclabs.io 
# force exit with no errors. Due to an apparent conflict with the Network Manager
# $RESOLVCONF sometimes exits with error code 6 even though it has performed the
# action correctly and OpenVPN shuts down.
exit 0

它没有down处理程序,因为NetworkManager会在连接终止时自动删除nameserverand search(DNS搜索)参数。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.