1.1 Openvpn概念
OpenVPN是一个用于创建虚拟专用网络加密通道的软件包,最早由James Yonan编写。OpenVPN可以使用公开密钥、电子证书、或者用户名/密码来进行身份验证。它大量使用了OpenSSL加密库中的SSLv3/TLSv1协议函数库。
OpenVPN可以在Solaris、Linux、OpenBSD、FreeBSD、NetBSD、Mac OS X以及Windows、Android和ios上运行。
OpenVPN不是一个基于Web的VPN软件,也不与IPsec及其它VPN软件包兼容。
1.1.1 Openvpn原理
OpenVPN的技术原理主要包括:虚拟网卡、加密(SSL协议的实现)、身份验证、功能与端口
1.1.2 虚拟网卡
虚拟网卡是使用网络底层编程技术实现的一个驱动软件,安装此类程序后主机上会增加一个非真实的网卡,它可以像其它网卡一样进行配置。
服务程序可以在应用层打开虚拟网卡,如果应用软件 (如网络浏览器)向虚拟网卡发送数据,则服务程序可以读取到该数据,如果服务程序写合适的数据到虚拟网卡,应用软件也可以接收 得到。
很多系统都可以安装虚拟网卡,所以OpenVPN的跨平台使用变得容易。在OpenVPN中,如果用户访问一个远程的虚拟地址(属于虚拟网卡配置的地址系列,区别于真实地址),则操作系统会通过路由机制
将数据包(TUN模式)或数据帧(TAP模式)发送到虚拟网卡上,服务程序接收该数据并进行相应的处理后,会通过socket从外网上发送出去。这完成了一个单向传输的过程,反过来也是一样。
当远程服务程序通过SOCKET从外网上接收到数据,并进行相应的处理后,又会发送回给虚拟网卡,则相应应用软件就会接收到。
1.1.3 加密
OpenVPN使用OpenSSL库来加密数据与控制信息,所以它可以使用任何OpenSSL支持的算法,它可以使用HMAC功能来进一步提高连接的安全性,OpenSSL的硬件加速也能提高它的性能。
1.1.4 身份验证
OpenVPN支持的身份验证方式:
1.1.5 预享私钥
最为简单,但它也只能用于创建点对点的VPN
1.1.6 第三方证书(PKI)
提供最完善的功能,但需要额外维护一个PKI证书系统。
1.1.7 用户名和密码组合
可以省略掉客户端证书但同样需要服务端证书用作加密。(OpenVPN 2.0+)
1.1.8 功能与端口
OpenVPN所有的通信都基于一个单一的IP端口,使用通用的网络协议,默认且推荐使用UDP协议,它也支持TCP。IANA指定给OpenVPN的官方端口为1194。
OpenVPN 2.0以后的版本每个进程可以同时管理数个并发的隧道。
OpenVPN可以通过大多数的代理服务器,并且能够在NAT环境中很好地工作。
服务端具有向客户端推送某些网络配置信息的功能,这些信息包括:IP地址、路由设置。
OpenVPN提供了两种虚拟网络接口:通用Tun/Tap驱动,通过它们,可以创建三层IP隧道,或者虚拟二层以太网,后者可以传送任何类型的二层以太网数据。
传送的数据可以通过LZO算法压缩。
1.1.9 OpenVPN的实现
OpenVPN的实现分为服务端和客户端。
搭建过程中注意软件版本,特别是Windows客户端这里使用的是最新的OpenVPN版本2.4.3,老的版本的客户端可能出现无法连接OpenVPN服务端的情况。
1.2 系统环境的优化
1.2.1 查看当前的系统版本,及内核信息
[root@openvpn ~]# uname -r
2.6.32-696.el6.x86_64
[root@openvpn ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
1.2.2 系统优化脚本
1.2.2.1 启动优化的脚本
vim /tmp/optimize-init_sys.sh
#!/bin/bash
##############################################################
# File Name: optimize-init_sys.sh
# Version: V1.0
# Author: Guoxiangfu
# Organization: guoxiangfu.com
# Emai: 978299310@qq.com
# Created Time : 2017-08-15 14:55:22
# Description:
##############################################################
. /etc/init.d/functions
#change system directory: create seripts/software directory
function change_dir(){
ShellDir="/server/scripts"
SoftwareDir="/server/tools"
mkdir -p $ShellDir &&\
mkdir -p $SoftwareDir
}
# input info verify
function info_verify(){
read -p "Please make sure the information you entered (yes|no): " info
case "$info" in
y*|Y*)
continue
;;
n*|N*)
exit 1
;;
esac
}
#change system hostname
function change_hostname(){
read -p "Please input hostname: " HostName
info_verify
hostname $HostName &&\
sed -i "2s/=.*$/=$HostName/g" /etc/sysconfig/network &&\
chk_hosts=$(grep -o "\b$HostName\b" /etc/hosts)
get_ip=$(ifconfig eth0|awk -F "[ :]+" 'NR==2 {print $4}')
if [ -z $chk_hosts ]
then
echo "$get_ip $HostName" >>/etc/hosts
else
continue
fi
}
#boot system optimize: setup chkconfig
function change_chkconfig(){
Boot_options="$1"
for boots in `chkconfig --list|grep "3:on"|awk '{print $1}'|grep -vE "$Boot_options"`
do
chkconfig $boots off
done
}
#setup system optimize: setup ulimit
function change_ulimit(){
grep "* - nofile 65535" /etc/security/limits.conf >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo '* - nofile 65535' >>/etc/security/limits.conf
fi
}
#setup system optimize: setup sysctl
function change_sysctl(){
cat /tmp/sysctl.conf >/etc/sysctl.conf &&\
modprobe bridge &>/dev/null &&\
sysctl -p &>/dev/null
}
#sshd software optimize: change sshd_conf
function change_sshdfile(){
SSH_Port="port 22"
SSH_ListenAddress=$(ifconfig eth0|awk -F "[ :]+" 'NR==2 {print $4}')
SSH_PermitRootLogin="PermitRootLogin no"
SSH_PermitEmptyPassword="PermitEmptyPasswords no"
SSH_GSSAPI="GSSAPIAuthentication no"
SSH_DNS="useDNS no"
#sed -i -e "13s/.*/$SSH_Port/g" /etc/ssh/sshd_config
#sed -i -e "15s/.*/ListenAddress $SSH_ListenAddress/g" /etc/ssh/sshd_config
#sed -i -e "42s/.*/$SSH_PermitRootLogin/g" /etc/ssh/sshd_config
#sed -i -e "65s/.*/$SSH_PermitEmptyPassword/g" /etc/ssh/sshd_config
sed -i -e "81s/.*/$SSH_GSSAPI/g" /etc/ssh/sshd_config
sed -i -e "122s/.*/$SSH_DNS/g" /etc/ssh/sshd_config
}
#selinux software optimize: change disable
function change_selinux(){
sed -i 's#SELINUX=.*#SELINUX=disabled#g' /etc/selinux/config &&\
setenforce 0
}
#firewall software optimize: change stop
function change_firewall(){
/etc/init.d/iptables stop >/dev/null 2>&1
}
#crond software optimize: time synchronization
function change_update(){
grep -i "#crond-id-001" /var/spool/cron/root >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo '#crond-id-001:time sync by hq' >>/var/spool/cron/root
echo "*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1">>/var/spool/cron/root
fi
}
#update yum info
function update_yum(){
wget -q -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
wget -q -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
}
#change profile file info
function change_profile(){
grep "PS1" /etc/profile >>/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "PS1='\[\e[32;1m\][\u@\h \W]\\$ \[\e[0m\]'" >>/etc/profile
fi
grep "alias grep" /etc/profile >>/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "alias grep='grep --color=auto'" >>/etc/profile
echo "alias ll='ls -l --color=auto --time-style=long-iso'" >>/etc/profile
fi
source /etc/profile
}
function main(){
change_dir
change_hostname
change_chkconfig "crond|network|rsyslog|sshd|sysstat"
change_ulimit
change_sysctl
change_sshdfile
change_selinux
change_firewall
change_update
update_yum
change_profile
}
main
action "system optimize complete" /bin/true
1.2.2.2 内核优化的参数
cat>>/tmp/sysctl.conf<<EOF
# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
# sysctl.conf(5) for more details.
# Controls IP packet forwarding
net.ipv4.ip_forward = 0
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1
# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1
# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
# Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536
# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536
# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736
# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296
###################################################################
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
#以下参数是对iptables防火墙的优化,防火墙不开会提示,可以忽略不理。
net.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_tcp_timeout_established = 180
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.wmem_max = 16777216
net.core.rmem_max = 16777216
EOF
1.2.3 执行系统优化的脚本
sh optimize-init_sys.sh
1.3 安装openvpn服务
1.3.1 安装依赖环境安装包
yum -y install openssl openssl-devel lzo openvpn easy-rsa
1.3.2 生成证书文件
cp -pr /usr/share/easy-rsa/2.0/* /etc/openvpn/
vim vars
[root@openvpn openvpn]# source vars
NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/keys
[root@openvpn openvpn]# ./clean-all
[root@openvpn openvpn]# ./build-ca 连续回车
1.3.3 生成服务端证书和密钥
[root@openvpn openvpn]# ./build-key-server server 连续回车—再加两个y
1.3.4 创建Diffie-Hellman,确保key穿越不安全网络的命令:
[root@openvpn openvpn]# ./build-dh
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
1.3.5 生成客户端证书和密钥
[root@openvpn openvpn]# ./build-key client 连续回车—再加两个y
1.3.6 配置服务端openvpn文件server.conf
l
ocal 10.0.0.72
port 19923
proto tcp
dev tun
ca /etc/openvpn/key/ca.crt
cert /etc/openvpn/keys/server.crt
key /etc/openvpn/keys/server.key
dh /etc/openvpn/keys/dh2048.pem
ifconfig-pool-persist /etc/openvpn/ipp.txt
server 172.16.0.0 255.255.255.0
push "route 172.16.1.0 255.255.255.0"
client-to-client
keepalive 20 120
comp-lzo
user root
group root
persist-key
persist-tun
status openvpn-status1.log
log-append openvpn1.log
verb 1
mute 20
1.3.7 防火墙设置
iptables -t nat -A POSTROUTING -s 172.16.0.0/24 -o eth0 -j MASQUERADE
/etc/init.d/iptables save
1.3.8 双网卡配置路由转发
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1 修改为1
1.3.9 客户端配置
[root@openvpn openvpn]# source vars
[root@openvpn openvpn]# ./build-key fuhua 创建的客户端用户 重复回车---最后输入两个y
1.3.10 启动服务端
/etc/init.d/openvpn restart
1.4 Windows客户端下载
1.4.1 下载地址
http://pan.baidu.com/s/1miBRIaO
1.4.2 安装openvpn 2.3.6
1.4.3 在服务端下载刚才创建的客户端的key
[root@openvpn keys]# sz -y fuhua.*
[root@openvpn keys]# sz -y ca.*
1.4.4 手动创建一个客户端client.ovpn配置文件
client
proto tcp
dev tun
remote 10.0.0.72 19923
ca ca.crt
cert fuhua.crt
key fuhua.key
resolv-retry infinite
nobind
mute-replay-warnings
keepalive 20 120
comp-lzo
user root
group root
persist-key
persist-tun
status openvpn-status.log
log-append openvpn.log
verb 3
mute 20
1.4.5 Windows客户端连接
1.4.6 Linux客户端连接
1.4.6.1 下载yum源
wget -q -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
wget -q -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
1.4.6.2 安装openvpn客户端
yum -y install openvpn
1.4.6.3 拷贝文件到openvpn文件目录下
[root@clientopenvpn openvpn]# ls ca.* fuhua.*
ca.crt ca.key fuhua.crt fuhua.csr fuhua.key
1.4.6.4 启动客户端文件
[root@openvpn openvpn]# openvpn --config client.ovpn &
[1] 37649
1.4.6.5 查看ip
[root@openvpn openvpn]# ifconfig tun0
Tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:172.16.0.6 P-t-P:172.16.0.5 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
发表评论
这脚本贴的能不能走点心
有啥问题啊?