>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
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

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐