这几天闲着没事干去折腾openvpn,没想到一路下来踩了无数坑,几乎所有的问题都遇到了(我自认为),经过不懈的努力,现在起码是能用了,接下来想将过程记录下来,以便大家能参考一下。
Ⅰ.服务端的配置
首先我们先安装3个依赖:
1.openvpn(服务器端)
2.easy-rsa(证书生成管理)
3.iptables-service(流量转发,服务器用作代理需要)
注:如果你想用自带的firewalld也是可以的,那么你就不用下载iptables了!但是,正所谓一山不容二虎,你不能同时两个都开起来啊,不然会导致连接不上的!但是我更推荐用iptables,问题比较少!
yum -y install openvpn easy-rsa iptables-services1.生成服务端证书
将easy-rsa复制一份到openvpn文件目录下,方便我们整理操作
cp -r /usr/share/easy-rsa/ /etc/openvpn/easy-rsa复制easy-rsa配置文件到/etc/openvpn/easy-rsa/3.0.8目录下,并重命名为vars(如果报错提示不存在这个路径 请查看文件路径的easy-rsa-3.0.8版本是不是与我的不同,如果是,则下你自己的版本路径即可)
cp -r /usr/share/doc/easy-rsa-3.0.8/vars.example /etc/openvpn/easy-rsa/3.0.8/vars选做:修改证书内容(主要是修改个人信息,反正我懒最后也没修改)
vim /etc/openvpn/easy-rsa/3.0.8/varsset_var EASYRSA_REQ_COUNTRY "CN"
set_var EASYRSA_REQ_PROVINCE "GUANGDONG"
set_var EASYRSA_REQ_CITY "GUANGZHOU"
set_var EASYRSA_REQ_ORG "ROCK" #组织
set_var EASYRSA_REQ_EMAIL "24721009098@qq.com"
set_var EASYRSA_REQ_OU "LittleROCK" # 拥有者初始化证书(之后如果想换证书需要从这里重新开始)
cd /etc/openvpn/easy-rsa/3.0.8
./easyrsa init-pki接下来我们生成无密码ca证书(方便后续登录,反正也是个人使用)
./easyrsa build-ca nopass创建server端证书和私钥文件
./easyrsa gen-req server nopass给server端证书签名 期间需要输入yes确认
./easyrsa sign server server创建dh文件,秘钥交换算法
./easyrsa gen-dh创建tls认证秘钥
openvpn --genkey --secret ta.key接下来将生成的证书拷贝到openvpn的文件目录下 方便管理
mkdir /etc/openvpn/certs
cp ./pki/ca.crt /etc/openvpn/certs/
cp ./pki/dh.pem /etc/openvpn/certs/
cp ./pki/issued/server.crt /etc/openvpn/certs
cp ./pki/private/server.key /etc/openvpn/certs
cp ta.key /etc/openvpn/certs2.创建server配置文件
将openvpn的模板拷贝到文件夹中(当然你也可以自己创建一个同名文件)
同样,提示文件路径不存在注意查看openvpn的版本号是不是一样,不一样则换成自己的版本号(或者直接打开文件路径查看文件夹名称即可)
cp /usr/share/doc/openvpn-2.4.12/sample/sample-config-files/server.conf /etc/openvpn/修改配置文件
cd /etc/openvpn
vim server.conf接下来我列出我的配置信息之后再逐一解释:
# 监听本机ip地址
local (这里填本机地址 你服务器的ip 在网卡中查看 ifconfig -> eth0 -> inet后面就是)
# 监控本机端口号
port 1149 #你运行的端口
# 指定采用的传输协议,可以选择tcp或udp
proto tcp
dev tun # 指定创建的通信隧道类型,可选tun或tap,window服务器必须是tap(tun支持手机上使用)
# 指定CA证书的文件路径
ca /etc/openvpn/certs/ca.crt
# 指定服务器端的证书文件路径
cert /etc/openvpn/certs/server.crt
# 指定服务器端的私钥文件路径
key /etc/openvpn/certs/server.key
# 指定迪菲赫尔曼参数的文件路径
dh /etc/openvpn/certs/dh.pem
# 指定虚拟局域网占用的IP地址段和子网掩码,不能和服务器eth0同网段
server 10.8.0.0 255.255.255.0
# 服务器自动给客户端分配IP后,客户端下次连接时,仍然采用上次的IP地址(第一次 分配的IP保存在ipp.txt中,下一次分配其中保存的IP)。
ifconfig-pool-persist ipp.txt
# 自动推送客户端上的网关及DHCP,此项开启了流量转发,有这项才能使用服务器代理上网
push "redirect-gateway def1 bypass-dhcp"
# OpenVPN的DHCP功能为客户端提供指定的 DNS、WINS等
push "dhcp-option DNS 8.8.8.8"
# 备用DNS
push "dhcp-option DNS 8.8.4.4"
# 允许客户端与客户端相连接,默认情况下客户端只能与服务器相连接
client-to-client
# 允许同一个客户端证书多次登录,看需配置 这里前面的 ; 表示注释掉
;duplicate-cn
# 这个选项我选择注释掉的原因非常关键 建议大家先打开看看,文章的最后会补充我这里不开启的原因
;keepalive 10 120
# 开启TLS-auth,使用ta.key防御攻击。服务器端的第二个参数值为0,客户端的为1。
tls-auth /etc/openvpn/certs/ta.key 0
# 加密认证算法,2.4之前是AES-256-CBC
cipher AES-256-GCM
# 使用lzo压缩的通讯,服务端和客户端都必须配置
comp-lzo
# 最大连接用户
;max-clients 100
# 定义运行的用户和组,openvpn用户是安装的时候系统自动创建的
user openvpn
group openvpn
# 重启时仍保留一些状态
persist-key
persist-tun
# 输出短日志,每分钟刷新一次,以显示当前的客户端
status /var/log/openvpn-status.log
# 日志保存路径
log /etc/openvpn/log/openvpn.log
log-append /etc/openvpn/log/openvpn.log
# 指定日志文件的记录详细级别,可选0-9,等级越高日志内容越详细 3为普通运行等级 5-6是调试等级(如果你发现无法连接等问题可开启)
verb 3
#相同信息的数量,如果连续出现 20 条相同的信息,将不记录到日志中
mute 20
# 下面这项只能udp连接开启
#explicit-exit-notify 1
# n秒后重新协商data chan. key (默认值=3600) 这里我设置为0意思是不协商 以防经常断连
reneg-sec 03.配置系统转发,如果想要代理上网的必须配置
允许转发
echo net.ipv4.ip_forward=1 >> /etc/sysctl.conf配置立即生效
sysctl -p防火墙配置(使用iptables)注:推荐使用iptables,网上大部分都是用这个,问题比较少
关闭firewall防火墙
systemctl stop firewalld
systemctl disable firewalld启动iptables
systemctl enable iptables
systemctl start iptables配置iptables转发流量,代理主要以iptables转发实现
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE允许tcp/udp 通过防火墙(你用的什么协议就只写哪一条 写多了可能导致转发失败)
iptables -I INPUT -p tcp --dport 1194(你开放的openvpn端口) -j ACCEPT
iptables -I INPUT -p udp --dport 1194(你开放的openvpn端口) -j ACCEPT保存规则并重启
service iptables save
systemctl restart iptables防火墙配置(使用firewalld)
关闭iptables
systemctl stop iptables
systemctl disable iptables启动firewalld
systemctl start firewalld配置firewalld转发流量,代理主要以firewalld转发实现
开启防火墙伪装,开启后才能转发端口
firewall-cmd --add-masquerade --permanent1111 端口转发 TCP 流量到 10.8.0.0:24 端口(1111端口可以设置成你喜欢的 如果你是udp那么将tcp改为udp 想删掉这些规则将add改为remove即可)
firewall-cmd --add-forward-port=port=1111:proto=tcp:toaddr=10.8.0.0:toport=24 --permanent
重启firewall
firewall-cmd --reload4.创建启动的服务脚本文件
因centos可能没有unit文件(就算有也需要按照下面修改内容),还需要自己新建/修改一个才能启动openvpn服务,写入以下内容
vim /lib/systemd/system/openvpn@.service[Unit]
Description=OpenVPN Robust And Highly Flexible Tunneling Application On %I
After=network.target
[Service]
Type=notify
PrivateTmp=true
ExecStart=/usr/sbin/openvpn --cd /etc/openvpn/ --config %i.conf
[Install]
WantedBy=multi-user.target设置openvpn开机启动
systemctl enable openvpn@server启动openvpn
systemctl start openvpn@server查看是否成功启动
systemctl status openvpn@server如果启动失败,打开日志文件夹查看日志
cd /etc/openvpn/log
cat openvpn.log根据日志内容修改错误部分(一般错误都是由于server.conf文件配置错误导致的)
常见的错误有:
- 服务器监听ip输入错误
- 服务器端口被占用
- ca证书路径输入错误(或压根没写)
- 设置了 udp 却忘记开启explicit-exit-notify 1
- 设置了 tcp 却开启了 explicit-exit-notify 1
Ⅱ.客户端的配置
这里使用windows下的openvpn-2.4.8来演示,在网上下载好安装即可
1.导入证书
在服务端上,生成用户证书
cd /etc/openvpn/easy-rsa/3.0.8生成客户端1的证书,客户端2依次类推
./easyrsa gen-req client1 nopass
注册客户端1的证书,要输入yes
./easyrsa sign client client1将证书拷贝到一个目录存着
cp ./pki/issued/client1.crt /etc/openvpn/client
cp ./pki/private/client1.key /etc/openvpn/client将client1.crt client1.key ta.key ca.crt四个文件下载到本地客户端目录的config目录下
配置客户端配置文件,拷贝客户端sample-config目录下的client.ovpn文件到config目录下
我这里采用Xftp来进行Linux和Windows的文件传输,非常方便。
2.配置客户端配置文件
下面简要写出我所有的配置内容
# 客户端
client
# 隧道类型,与服务器一致
dev tun
# tcp还是udp,与服务器一致
proto tcp
# 服务器ip和端口
remote 202.146.220.146 25000
# 自动重连
resolv-retry infinite
# 不绑定本地特定的端口
nobind
# 服务器重启后保持一些状态
persist-key
persist-tun
# 客户端证书目录
ca ca.crt
cert client1.crt
key client1.key
# 不明,推测是远程证书验证之类的
remote-cert-tls server
# tls握手秘钥,与服务器保持一致,服务器0,客户端1
tls-auth ta.key 1
# 加密方式与服务器一致
cipher AES-256-GCM
# 开启数据压缩与服务器一致
comp-lzo
verb 3
# 相同信息的数量,如果连续出现 20 条相同的信息,将不记录到日志中
mute 20
# 使客户端中所有流量经过VPN,所有网络连接都使用vpn
redirect-gateway def1 bypass-dns
# 不保存密码
auth-nocache如果你是云服务器,记得打开云服务器安全组里面的1194端口(你设置的端口)
至此,所有东西已经设置完成,尝试连接服务器并访问内网,尝试成不成功吧!
如果不成功或中途遇到问题,请看接下来我遇到的所有坑和解决方法:
Ⅲ.遇到的问题和解决方法
1.安装openvpn过程遇到的问题
- 你可能发现,当你直接输入openvpn的时候显示不存在这个命令,那么你一定是安装失败了,使用yum来安装比用wget安装来的方便,重新安装吧!
2.配置证书遇到的问题
- 你可能发现你输入我上文移动证书时的cp指令会失效,那么你一定是没看到我的注释!赶紧翻回去看看吧!
3.客户端无法连接服务器
- 查看客户端配置文件配置是否正确,常见错误:没有放齐4个证书(client1.crt client1.key ta.key ca.crt)
- 证书配置名称与证书名称不匹配(比如配置里面写的是client1.crt而实际名称是client.crt)
- 客户端证书不需要写路径(谁知道你会不会一时糊涂写上去了呢)
- 传输协议是否与服务器对应(两端必须同时为tcp或udp,tun或tap)
- 服务器连接ip、端口是否正确(一般ip不是你在服务端看到的ip,是你外网连接服务器的ip)
- 服务器和客户端是否同时设置了comp-lzo,加密算法是不是同时是cipher AES-256-GCM
- 请检查你的防火墙,转发设置是否正确,端口是否占用,是否只开了一个防火墙(firewalld 或 iptables)一般很大程度上连接不上都是防火墙设置有误导致的!
4.连接上了,能Ping通服务端(10.8.0.1)但是却不能访问互联网
- 检查你的防火墙流量转发设置有没有正确
- 查看防火墙设置是不是混入了奇怪的规则(比如同时设置了tcp和udp传输限制)使用:iptables -F (flush 清除所有的已定规则)、iptables -X (delete 删除所有用户“自定义”的链(tables))、iptables -Z (zero 将所有的chain的计数与流量统计都归零)重新输入防火墙规则,如果还是不行那么就仅写入iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE 规则看看行不行(如果可以 可能是你端口写错了)
- 关掉windows防火墙(一般不是这个原因,最简单的测试方法就是换一台电脑再试一次)
5.连接上了,也能访问互联网,但是一段时间过后自己断开,并且需要换一个端口才能重新连上,并且一段时间自己断开,并且需要换一个端口......
这个问题真的是困扰了我好久,最主要的是还会禁用端口!(我的1145端口以及很多其他端口也就再也连不上了)并且我是个Linux小白,并不知道具体原因,但是通过我的修改了配置文件里的参数莫名其妙就好了
- 将 keepalive规则注释掉(这个非常重要!具体原因未知,应该是tcp协议种种问题导致的)这个是保持客户端与服务器之间的连接所需要的参数,但是让我一直断开并且封禁我端口是咋回事啊喂!
Ⅳ.小结
至此,所有内容写完,如果大家还是有问题,可以将配置文件里面的verb设置成5或6,然后打开log文件(/etc/openvpn/log/openvpn.log)复制错误信息上网百度一下。在安装openvpn的这3天里,踩了无数的坑,希望看到这篇文章的你能通过这篇文章,少走弯路,建立一个属于你自己的vpn!
本文参考链接:




Comments | 61 条评论
wow,真厉害
关闭OpenVPN服务:
重启OpenVPN服务:
前边的都成功了,到客户端这里出错了:
╰─# ./easyrsa sign client client1
Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/3.0.8/vars
Using SSL: openssl OpenSSL 1.1.1u 30 May 2023
Easy-RSA error:
No request found for the input: ‘client1’
Expected to find the request at: /etc/openvpn/easy-rsa/3.0.8/pki/reqs/client1.req
卸载 OpenVPN
Ищете острых ощущений и спонтанного общения?
https://chatruletka18.cam/ Чат рулетка — это уникальный формат онлайн-знакомств,
который соединяет вас с абсолютно случайными
людьми со всего мира через живое видео.
Просто нажмите "Старт", и система моментально подберет вам собеседника.
Никаких анкет, фильтров
или долгих поисков — только живая,
непредсказуемая беседа лицом
к лицу. Это идеальный способ найти новых
друзей, погружаясь в мир случайных, но всегда увлекательных
встреч. Главное преимущество такого сервиса — его
анонимность и полная спонтанность: вы
никогда не знаете, кто окажется по ту сторону экрана в следующий момент.
Saya sudah bermain di LOKET88 dan ternyata luar biasa.
RTP tinggi membuat saya sering dapat jackpot.
Selain itu, penarikan instan membuat saya
makin betah.
Patut dicoba bagi pecinta game online.
Thanks for another informative site. Where else could I get that type of info written in such
a perfect approach? I have a challenge that I’m just now working on, and I have been at the glance out
for such information.
It’s not my first time to pay a visit this
web site, i am visiting this web page dailly
and take pleasant data from here all the time.
Every weekend i used to pay a visit this site, because i wish for enjoyment, as
this this web site conations genuinely pleasant funny stuff too.
My spouse and I stumbled over here coming from a different page and thought I might check things out.
I like what I see so now i am following you. Look forward to looking into your web page for a second time.
ck4444
Asking questions are genuinely good thing if you are not
understanding anything fully, but this paragraph offers pleasant understanding even.
Do you mind if I quote a few of your articles as long as
I provide credit and sources back to your site?
My blog is in the very same niche as yours and my visitors would certainly benefit from a lot of the information you provide here.
Please let me know if this alright with you. Thanks a lot!
The other day, while I was at work, my sister stole my iphone and tested to see if it
can survive a 30 foot drop, just so she can be a youtube sensation. My apple ipad
is now destroyed and she has 83 views. I know this is totally off topic but I had to share it with someone!
Awesome blog! Is your theme custom made or did you download it from somewhere?
A design like yours with a few simple tweeks would really make my
blog stand out. Please let me know where you got your theme.
Kudos
I visited various web sites but the audio feature for audio songs current at this web site is really fabulous.
Very great post. I simply stumbled upon your blog and wanted
to mention that I have really enjoyed surfing around your weblog posts.
After all I’ll be subscribing on your rss feed and I’m hoping you write again soon!
What’s up, yeah this article is actually fastidious and I have learned lot of things from it on the topic of blogging.
thanks.
Hello colleagues, its impressive piece of writing concerning educationand completely
explained, keep it up all the time.
whoah this weblog is excellent i like studying your posts.
Stay up the good work! You understand, many persons are hunting around for this
info, you could aid them greatly.
whoah this blog is excellent i really like reading your articles.
Keep up the great work! You understand, lots of people are hunting around for this information, you could aid them greatly.
What’s up, this weekend is nice for me, for
the reason that this point in time i am reading this impressive
educational piece of writing here at my residence.
I pay a visit every day a few web sites and blogs to
read articles, but this weblog offers feature based content.
Oh my goodness! Awesome article dude! Many thanks,
However I am going through issues with your RSS.
I don’t understand the reason why I can’t subscribe to it.
Is there anyone else having the same RSS issues?
Anyone that knows the solution can you kindly respond?
Thanks!!
Fastidious response in return of this query with genuine arguments and telling the whole thing
about that.
You actually make it seem so easy together with your presentation but I find
this matter to be really something that I believe I’d by no means understand.
It seems too complex and extremely vast for me. I am looking forward
to your next post, I’ll try to get the hang of it!
You should be a part of a contest for one of the highest
quality blogs on the web. I am going to recommend this web
site!
An impressive share! I have just forwarded this onto a co-worker who was conducting a
little research on this. And he actually ordered me dinner due to
the fact that I found it for him… lol. So let me reword this….
Thanks for the meal!! But yeah, thanx for spending some time to discuss this subject here on your blog.
I enjoy looking through a post that can make people think.
Also, many thanks for allowing me to comment!
Unquestionably believe that which you said. Your favorite justification seemed to be on the internet the
easiest thing to be aware of. I say to you, I certainly get annoyed while people think about worries that they just don’t know about.
You managed to hit the nail upon the top and also defined
out the whole thing without having side effect , people could take a
signal. Will probably be back to get more. Thanks
Great items from you, man. I’ve understand your stuff
prior to and you are simply extremely magnificent.
I really like what you’ve received here, certainly like what you are stating and
the way through which you are saying it. You are making it enjoyable and you
still take care of to stay it sensible. I cant wait to
learn much more from you. This is really a wonderful website.
Attractive component of content. I simply stumbled upon your web site and in accession capital to
assert that I acquire in fact enjoyed account your weblog posts.
Any way I will be subscribing for your feeds and even I success you
access consistently fast.
I think the admin of this site is actually working
hard for his web site, as here every information is quality based information.
I visited multiple sites but the audio quality for audio songs present at this web page is in fact fabulous.
We stumbled over here by a different web page and thought
I might as well check things out. I like what I see so
i am just following you. Look forward to looking over your web page repeatedly.
Greetings I am so happy I found your web site, I really
found you by accident, while I was looking on Google for something else,
Anyhow I am here now and would just like to say many thanks for a
tremendous post and a all round entertaining blog (I also
love the theme/design), I don’t have time to
go through it all at the moment but I have book-marked
it and also included your RSS feeds, so when I have
time I will be back to read a great deal more, Please do keep up the awesome jo.
Appreciate this post. Let me try it out.
A person essentially help to make significantly articles
I would state. That is the very first time I frequented your web page and up to now?
I surprised with the analysis you made to create this particular publish
extraordinary. Fantastic activity!
Heya i’m for the first time here. I found this board and I find It truly useful & it
helped me out a lot. I hope to give something back and aid others like you aided me.
Terrific post but I was wondering if you could write
a litte more on this subject? I’d be very grateful if you could elaborate a little bit further.
Cheers!
Wow, this piece of writing is fastidious, my younger sister is analyzing such things, thus
I am going to let know her.
It’s genuinely very difficult in this busy life to listen news on TV, thus
I just use world wide web for that reason, and obtain the latest information.
Hi! I know this is somewhat off topic but I was wondering
if you knew where I could find a captcha plugin for my comment form?
I’m using the same blog platform as yours and I’m having trouble finding one?
Thanks a lot!
When someone writes an article he/she maintains
the thought of a user in his/her mind that how a user can be aware of it.
So that’s why this paragraph is perfect. Thanks!
My brother recommended I would possibly like this blog.
He used to be totally right. This submit actually
made my day. You cann’t believe simply how a lot time I had spent for this information! Thank you!
Howdy this is kind of of off topic but I was wondering if blogs use WYSIWYG editors or if you
have to manually code with HTML. I’m starting a blog soon but have no coding experience so I wanted to get advice
from someone with experience. Any help would be enormously appreciated!
Ridiculous quest there. What occurred after? Take care!
Hello, all the time i used to check blog posts
here early in the daylight, because i enjoy to learn more and more.
I was recommended this web site by my cousin. I’m not sure whether this post is
written by him as nobody else know such detailed about my problem.
You are wonderful! Thanks!
Good way of telling, and nice post to take information on the topic of my presentation topic, which
i am going to deliver in college.
Hello mates, good article and good urging commented at this
place, I am really enjoying by these.
Pretty nice post. I just stumbled upon your blog and wished to say that
I’ve truly enjoyed surfing around your blog posts. In any case I’ll be subscribing to
your feed and I hope you write again very soon!
Good day! Do you know if they make any plugins to assist with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good results.
If you know of any please share. Thanks!
This post will assist the internet users for building up new
website or even a weblog from start to end.
Awesome article.
Do you mind if I quote a couple of your posts as long as
I provide credit and sources back to your weblog?
My blog site is in the very same niche as yours and
my users would genuinely benefit from a lot of the
information you present here. Please let me know if this ok with you.
Regards!
Asking questions are truly pleasant thing if you are not understanding something completely,
however this article provides nice understanding even.
I was suggested this website by my cousin. I’m not sure whether this post is
written by him as nobody else know such detailed about my problem.
You are incredible! Thanks!
I know this site gives quality depending posts and other data, is there any
other site which provides such data in quality?
Aku sudah mengunjungi LOKET88 dan hasilnya sangat memuaskan.
tingkat kemenangan besar membantu saya mudah menang.
Selain itu, withdraw mudah membuat semua terasa aman.
Rekomendasi banget bagi teman-teman yang cari hiburan online.
Truly when someone doesn’t understand after that its up to other people that they will help, so here it
occurs.