openvpn 搭建

[root@zabbix ~]# yum -y install easy-rsa

[root@zabbix ~]# mkdir /opt/easy-rsa

[root@z abbix ~]# cd !$

[root@zabbix easy-rsa]# ll /usr/share/easy-rsa/

lrwxrwxrwx 1 root root  5 Jan  5 11:43 3 -> 3.0.8

lrwxrwxrwx 1 root root  5 Jan  5 11:43 3.0 -> 3.0.8

drwxr-xr-x 3 root root 66 Jan  5 11:43 3.0.8

[root@zabbix easy-rsa]# cp -a  /usr/share/easy-rsa/3.0.8/* ./

cp -a /usr/share/doc/easy-rsa-3.0.8/vars.example  ./vars

[root@zabbix easy-rsa]# vim vars

set_var EASYRSA_DN "cn_only"

set_var EASYRSA_REQ_COUNTRY "CN"

set_var EASYRSA_REQ_PROVINCE "Shanghai"

set_VAR EASYRSA_REQ_CITY "Shanghai"

set_var EASYRSA_REQ_ORG "fuxing"

set_var EASYRSA_REQ_EMAIL "569933452@qq.com"

set_var EASYRSA_NS_SUPPORT "yes"

[root@zabbix easy-rsa]# grep -Ev '^$|^#' vars

if [ -z "$EASYRSA_CALLER" ]; then

echo "You appear to be sourcing an Easy-RSA 'vars' file." >&2

echo "This is no longer necessary and is disallowed. See the section called" >&2

echo "'How to use this file' near the top comments for more details." >&2

return 1

fi

set_var EASYRSA_DN "cn_only"

set_var EASYRSA_REQ_COUNTRY "CN"

set_var EASYRSA_REQ_PROVINCE "Shanghai"

set_VAR EASYRSA_REQ_CITY "Shanghai"

set_var EASYRSA_REQ_ORG "fuxing"

set_var EASYRSA_REQ_EMAIL "569933452@qq.com"

set_var EASYRSA_NS_SUPPORT "yes"

[root@zabbix easy-rsa]# ./easyrsa  init-pki

#生成根证书

[root@zabbix easy-rsa]# ./easyrsa build-ca

/opt/easy-rsa/vars: line 97: set_VAR: command not found

Note: using Easy-RSA configuration from: /opt/easy-rsa/vars

Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017

Enter New CA Key Passphrase:  设置密码 1234

Re-Enter New CA Key Passphrase:    设置密码 1234

Generating RSA private key, 2048 bit long modulus

......................+++

..................................................................................................+++

e is 65537 (0x10001)

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.

-----

Common Name (eg: your user, host, or server name) [Easy-RSA CA]:

CA creation complete and you may now import and sign cert requests.

Your new CA certificate file for publishing is at:

/opt/easy-rsa/pki/ca.crt

生成服务端证书

[root@zabbix easy-rsa]# ./easyrsa gen-req server nopass

/opt/easy-rsa/vars: line 97: set_VAR: command not found

Note: using Easy-RSA configuration from: /opt/easy-rsa/vars

Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017

Generating a 2048 bit RSA private key

...+++

....................................+++

writing new private key to '/opt/easy-rsa/pki/easy-rsa-54384.2xag0s/tmp.Kho3Lj'

-----

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.

-----

Common Name (eg: your user, host, or server name) [server]:  回车

Keypair and certificate request completed. Your files are:

req: /opt/easy-rsa/pki/reqs/server.req

key: /opt/easy-rsa/pki/private/server.key

[root@zabbix easy-rsa]# ./easyrsa  sign server server

/opt/easy-rsa/vars: line 97: set_VAR: command not found

Note: using Easy-RSA configuration from: /opt/easy-rsa/vars

Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017

You are about to sign the following certificate.

Please check over the details shown below for accuracy. Note that this request

has not been cryptographically verified. Please be sure it came from a trusted

source or that you have verified the request checksum with the sender.

Request subject, to be signed as a server certificate for 825 days:

subject=

    commonName                = server

Type the word 'yes' to continue, or any other input to abort.

  Confirm request details: yes  输入yes

Using configuration from /opt/easy-rsa/pki/easy-rsa-54459.15y3wH/tmp.f0xZtk

Enter pass phrase for /opt/easy-rsa/pki/private/ca.key:  密码为1234

Check that the request matches the signature

Signature ok

The Subject's Distinguished Name is as follows

commonName            :ASN.1 12:'server'

Certificate is to be certified until Apr 10 04:02:19 2023 GMT (825 days)

Write out database with 1 new entries

Data Base Updated

Certificate created at: /opt/easy-rsa/pki/issued/server.crt

生成算法

[root@zabbix easy-rsa]# ./easyrsa  gen-dh

生成客户端 证书

[root@zabbix easy-rsa]# ./easyrsa gen-req client nopass

[root@zabbix easy-rsa]# ./easyrsa sign client client

/opt/easy-rsa/vars: line 97: set_VAR: command not found

Note: using Easy-RSA configuration from: /opt/easy-rsa/vars

Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017

You are about to sign the following certificate.

Please check over the details shown below for accuracy. Note that this request

has not been cryptographically verified. Please be sure it came from a trusted

source or that you have verified the request checksum with the sender.

Request subject, to be signed as a client certificate for 825 days:

subject=

    commonName                = client

Type the word 'yes' to continue, or any other input to abort.

  Confirm request details: yes  输入yes

Using configuration from /opt/easy-rsa/pki/easy-rsa-54701.biJwTN/tmp.QYdddb

Enter pass phrase for /opt/easy-rsa/pki/private/ca.key: 密码1234

Check that the request matches the signature

Signature ok

The Subject's Distinguished Name is as follows

commonName            :ASN.1 12:'client'

Certificate is to be certified until Apr 10 04:05:56 2023 GMT (825 days)

Write out database with 1 new entries

Data Base Updated

Certificate created at: /opt/easy-rsa/pki/issued/client.crt

[root@zabbix easy-rsa]# yum -y install openvpn

[root@zabbix easy-rsa]# vim /etc/openvpn/server.conf

[root@zabbix easy-rsa]# more !$

more /etc/openvpn/server.conf

port 1194 #端口

proto udp #协议

dev tun #采用路由隧道模式tun

ca ca.crt #ca证书文件位置

cert server.crt #服务器端公钥名称

key server.key #服务端私钥名称

dh dh.pem #交换证书

server 172.16.99.0 255.255.255.0 #给客户端分配地址池,注意,不能和VPN服务器内网网段有相同

push "route 10.197.0.0 255.255.0.0" #允许客户端访问内网10.197.0.0网段

ifconfig-pool-persist ipp.txt #地址池记录文件位置

keepalive 10 120 #存活时间,10秒ping一次,120如未收到响应则视为断线

max-clients 100 #最多允许 100个客户端连接

status openvpn-status.log #日志记录位置

verb 3 #openvpn版本

client-to-client #客户端与客户端之间支持通信

log /var/log/openvpn.log #openvpn日志记录位置

persist-key #通过keepalive 检测超时后,重新启动VPN,不重新读取keys,保留第一次使用的keys

persist-tun #检测超时后,重新启动VPN,一直保持tun是linkup的。否则网络会优先linkdown再linkup。

duplicate-cn

[root@zabbix openvpn]# cd /etc/openvpn/

[root@zabbix openvpn]# cp -a /opt/easy-rsa/pki/ca.crt  ./

[root@zabbix openvpn]# cp -a /opt/easy-rsa/pki/issued/server.crt ./

[root@zabbix openvpn]# cp -a /opt/easy-rsa/pki/private/server.key  ./

[root@zabbix openvpn]# cp -a /opt/easy-rsa/pki/dh.pem  ./

[root@zabbix openvpn]# systemctl -f enable openvpn@server.service

Created symlink from /etc/systemd/system/multi-user.target.wants/openvpn@server.service to /usr/lib/systemd/system/openvpn@.service.

[root@zabbix openvpn]# systemctl start openvpn@server.service

安装 openvpn客户端

[root@zabbix openvpn]#  sz /opt/easy-rsa/pki/ca.crt

[root@zabbix openvpn]#  sz /opt/easy-rsa/pki/ca.crt

[root@zabbix openvpn]# sz /opt/easy-rsa/pki/issued/client.crt

OO

[root@zabbix openvpn]# sz /opt/easy-rsa/pki/private/client.key

windows创建fx.ovpns

client  #指定当前VPN是客户端

dev tun #使用tun隧道传输协议

proto udp #使用udp协议传输数据

remote 10.197.17.99 1194 #openvpn #服务器IP地址端口号

resolv-retry infinite #断线自动重新连接

nobind #不绑定本地特定的端口号

ca ca.crt #指定CA证书的文件路径

cert client.crt  #指定当前客户端的私钥文件路径

key client.key #指定当前 客户端 的私钥文件路径

verb 3 #指定日志文件的记录详细级别,可选0-9

persist-key #通过keepalive检测超时后,重新启动VPN,不重新读取keys,保留第一次使用的keys

persist-tun #检测超时后,重新启动VPN,一直保持tun是linkup的。否则网络会先linkdown然后再linkup

开启内核转发

[root@zabbix openvpn]# echo "net.ipv4.ip_forward=1" >>/etc/sysctl.conf

[root@zabbix openvpn]#  sysctl -p

net.ipv4.ip_forward = 1

第一种:访问内部资源 ,需要手动一台台添加回指路由

route add -net  172.16.99.0/24 gw 10.197.17.99

第二种 开启 防火墙伪装

[root@zabbix openvpn]# systemctl  start firewalld.service

[root@zabbix openvpn]#

[root@zabbix openvpn]# firewall-cmd  --list-all

public (active)

  target: default

  icmp-block-inversion: no

  interfaces: eth0

  sources:

  services: ssh dhcpv6-client

  ports:

  protocols:

  masquerade: no

  forward-ports:

  source-ports:

  icmp-blocks:

  rich rules:

永久添加

[root@zabbix openvpn]# firewall-cmd --add-masquerade --permanent

success

添加openvpn服务

[root@zabbix openvpn]# firewall-cmd --add-service=openvpn

success

firewall-cmd --add-service=openvpn --permanent

重新加载

[root@zabbix openvpn]# firewall-cmd --reload

success

[root@zabbix openvpn]# firewall-cmd  --list-all

public (active)

  target: default

  icmp-block-inversion: no

  interfaces: eth0

  sources:

  services: ssh dhcpv6-client

  ports:

  protocols:

  masquerade: yes

  forward-ports:

  source-ports:

  icmp-blocks:

  rich rules:

双重认证

/etc/openvpn/server.conf

script-security 3

auth-user-pass-verify /etc/openvpn/check.sh via-env

username-as-common-name

[root@openvpn ~]# vim /etc/openvpn/check.sh

#!/bin/sh

###########################################################

PASSFILE="/etc/openvpn/openvpnfile"

LOG_FILE="/var/log/openvpn-password.log"

TIME_STAMP=`date "+%Y-%m-%d %T"`

if [ ! -r "${PASSFILE}" ]; then

    echo "${TIME_STAMP}: Could not open password file \"${PASSFILE}\" for reading." >> ${LOG_FILE}

    exit 1

fi

CORRECT_PASSWORD=`awk '!/^;/&&!/^#/&&$1=="'${username}'"{print $2;exit}' ${PASSFILE}`

if [ "${CORRECT_PASSWORD}" = "" ]; then

    echo "${TIME_STAMP}: User does not exist: username=\"${username}\", password=\"${password}\"." >> ${LOG_FILE}

    exit 1

fi

if [ "${password}" = "${CORRECT_PASSWORD}" ]; then

    echo "${TIME_STAMP}: Successful authentication: username=\"${username}\"." >> ${LOG_FILE}

    exit 0

fi

echo "${TIME_STAMP}: Incorrect password: username=\"${username}\", password=\"${password}\"." >> ${LOG_FILE}

exit 1

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容