阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

在内网搭建一台服务器实现从外网连接到内网

222次阅读
没有评论

共计 8298 个字符,预计需要花费 21 分钟才能阅读完成。

在内网搭建一台服务器实现从外网连接到内网

环境
服务端:CentOS 6.7 32-bit
客户端:Windows XP

服务端配置
# 关闭 SELinux
sed -i ‘/^SELINUX\b/s/=.*/=disabled/’ /etc/selinux/config
setenforce 0

# 安装 EPEL 源(默认 yum 源没有 openvpn 和 easy-rsa 软件包)
rpm -ivh http://mirrors.ustc.edu.cn/Fedora/epel/5/i386/epel-release-5-4.noarch.rpm

# 安装 openvpn 和 easy-rsa 软件包
yum -y install openvpn easy-rsa

# 切换到 /usr/share/easy-rsa/2.0/ 目录
cd /usr/share/easy-rsa/2.0/
# 初始化环境变量
source vars
# 清除所有与证书相关的文件
./clean-all
# 生成 CA 相关文件(一路按回车即可)
./build-ca
# 生成服务端相关文件(一路按回车,直到提示需要输入 y / n 时,输入 y 再按回车,一共两次)
./build-key-server server
# 生成客户端相关文件(一路按回车,直到提示需要输入 y / n 时,输入 y 再按回车,一共两次)
./build-key client
# 生成 dh2048.pem 文件(生成过程时快时慢,在此期间不要去中断它)
./build-dh
# 生成 ta.key 文件(防 DDos 攻击)
openvpn –genkey –secret keys/ta.key
# 在 openvpn 的配置目录下新建一个 key 目录
mkdir /etc/openvpn/keys
# 将 openvpn 配置文件需要用到的文件复制一份到刚创建好的 keys 目录中
cp /usr/share/easy-rsa/2.0/keys/{ca.crt,server.{crt,key},dh2048.pem,ta.key} /etc/openvpn/keys/

# 创建 /etc/openvpn/server.conf 文件,内容如下
port 1194
proto udp
dev tun
ca keys/ca.crt
cert keys/server.crt
key keys/server.key  # This file should be kept secret
dh keys/dh2048.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push “route 192.168.1.0 255.255.255.0” # 192.168.1.0/24 是我这台 VPN 服务器所在的内网的网段,读者应该根据自身实际情况进行修改
keepalive 10 120
tls-auth keys/ta.key 0 # This file is secret
comp-lzo
persist-key
persist-tun
status openvpn-status.log
verb 3

# 开启路由转发功能
sed -i ‘/net.ipv4.ip_forward/s/0/1/’ /etc/sysctl.conf
echo 1 > /proc/sys/net/ipv4/ip_forward

# 配置防火墙
iptables -F
iptables -X
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE
service iptables save

# 启动 openvpn 服务并将其设置为开机启动
service openvpn start
chkconfig openvpn on
客户端配置
# 创建一份客户端文件(命名为 client.ovpn),内容如下(读者要注意修改下面的服务端公网 IP)
client
dev tun
proto udp
remote 服务端公网 IP 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ns-cert-type server
comp-lzo
verb 3
tls-auth [inline] 1
<ca>
将 /usr/share/easy-rsa/2.0/keys/ca.crt 的全部内容复制粘贴于此
</ca>
<cert>
将 /usr/share/easy-rsa/2.0/keys/client.crt 的全部内容复制粘贴于此
</cert>
<key>
将 /usr/share/easy-rsa/2.0/keys/client.key 的全部内容复制粘贴于此
</key>
<tls-auth>
将 /usr/share/easy-rsa/2.0/keys/ta.key 的全部内容复制粘贴于此
</tls-auth>
从服务端下载 client.ovpn,并将其复制到 openvpn 的安装目录的 config 目录下,最后,启动 openvpn 程序,连接服务端,如果能获取到 IP,且能 ping 内网的其他机器就表示配置成功了。
最后给出我的 client.ovpn 的范例文本供读者参考。

client
dev tun
proto udp
remote 192.168.1.88 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ns-cert-type server
comp-lzo
verb 3
tls-auth [inline] 1
<ca>
—–BEGIN CERTIFICATE—–
MIIFEjCCA/qgAwIBAgIJALomSu6uks0gMA0GCSqGSIb3DQEBCwUAMIG2MQswCQYD
VQQGEwJVUzELMAkGA1UECBMCQ0ExFTATBgNVBAcTDFNhbkZyYW5jaXNjbzEVMBMG
A1UE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7
116cd7a43d5f9d02c84d566406a3a657
84f1e69c23c3d954b1a19dc4d373b8a3
7c717d397c51e947183a628c4f4a7e98
173a65e0ce9806b2b04f1ce0e45ffacb
67bbca2db49cb3b78c573b85fb3d79c4
bbbf61d9147513957ac4668e541db859
c449eaf04b0d0585dc4c102ca010d91a
5ad275b7fb13e95f0a971a88a7550cb4
3485825fb6304b8537ac9cd6af5fda68
4a0d94d47f3a0478e722f20e0043de1c
c18684f5b68e6f19ad5b302cb9ddc1ca
b326c80c4b6bb235dda607a5fa79fbc8
5da586741a428e2ab390827c5145893a
d78f0bef7c86710ec7752d60cb94cada
—–END OpenVPN Static key V1—–
</tls-auth>

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-05/131226.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计8298字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中