>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
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="[email protected]"
export KEY_OU="University"
export KEY_NAME="server"
export EASYRSA_CRL_DAYS=3650
export EASYRSA_CERT_EXPIRE=3650
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_CERT_EXPIRE=3650 ./easyrsa build-server-full server nopass
./easyrsa gen-dh #创建交换密钥
EASYRSA_CRL_DAYS=3650
./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/
cd /etc/openvpn/
openvpn --genkey --secret ta.key
ln -s /usr/share/doc/openvpn/examples/sample-config-files /etc/openvpn/sample-config-files
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
client-config-dir /etc/openvpn/ip
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
vi /etc/openvpn/ip/client
ifconfig-push 10.8.0.6 255.255.255.0
cd /etc/openvpn
/usr/sbin/openvpn --config /etc/openvpn/server.conf &
killall openvpn
新增用户的shell
vi adduser.sh
#!/bin/bash
openvpn_username=$1
export EASYRSA_CRL_DAYS=3650
export EASYRSA_CERT_EXPIRE=3650
cd /usr/share/easy-rsa
#./easyrsa gen-req $openvpn_username nopass
#./easyrsa sign client $openvpn_username
./easyrsa build-client-full $openvpn_username nopass
rm -rf /etc/openvpn/$openvpn_username
mkdir /etc/openvpn/$openvpn_username
cp /usr/share/easy-rsa/pki/private/$openvpn_username.key /etc/openvpn/$openvpn_username
cp /usr/share/easy-rsa/pki/issued/$openvpn_username.crt /etc/openvpn/$openvpn_username
cp /usr/share/easy-rsa/pki/ca.crt /etc/openvpn/$openvpn_username
cp /etc/openvpn/ta.key /etc/openvpn/$openvpn_username
cd /etc/openvpn
echo "client
dev tun
proto tcp
remote 139.224.225.186 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert $openvpn_username.crt
key $openvpn_username.key
remote-cert-tls server
tls-auth ta.key 1
cipher AES-256-GCM
verb 3" > $openvpn_username/$openvpn_username.ovpn
time_txt=$(date "+%Y-%m-%d-%H-%M-%S")
openvpn_client_filename=$openvpn_username-$time_txt
echo $openvpn_client_filename
tar -zcvf $openvpn_client_filename.tar.gz ./$openvpn_username
mv $openvpn_client_filename.tar.gz /var/www/html/
echo "<br /><a href="down.html">down.html</a>" > /var/www/html/index.html
echo "<br /><a href="http://139.224.225.186:88/$openvpn_client_filename.tar.gz">$openvpn_client_filename.tar.gz</a>" > /var/www/html/down.html
# 顺便把客户端证书生成
./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
这一行等于上面两行
#EASYRSA_CERT_EXPIRE=3650 ./easyrsa build-client-full client nopass
客户端文件打包
rm -rf /etc/openvpn/c
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 test230822-1.tar.gz ./c
docker cp u1:/etc/openvpn/test230822-1.tar.gz ./
http://139.224.225.186/test230822-1.tar.gz
client
dev tun
proto tcp
remote 139.224.225.186 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert client.crt
key client.key
remote-cert-tls server
tls-auth ta.key 1
cipher AES-256-GCM
verb 3
windows客户端下载
中国科学技术大学超级计算中心OpenVPN系统
https://scc.ustc.edu.cn/vpn/readme.html
打包完整的服务端!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
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
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 > /a/rules.v4
iptables-restore < /a/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 eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o ens33 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
okok!!!!!!!!!!!!!!!!
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 -j MASQUERADE
okok!!!!!!!!!!
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
test
iptables -t nat -A PREROUTING -d 172.19.99.26 -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 172.19.99.26:99
test
iptables -t nat -A PREROUTING -d 172.17.0.2 -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 172.17.0.2:99
test
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
iptables -t nat -A PREROUTING -d 139.224.225.186 -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 139.224.225.186
test!!!!!!!!!!!!!!
iptables -t nat -A PREROUTING -d 192.168.0.25 -p tcp --dport 2000 -j DNAT --to 10.8.0.6:8080
iptables -t nat -A POSTROUTING -d 10.8.0.6 -p tcp --dport 8080 -j SNAT --to 192.168.0.25:2000
神泣端口
2020-->30800
2021-->30810
iptables -t nat -A PREROUTING -d 192.168.0.25 -p tcp --dport 2020 -j DNAT --to 10.8.0.2:8000
iptables -t nat -A POSTROUTING -d 10.8.0.2 -p tcp --dport 8000 -j SNAT --to 192.168.0.25:2020
iptables -t nat -A PREROUTING -d 192.168.0.25 -p tcp --dport 2022 -j DNAT --to 10.8.0.2:8002
iptables -t nat -A POSTROUTING -d 10.8.0.2 -p tcp --dport 8002 -j SNAT --to 192.168.0.25:2022
iptables -t nat -A PREROUTING -d 192.168.0.25 -p tcp --dport 2020 -j DNAT --to 10.8.0.2:8000
iptables -t nat -A POSTROUTING -d 10.8.0.2 -p tcp --dport 8000 -j SNAT --to 192.168.0.25:2020
iptables -t nat -A PREROUTING -d 192.168.0.25 -p tcp --dport 2021 -j DNAT --to 10.8.0.2:9000
iptables -t nat -A POSTROUTING -d 10.8.0.2 -p tcp --dport 9000 -j SNAT --to 192.168.0.25:2021
iptables -t nat -A PREROUTING -d 192.168.0.25 -p tcp --dport 2022 -j DNAT --to 10.8.0.2:8800
iptables -t nat -A POSTROUTING -d 10.8.0.2 -p tcp --dport 8800 -j SNAT --to 192.168.0.25:2022
121.37.187.195
iptables -t nat -A PREROUTING -d 121.37.187.195 -p tcp --dport 2020 -j DNAT --to 10.8.0.2:8000
iptables -t nat -A POSTROUTING -d 10.8.0.2 -p tcp --dport 8000 -j SNAT --to 121.37.187.195:2020
iptables -t nat -A PREROUTING -d 121.37.187.195 -p tcp --dport 2021 -j DNAT --to 10.8.0.2:9000
iptables -t nat -A POSTROUTING -d 10.8.0.2 -p tcp --dport 9000 -j SNAT --to 121.37.187.195:2021
iptables -t nat -A PREROUTING -d 121.37.187.195 -p tcp --dport 2022 -j DNAT --to 10.8.0.2:8800
iptables -t nat -A POSTROUTING -d 10.8.0.2 -p tcp --dport 8800 -j SNAT --to 121.37.187.195:2022
用阿里云或腾讯云香港轻量应用服务器做传奇微端的方法-架设技术分享 https://www.108pc.com/thread-4353-1-1.html
wget --no-check-certificate -qO InstallNET.sh 'https://moeclub.org/attachment/LinuxShell/InstallNET.sh' && bash InstallNET.sh -dd 'http://down.80host.com/iso/dd/WinSrv2008x64-Chinese_datacenter_sp1_vl_kvm_xen.vhd.gz'
dock!!!!!
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
test smb
iptables -t nat -A PREROUTING -d 172.19.99.26 -p tcp --dport 2040 -j DNAT --to 10.8.0.6:445
iptables -t nat -A POSTROUTING -d 10.8.0.6 -p tcp --dport 445 -j SNAT --to 172.19.99.26:2040
windows
netsh interface portproxy add v4tov4 listenport=445 listenaddress=127.0.0.1 connectport=新端口 connectaddress=服务器IP地址
netsh interface portproxy add v4tov4 listenport=445 listenaddress=127.0.0.1 connectport=2040 connectaddress=139.224.225.186
netsh interface portproxy show all
通过docker搭建openvpn - 吴昊博客
https://blog.whsir.com/post-2809.html
iptables -t nat -F
iptables -F INPUT
iptables -F OUTPUT
iptables -F FORWARD
okok!!!!!!!!!!
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
test
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT –to-source 139.224.225.186
您的公共接口是在此命令的输出中找到的跟在单词“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
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
文章评论