openvpn easy-rsa

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ubuntu18
apt-get install openvpn easy-rsa libssl-dev openssl
Service - OpenVPN | Ubuntu
https://ubuntu.com/server/docs/service-openvpn

ubuntu 22.04搭建OpenVPN服务器_ubuntu vpn_躲猫猫的喵喵的博客-CSDN博客
https://blog.csdn.net/wfjdemmye/article/details/131486127

查看安装软件的详细安装信息
dpkg --list openvpn
dpkg -L easy-rsa


export KEY_COUNTRY="CN"
export KEY_PROVINCE="GD"
export KEY_CITY="ShanTou City"
export KEY_ORG="STU"
export KEY_EMAIL="qfuqin@163.com"
export KEY_OU="University"
export KEY_NAME="server"

tar -xf EasyRSA-3.0.8.tgz            #解压证书生成工具
cd EasyRSA-3.0.8/                    #进入工具目录
./easyrsa init-pki                   #创建一个新的 PKI 和 CA
./easyrsa build-ca nopass            #创建不需要密码的新CA 
./easyrsa gen-req server nopass      #创建不需要密码的服务器证书 此处server 为证书名字
./easyrsa sign server server         # 此处第二个server 为上一步的证书名字
./easyrsa gen-dh                     #创建交换密钥 


./easyrsa gen-crl server nopass  

cp pki/dh.pem /etc/openvpn/
cp pki/ca.crt /etc/openvpn/
cp pki/issued/server.crt /etc/openvpn/
cp pki/private/server.key /etc/openvpn/   
cp pki/crl.pem /etc/openvpn/           #将证书copy 到openvpn 目录
cd /etc/openvpn/
openvpn --genkey --secret ta.key


ln -s /usr/share/doc/openvpn/examples/sample-config-files /etc/openvpn/sample-config-files

vi /etc/openvpn/server.conf


local 0.0.0.0
port 1194
proto tcp
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh.pem
topology subnet
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist /var/log/openvpn/ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
keepalive 10 120
tls-auth ta.key 0 
cipher AES-256-GCM
persist-key
persist-tun
status /var/log/openvpn/openvpn-status.log
verb 3

Openvpn 分配固定IP
修改server.conf 添加一行:
client-config-dir /etc/openvpn/ip
添加客户端固定IP
cat > /etc/openvpn/ip/client
ifconfig-push 10.9.0.5 10.9.0.6


/usr/sbin/openvpn --config /etc/openvpn/server.conf


# 顺便把客户端证书生成
./easyrsa gen-req client nopass       #创建不需要密码的客户端证书 此处client 为证书名字
生成的文件
req: /usr/share/easy-rsa/pki/reqs/client.req
key: /usr/share/easy-rsa/pki/private/client.key

./easyrsa sign client client           # 此处第二个client 为上一步的证书名字
生成的文件
Certificate created at: /usr/share/easy-rsa/pki/issued/client.crt

客户端文件打包
mkdir /etc/openvpn/c
cp /usr/share/easy-rsa/pki/private/client.key /etc/openvpn/c
cp /usr/share/easy-rsa/pki/issued/client.crt /etc/openvpn/c
cp /usr/share/easy-rsa/pki/ca.crt /etc/openvpn/c
cp /etc/openvpn/ta.key /etc/openvpn/c

cd /etc/openvpn
tar -zcvf test134.tar.gz ./c


windows客户端下载
中国科学技术大学超级计算中心OpenVPN系统
https://scc.ustc.edu.cn/vpn/readme.html


打包完整的服务端!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

我自己打包的文件

【免费】openvpn-easy-rsa完整配置资源-CSDN文库
https://download.csdn.net/download/cyuyan112233/88215927?spm=1001.2014.3001.5503

cd /etc/openvpn
tar -zcvf openvpn-server-bak.tar.gz /etc/openvpn
tar -xzvf openvpn-server-bak.tar.gz -C /

okokok!!!!!!!!!!!!!!!!
systemctl enable openvpn@server
systemctl disable openvpn@server
systemctl start openvpn@server
systemclt status openvpn@server

okokok!!!!!!!!!!!!!!!!!!
/etc/sysctl.conf
net.ipv4.ip_forward=1
sudo sysctl -p

okokok!!!!!!!!!!!!!!!!!!!
DROP改成ACCEPT vim /etc/default/ufw
DEFAULT_FORWARD_POLICY="ACCEPT"
ufw disable
ufw status
ufw enable
ufw allow 1194/tcp
echo 1 > /proc/sys/net/ipv4/ip_forward

dns设置
vi /etc/resolv.conf

防火墙
iptables-save > /etc/iptables/rules.v4
iptables-restore < /etc/iptables/rules.v4

安装
apt-get install iptables-persistent
iptables-legacy-save > /etc/iptables/rules.v4
iptables-legacy-restore < /etc/iptables/rules.v4
-A POSTROUTING -s 10.8.0.0/24 -o ens33 -j MASQUERADE

iptables -t nat -F
iptables -F INPUT
iptables -F OUTPUT
iptables -F FORWARD
iptables -t nat -A POSTROUTING -s 0/0 -j MASQUERADE

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
iptables -t nat -A PREROUTING -d 192.168.0.25 -p tcp --dport 99 -j DNAT --to 10.8.0.6:80
iptables -t nat -A POSTROUTING -d 10.8.0.6 -p tcp --dport 80 -j SNAT --to 192.168.0.25:99
通过docker搭建openvpn - 吴昊博客
https://blog.whsir.com/post-2809.html

iptables -t nat -F
iptables -F INPUT
iptables -F OUTPUT
iptables -F FORWARD
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o ens33 -j MASQUERADE
iptables -A INPUT -j REJECT iptables -A FORWARD -j REJECT 
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT –to-source 192.168.8.134


您的公共接口是在此命令的输出中找到的跟在单词“dev”之后的字符串。例如,此结果显示名为 的接口eth0,它在下面突出显示:
ip route list default
Output
default via 159.65.160.1 dev eth0 proto static


/etc/ufw/before.rules
*nat
:POSTROUTING ACCEPT [0:0]
# Allow traffic from OpenVPN client to eth0(changeto the interface you discovered!)
-A POSTROUTING -s 10.8.0.0/8 -o eth0 -jMASQUERADE #这里为配置文件设置的虚拟网段
COMMIT


iptables -A INPUT -j REJECT iptables -A FORWARD -j REJECT 
有的VPS比较奇怪,我们需要再加点料iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT –to-source 114.138.164.131 –to-source后面跟你VPS的IP

okokok!!!!!!!!!!!!!!!
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE
iptables -A FORWARD -s 10.8.0.0/24 -j ACCEPT
iptables -A FORWARD -d 10.8.0.0/24 -j ACCEPT

okokok!!!!!!!!!!!!!!

#iptables-save > /etc/network/iptables.up.rules
iptables-save > /etc/network/interfaces

iptables -t nat -L #查看nat规则
iptables -t nat -A POSTROUTING -s 10.0.1.0/24 -j MASQUERADE #针对10.0.1.0(虚拟的vpn网段)这个网段进行地址转换
iptables -t nat -A POSTROUTING -s 10.0.1.0/24 -o ens33 -j MASQUERADE

MASQUERADE,地址伪装,在iptables中有着和SNAT相近的效果,但也有一些区别,但使用SNAT的时候,出口ip的地址范围可以是一个,也可以是多个,例如:
如下命令表示把所有10.8.0.0网段的数据包SNAT成192.168.5.3的ip然后发出去
iptables -t nat -A POSTROUTING -s 10.8.0.0/255.255.255.0 -o eth0 -j SNAT --to-source 192.168.5.3

如下命令表示把所有10.8.0.0网段的数据包SNAT成192.168.5.3/192.168.5.4/192.168.5.5等几个ip然后发出去
iptables -t nat -A POSTROUTING -s 10.8.0.0/255.255.255.0 -o eth0 -j SNAT --to-source 192.168.5.3-192.168.5.5

test!!!!!!!!!!!!!!
iptables -t nat -A POSTROUTING -s 10.8.0.0/255.255.255.0 -o ens33 -j MASQUERADE


开启转发:echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0(外网网卡) -j SNAT --to(外网地址)
备用:iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eth0 -j MASQUERADE
备用:iptables -t nat -A POSTROUTING -s 192.168.10.0/24  -j MASQUERADE
映射内部web: iptables -t nat -A PREROUTING -d (外网地址) -p tcp --dport 80 -j DNAT --to 10.1.120.251

自动启动
okokok!!!!!!!!!!!!!!!!!!!!!!!!!
1.检查系统目录/lib/systemd/system/rc-local.service
#######################################
/etc/rc.local
#!/bin/bash
添加 iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE
chmod 777 /etc/rc.local
#######################################

2.创建/etc/rc.local脚本文件,并写入想要运行的脚本程序
(可以先写个示例验证一下)
#! /bin/bash
#### 这里在/usr/local里面创建文件夹是想看是否有执行的权限
#### 事实证明是有的
mkdir /usr/local/temp
echo "test auto bootstrap" > /usr/local/temp/1.log
3.给rc.local执行的权限
sudo chmod +x /etc/rc.local
4.启用服务
sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

OpenVPN吊销用户和增加用户

OpenVPN吊销用户和增加用户-一亩三分地  https://www.qiqios.cn/279.html
https://www.qiqios.cn/279.html
./easyrsa revoke client
./easyrsa gen-crl

查看吊销用户

root@3a333541c8a1:/usr/share/easy-rsa# cat pki/index.txt
R       251120070739Z   230818090418Z   9DD3FC33B05736AB11FB4839DCCEE633        unknown /CN=client

编辑服务器配置文件,重启服务 
vi /etc/openvpn/server.conf
crl-verify /usr/share/easy-rsa/pki/crl.pem
systemctl restart openvpn.service

手动关闭启动

killall -TERM openvpn

cd /etc/openvpn
/usr/sbin/openvpn --config /etc/openvpn/server.conf &


<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

##############################################
# Sample client-side OpenVPN 2.0 config file #
# for connecting to multi-client server.     #
#                                            #
# This configuration can be used by multiple #
# clients, however each client should have   #
# its own cert and key files.                #
#                                            #
# On Windows, you might want to rename this  #
# file so it has a .ovpn extension           #
##############################################

# Specify that we are a client and that we
# will be pulling certain config file directives
# from the server.
client

# Use the same setting as you are using on
# the server.
# On most systems, the VPN will not function
# unless you partially or fully disable
# the firewall for the TUN/TAP interface.
;dev tap
dev tun

# Windows needs the TAP-Win32 adapter name
# from the Network Connections panel
# if you have more than one.  On XP SP2,
# you may need to disable the firewall
# for the TAP adapter.
;dev-node MyTap

# Are we connecting to a TCP or
# UDP server?  Use the same setting as
# on the server.
proto tcp
;proto udp

# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote 139.224.225.186 1194
;remote my-server-2 1194

# Choose a random host from the remote
# list for load-balancing.  Otherwise
# try hosts in the order specified.
;remote-random

# Keep trying indefinitely to resolve the
# host name of the OpenVPN server.  Very useful
# on machines which are not permanently connected
# to the internet such as laptops.
resolv-retry infinite

# Most clients don't need to bind to
# a specific local port number.
nobind

# Downgrade privileges after initialization (non-Windows only)
;user nobody
;group nobody

# Try to preserve some state across restarts.
persist-key
persist-tun

# If you are connecting through an
# HTTP proxy to reach the actual OpenVPN
# server, put the proxy server/IP and
# port number here.  See the man page
# if your proxy server requires
# authentication.
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]

# Wireless networks often produce a lot
# of duplicate packets.  Set this flag
# to silence duplicate packet warnings.
;mute-replay-warnings

# SSL/TLS parms.
# See the server config file for more
# description.  It's best to use
# a separate .crt/.key file pair
# for each client.  A single ca
# file can be used for all clients.
ca ca.crt
cert client.crt
key client.key

# Verify server certificate by checking that the
# certificate has the correct key usage set.
# This is an important precaution to protect against
# a potential attack discussed here:
#  http://openvpn.net/howto.html#mitm
#
# To use this feature, you will need to generate
# your server certificates with the keyUsage set to
#   digitalSignature, keyEncipherment
# and the extendedKeyUsage to
#   serverAuth
# EasyRSA can do this for you.
remote-cert-tls server

# If a tls-auth key is used on the server
# then every client must also have the key.
tls-auth ta.key 1

# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
# Note that v2.4 client/server will automatically
# negotiate AES-256-GCM in TLS mode.
# See also the data-ciphers option in the manpage
;cipher AES-256-CBC
cipher AES-256-GCM

# Enable compression on the VPN link.
# Don't enable this unless it is also
# enabled in the server config file.
#comp-lzo

# Set log file verbosity.
verb 3

# Silence repeating messages
;mute 20


指定客户端ip设置

19:05 2024/2/28
/etc/openvpn/server.conf
client-config-dir /etc/openvpn/ccd


/etc/openvpn/ccd/client
ifconfig-push 10.8.0.6 255.255.255.0

》》》》》》》》》》》》》》》》》》》》》》》》

神泣端口映射

iptables -t nat -A PREROUTING -d 172.19.99.26 -p tcp --dport 2020 -j DNAT --to 10.8.0.6:30800
iptables -t nat -A POSTROUTING -d 10.8.0.6 -p tcp --dport 30800 -j SNAT --to 172.19.99.26:2020
iptables -t nat -A PREROUTING -d 172.19.99.26 -p tcp --dport 2021 -j DNAT --to 10.8.0.6:30810
iptables -t nat -A POSTROUTING -d 10.8.0.6 -p tcp --dport 30810 -j SNAT --to 172.19.99.26:2021

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 安装OpenVPN 首先,安装OpenVPN及其依赖包: ``` yum install -y epel-release yum install -y openvpn easy-rsa ``` 2. 生成证书 创建一个目录用于存储证书: ``` mkdir /etc/openvpn/easy-rsa cp -r /usr/share/easy-rsa/* /etc/openvpn/easy-rsa/ ``` 编辑文件 `/etc/openvpn/easy-rsa/vars`,修改以下变量: ``` export KEY_COUNTRY="CN" export KEY_PROVINCE="Beijing" export KEY_CITY="Beijing" export KEY_ORG="My Company" export KEY_EMAIL="admin@example.com" export KEY_OU="My Unit" ``` 切换到 `/etc/openvpn/easy-rsa/` 目录,执行以下命令: ``` ./easyrsa init-pki ./easyrsa build-ca ./easyrsa gen-dh ./easyrsa gen-req server nopass ./easyrsa sign-req server server ./easyrsa gen-req client nopass ./easyrsa sign-req client client ``` 以上命令中,第一行初始化PKI(public key infrastructure)目录,第二行生成CA(Certificate Authority)证书,第三行生成DH(Diffie-Hellman)参数,第四行生成服务器证书请求,第五行签署服务器证书请求,第六行生成客户端证书请求,第七行签署客户端证书请求。 3. 配置OpenVPN 创建配置文件 `/etc/openvpn/server.conf`,并添加以下内容: ``` port 1194 proto udp dev tun ca /etc/openvpn/easy-rsa/pki/ca.crt cert /etc/openvpn/easy-rsa/pki/issued/server.crt key /etc/openvpn/easy-rsa/pki/private/server.key dh /etc/openvpn/easy-rsa/pki/dh.pem server 10.8.0.0 255.255.255.0 ifconfig-pool-persist ipp.txt push "redirect-gateway def1 bypass-dhcp" push "dhcp-option DNS 8.8.8.8" push "dhcp-option DNS 8.8.4.4" keepalive 10 120 cipher AES-256-CBC user nobody group nobody persist-key persist-tun status openvpn-status.log verb 3 ``` 以上配置文件中,`port` 指定OpenVPN监听的端口号,`proto` 指定使用的协议(UDP或TCP),`dev` 指定使用的虚拟网络设备,`ca`、`cert`、`key`、`dh` 分别指定证书和私钥文件的路径,`server` 指定虚拟IP地址池,`ifconfig-pool-persist` 指定分配的IP地址和客户端名称的持久化存储文件,`push` 指令用于将服务端配置推送给客户端,`keepalive` 指定心跳包的时间间隔和重试次数,`cipher` 指定加密算法,`user`、`group` 指定OpenVPN进程的运行用户和组,`persist-key`、`persist-tun` 分别指定持久化存储密钥和虚拟网络设备,`status` 指定状态日志文件,`verb` 指定日志输出级别。 4. 启动OpenVPN 启动OpenVPN服务: ``` systemctl start openvpn@server ``` 查看服务状态: ``` systemctl status openvpn@server ``` 如果服务正常运行,可以通过以下命令查看OpenVPN服务器的IP地址: ``` ip addr show tun0 ``` 5. 客户端配置 在客户端电脑上安装OpenVPN客户端,下载证书和配置文件: ``` scp root@<server_ip>:/etc/openvpn/easy-rsa/pki/ca.crt <destination_folder> scp root@<server_ip>:/etc/openvpn/easy-rsa/pki/issued/client.crt <destination_folder> scp root@<server_ip>:/etc/openvpn/easy-rsa/pki/private/client.key <destination_folder> scp root@<server_ip>:/etc/openvpn/client.ovpn <destination_folder> ``` 编辑文件 `client.ovpn`,将以下内容替换为实际值: ``` remote <server_ip> 1194 proto udp dev tun nobind persist-key persist-tun ca ca.crt cert client.crt key client.key cipher AES-256-CBC ``` 将 `ca.crt`、`client.crt` 和 `client.key` 移动到OpenVPN客户端的配置目录,例如 `/etc/openvpn/client/`。 启动OpenVPN客户端: ``` openvpn --config /etc/openvpn/client/client.ovpn ``` 如果连接成功,可以通过以下命令查看客户端的IP地址: ``` ip addr show tun0 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值