在Ubuntu 14.04上安装OpenVPN

1)首先安装OpenVPN

sudo apt-get update

sudo apt-get install openvpn

或者你直接在官方下载源码编译安装:

tar -zxf openvpn-<version>.tar.gz
cd openvpn-<version>
./configure
make
make install

2)启动openvpn

sudo openvpn --config /etc/openvpn/config.ovpn

其中/etc/openvpn 为你的软件路径,config.ovpn 为你的用户配置文件

Ubuntu 14.04 安装配置好的OpenVPN 后可以ping 通8.8.8.8 但是无法上网,主要是DNS的问题,接下我们需要解决这个问题

3)我们需要在设置:

sudo vi /etc/sysctl.conf
将net.ipv4.ip_forward=0 修改为 net.ipv4.ip_forward=1

修改/etc/openvpn/config.ovpn 文件,添加:

script-security 2
up /etc/openvpn/update-resolv-conf
down /etc/openvpn/update-resolv-conf
down-pre

其中update-resolv-conf 的代码为:

#!/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
#
# Used snippets of resolvconf script by Thomas Hood <该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;
# and Chris Hanson
# Licensed under the GNU GPL. See /usr/share/common-licenses/GPL.
# 07/2013 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。 Fixed intet name
# 05/2006 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。
#
# 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'

## The 'type' builtins will look for file in $PATH variable, so we set the
## PATH below. You might need to directly set the path to 'resolvconf'
## manually if it still doesn't work, i.e.
## RESOLVCONF=/usr/sbin/resolvconf
export PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin
RESOLVCONF=$(type -p resolvconf)

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
R=""
if [ "$IF_DNS_SEARCH" ]; then
R="search "
for DS in $IF_DNS_SEARCH ; do
R="${R} $DS"
done
R="${R}
"
fi

for NS in $IF_DNS_NAMESERVERS ; do
R="${R}nameserver $NS
"
done
#echo -n "$R" | $RESOLVCONF -x -p -a "${dev}"
#echo -n "$R" | $RESOLVCONF -x -a "${dev}.inet"
echo -n "$R" | $RESOLVCONF -a "${dev}.inet"
;;
down)
$RESOLVCONF -d "${dev}.inet"
;;
esac

# Workaround / 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。
# 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

然后你赋予这个文件可执行权限:
sudo chmod +x update-resolv-conf

然后你启动openvpn
sudo openvpn --config /etc/openvpn/config.ovpn

这样就解决了可以连上OpenVPN但是无法上网的问题。

4)不过xinji遇到一个问题,就是每次登录都需要输入账号和密码,对我来说比较麻烦,我希望它能记住我的操作

我想到了一个办法:
在/etc/openvpn/ 建立一个auth.txt

sudo vi auth.txt
ilovexinji
ilovexinji.com

这个文本文件第一行是用户名,第二行是密码.然后在配置文件中/etc/openvpn/config.ovpn 中添加:
auth-user-pass auth.txt

就这样你就可以成功的连接OpenVPN 而不要输入用户名和密码。

如果你是Win7 用户,直接下载客户端安装就可以,然后导入你的配置文件:
https://swupdate.openvpn.org/community/releases/openvpn-install-2.4.4-I601.exe

 

百度网盘分享:openvpn-2.4.4.tar.gz 链接: https://pan.baidu.com/s/1bpKz3jT 密码: 2byn 

openvpn-install-2.4.4-I601.exe 链接: https://pan.baidu.com/s/1qYhztpi 密码: 4v8j

 

提交评论

安全码
刷新

附加信息