Ubuntu18.04 下连接VPN,域名解析异常问题

2,048 阅读3分钟

前言

本文记录Ubuntu下连接VPN,可能遇到的几个问题及其解决方案:

  • 1.进入内网环境后,无法访问外网,提示:无法访问此网址
  • 2.进入内网环境后,部分内网网站无法访问

1.进入内网环境后,无法访问外网

**原因:**进入内网后,外网的服务器被内网墙了,无法正确解析出外网IP导致无法访问 **解决思路:**手动修改本地DNS服务器 **解决方法:**在 /etc/systemd/resolved.conf 中修改DNS服务器 (至于为什么是这个路径,可以了解下Linux中/etc路径下的各个目录和文件的用途)

先cat下看看该文件的内容

执行命令: cat /etc/systemd/resolved.conf

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details

[Resolve]
#DNS=X.X.X.X  (这里被我改过,没记录改之前是什么ip地址了所以用x替代)
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes

这里要改的就是DNS这一行,修改成我们希望使用的DNS服务器,这里可以改为: DNS=8.8.8.8 8.8.4.4 这里指定了两个DNS服务器: 8.8.8.88.8.4.4 8.8.8.8是一个IP地址,是Google提供的免费DNS服务器的IP地址,Google提供的另外一个免费DNS服务器的IP地址是:8.8.4.4 ,用户可以使用Google提供的DNS服务器上网 .

具体操作如下: 执行命令:gedit /etc/systemd/resolved.conf 把原本 #DNS=X.X.X.X 这一句改成 DNS=8.8.8.8 8.8.4.4

你的电脑上 的该文件可能是只读权限,若是如此,需要通过chmod命令(命令详解看这)修改一下该文件的权限:

  • 1.执行 su 进入超级用户模式
  • 2.执行 chmod -R 777 /etc/systemd/resolved.conf修改该文件的权限

修改完之后需要重启systemd-resolved服务使修改生效:执行命令 systemctl restart systemd-resolved.service

这样我们就可以同时访问内网和外网了.

2.进入内网环境后,部分内网网站无法访问

**问题原因:**既然是部分网站无法访问,说明不是VPN的连接问题,而是这部分无法访问的网站的IP地址没被正确解析出来 **解决思路:**在DNS配置文件中将这部分无法访问的内网的域名和IP地址映射起来 **解决方法:**在 /etc/hosts 文件中手动做内网域名及其IP地址的映射

先来看看这个文件的内容: 执行命令:cat /etc/hosts

127.0.0.1	localhost
127.0.1.1	xxx

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

我们要做的就是在这个文件中加入域名与IP地址的映射关系,假设你们公司内网某网站 xxx.xxx.xxx ,其IP地址假设为 111.111.111.111 ,那么就需要在文件 中加入: 111.111.111.111 xxx.xxx.xxx 修改完后保存文件即可生效.