[ad_1]
虚拟专用网络 (虚拟专用网) 是一种创建安全网络连接的网络技术 通过公共网络 比如互联网。
开放式VPN 是开源的 虚拟专用网 James Yonan 的守护进程。 开放式VPN 是一个强大且高度灵活的 VPN 守护进程. 开放式VPN 支持 SSL/TLS 安全性, 以太网桥接, TCP 或者 UDP 隧道 通过代理运输或 网络地址转换,支持动态 IP 地址和 DHCP,可扩展到成百上千的用户,以及可移植到大多数主要操作系统平台。
在本教程中解释步骤 设置和配置 OpenVPN 服务器 和 客户 在 Ubuntu 13.10 用于远程访问。
在 Ubuntu 13.10 上安装 OpenVpn 服务器
要在终端中安装 openvpn,请输入:
$ sudo apt-get install openvpn easy-rsa -y
从 openvpn 服务器复制必要的密钥
要设置您自己的证书颁发机构 (CA) 并为 OpenVPN 服务器和多个客户端生成证书和密钥,请首先将 easy-rsa 目录复制到 /etc/openvpn。
$ mkdir /etc/openvpn/easy-rsa $ cp -rf /usr/share/easy-rsa/* /etc/openvpn/easy-rsa/
编辑 /etc/openvpn/easy-rsa/vars:
$ vi /etc/openvpn/easy-rsa/vars
并且,更改与您的国家、州、城市、邮件 ID 等匹配的值。
... export KEY_COUNTRY="BA" export KEY_PROVINCE="RS" export KEY_CITY="Banjaluka" export KEY_ORG="Lin Tut" export KEY_EMAIL="[email protected]" export KEY_CN=MyVPN export KEY_NAME=MyVPN export KEY_OU=MyVPN ...
Enter 以下用于生成主证书颁发机构 (CA) 证书和密钥:
$ cd /etc/openvpn/easy-rsa/ $ cp openssl-1.0.0.cnf openssl.cnf $ source vars $ ./clean-all
运行以下命令生成 CA 证书和 CA 密钥:
$ ./build-ca
示例输出:
$ ./build-ca Generating a 1024 bit RSA private key ...............++++++ ...++++++ writing new private key to 'ca.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [BA]:----> Press Enter State or Province Name (full name) [RS]:----> Press Enter Locality Name (eg, city) [Banjaluka]:----> Press Enter Organization Name (eg, company) [Lint Tut]:----> Press Enter Organizational Unit Name (eg, section) [MyVPN]:----> Press Enter Common Name (eg, your name or your server's hostname) [MyVPN]:----> Press Enter Name [MyVPN]:----> Press Enter Email Address [[email protected]]:----> Press Enter
服务器证书
接下来,我们将为服务器生成证书和私钥:
$ ./build-key-server server
示例输出:
./build-key-server server Generating a 1024 bit RSA private key ................++++++ ..........++++++ writing new private key to 'server.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [BA]:----> Press Enter State or Province Name (full name) [RS]:----> Press Enter Locality Name (eg, city) [Banjaluka]:----> Press Enter Organization Name (eg, company) [Lint Tut]:----> Press Enter Organizational Unit Name (eg, section) [MyVPN]:----> Press Enter Common Name (eg, your name or your server's hostname) [server]:----> Press Enter Name [MyVPN]:----> Press Enter Email Address [[email protected]]:----> Press Enter . Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:----> Press Enter An optional company name []:----> Press Enter Using configuration from /etc/openvpn/easy-rsa/openssl-1.0.0.cnf Check that the request matches the signature Signature ok The Subject's Distinguished Name is as follows countryName :PRINTABLE:'BA' stateOrProvinceName :PRINTABLE:'RS' localityName :PRINTABLE:'Banjaluka' organizationName :PRINTABLE:'Lint Tut' organizationalUnitName:PRINTABLE:'MyVPN' commonName :PRINTABLE:'server' name :PRINTABLE:'MyVPN' emailAddress :IA5STRING:'[email protected]' Certificate is to be certified until Mar 31 21:56:13 2024 GMT (3650 days) Sign the certificate? [y/n]:y ----> Type Y and Press Enter . 1 out of 1 certificate requests certified, commit? [y/n]y ----> Type Y and Press Enter Write out database with 1 new entries Data Base Updated
客户证书
这 VPN客户端 还需要一个 认证自己的证书 到 服务器. 通常,您为每个客户端创建不同的证书。 要创建证书,请以 root 用户身份在终端中输入以下内容:
$ ./build-key client
生成 Diffie Hellman 参数
Enter 以下命令生成DH参数。
$ ./build-dh
进入/etc/openvpn/easy-rsa/keys/目录,输入以下命令将上述文件传送到/etc/openvpn/目录。
$ cd /etc/openvpn/easy-rsa/keys/ $ cp dh1024.pem ca.crt server.crt server.key /etc/openvpn/
然后,您必须将所有客户端证书和密钥复制到远程 VPN 客户端,以便对 VPN 服务器进行身份验证。 在我们的例子中,我们只为一个客户端生成了证书和密钥,因此我们必须将以下文件复制到 VPN 客户端。
ca.crt 客户端.crt 客户端.key
您必须安全地将上述文件复制到您的 VPN 客户端。 小心复制密钥。 如果有人有机会获得密钥,他们就可以轻松入侵并完全访问您的虚拟专用网络。
配置 VPN 服务器
复制文件 服务器配置文件 归档到 /etc/openvpn/ 目录。
$ cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/
使用以下命令提取文件:
$ gzip -d /etc/openvpn/server.conf.gz
编辑文件 服务器配置文件,
$ vi /etc/openvpn/server.conf
查找并取消注释以下行以通过 OpenVPN 服务器路由客户端系统流量。
[...] push "redirect-gateway def1 bypass-dhcp" [...]
此外,取消注释并更改 DNS 服务器以反映您自己的 DNS 值。 这里我使用的是 Google 公共 DNS 服务器。
[...] push "dhcp-option DNS 208.67.222.222" push "dhcp-option DNS 208.67.220.220" [...]
取消注释以下几行:
[...] user nobody group nogroup [...]
Save 和 close 文件。
然后我们需要复制和编辑 client.conf 文件。 我们需要将此文件传输到 VPN 客户端。 首先将文件复制到任何位置(例如 /家 目录)。
$ cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf /home/
编辑文件 客户端配置文件,
$ vi /home/client.conf
设置 VPN 服务器主机名/IP 地址:
# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote 192.168.1.5 1194 # Enter IP on your OpenVPN server
IP转发和路由配置
编辑 配置文件 文件,
$ vi /etc/sysctl.conf
找到以下行并设置值“1”以启用 IP 转发。
# Controls IP packet forwarding net.ipv4.ip_forward = 1
运行以下命令以应用 系统控制 变化。
$ sysctl -p
Enter 使用以下命令启用 IP 转发:
$ echo 1 > /proc/sys/net/ipv4/ip_forward
调整 iptables 以正确通过 VPN 转发流量。
Enter 以下命令一一通过OpenVPN转发流量:
$ iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT $ iptables -A FORWARD -s 10.8.0.0/24 -j ACCEPT $ iptables -A FORWARD -j REJECT $ iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
安装 iptables-persistent 并保存 iptables 规则:
$ apt-get install iptables-persistent
接受默认值并保存 iptables 规则。
如果您使用 UFW,则应运行以下命令以允许 openvpn 端口 1194:
$ ufw allow 1194/udp $ ufw allow 1194/tcp
最后,使用以下命令启动 openvpn 服务并使其在每次重新启动时自动启动:
$ service openvpn start
使用 ifconfig 命令验证是否创建了 VPN 接口(tun0):
$ ifconfig
示例输出:
配置 OpenVPN 客户端
最后让我们创建一个 server.ovpn 配置文件。 为方便起见,您只需使用记事本(或任何其他简单的文本编辑器工具)在本地计算机上创建它。 Enter 在该文件中遵循以下内容:
client dev tun proto udp remote 192.168.1.5 1194 # - Your server IP and OpenVPN Port resolv-retry infinite nobind tun-mtu 1500 tun-mtu-extra 32 mssfix 1450 persist-key persist-tun ca ca.crt auth-user-pass comp-lzo reneg-sec 0 verb 3
然后使用 .ovpn 扩展名保存它。 Save 您安装位置的 config 目录中的该文件 OpenVPN客户端 在你的电脑里。
[ad_2]