[toc]
CentOS7手动安装OpenVPN
1.前期环境准备
1.1 实验环境
OpenVPN软件版本 2.4.9
系统 | IP | 公网IP | 配置 | 内核 |
---|---|---|---|---|
CentOS7.9 | 172.16.0.71 | 8.8.8.8 | 1c1g | 3.10.0-1160.11.1.el7.x86_64 |
mac本机 | 10.0.18.249 | 9.9.9.9 | - | - |
1.2 OpenVPN机器配置必要修改
1.2.1 开启路由转发
# 不存在则配置路由转发
grep 'net.ipv4.ip_forward = 1' /etc/sysctl.conf || echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
# 使配置生效
sysctl -p
1.2.2 iptables配置
设置iptables规则
# 客户端连接vpn后,默认分配 10.8.0.0/24网段,需要进行nat设置
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
# 配置开机启动
echo 'iptables -t nat -A POSTROUTING -s 10.7.0.0/24 -o eth0 -j MASQUERADE' >> /etc/rc.d/rc.local
# 给rc.local文件增加可执行权限,否则开机不会执行
chmod u+x /etc/rc.d/rc.local
验证
# 验证
$ iptables -L -n -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- 10.8.0.0/24 0.0.0.0/0
删除上述iptables配置信息使用如下命令,可以对比正常和异常访问
iptables -t nat -D POSTROUTING 1
1.2.3 系统时间与硬件时间同步
# 配置时间同步
$ crontab -l
*/10 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
# 系统使用上海时间
$ ll /etc/localtime
lrwxrwxrwx. 1 root root 35 Mar 7 2019 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
# 查看硬件时间
$ hwclock --show
Thu 27 May 2021 02:49:25 PM CST -0.109431 seconds
# 系统时间同步到硬件时间
hwclock --systohc
2.安装过程
2.1 安装依赖包
yum -y install lz4-devel lzo-devel pam-devel openssl-devel systemd-devel sqlite-devel autoconf automake libtool libtool-ltdl
2.2 编译安装openvpn
2.2.1 下载源码包
wget https://github.com/OpenVPN/openvpn/archive/v2.4.9.tar.gz
2.2.2 解压缩并进入源码目录
tar xf v2.4.9.tar.gz && cd openvpn-2.4.9
2.2.3 开始编译安装
nproc
命令可以直接获取系统核心数
# 生成configure文件
autoreconf -i -v -f
# 编译安装
./configure --prefix=/usr/local/openvpn --enable-lzo --enable-lz4 --enable-crypto --enable-server --enable-plugins --enable-port-share --enable-iproute2 --enable-pf --enable-plugin-auth-pam --enable-pam-dlopen --enable-systemd
make -j${nproc} && make install
2.2.4 做一下openvpn命令的软连接
ln -s /usr/local/openvpn/sbin/openvpn /usr/local/sbin/openvpn
2.2.5 查看openvpn版本
$ openvpn --version
OpenVPN 2.4.9 x86_64-unknown-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4] [EPOLL] [MH/PKTINFO] [AEAD] built on Jul 20 2021
library versions: OpenSSL 1.0.2k-fips 26 Jan 2017, LZO 2.06
Originally developed by James Yonan
Copyright (C) 2002-2018 OpenVPN Inc <sales@openvpn.net>
Compile time defines: enable_async_push=no enable_comp_stub=no enable_crypto=yes enable_crypto_ofb_cfb=yes enable_debug=yes enable_def_auth=yes enable_dlopen=unknown enable_dlopen_self=unknown enable_dlopen_self_static=unknown enable_fast_install=yes enable_fragment=yes enable_iproute2=yes enable_libtool_lock=yes enable_lz4=yes enable_lzo=yes enable_management=yes enable_multihome=yes enable_pam_dlopen=yes enable_pedantic=no enable_pf=yes enable_pkcs11=no enable_plugin_auth_pam=yes enable_plugin_down_root=yes enable_plugins=yes enable_port_share=yes enable_selinux=no enable_server=yes enable_shared=yes enable_shared_with_static_runtimes=no enable_small=no enable_static=yes enable_strict=no enable_strict_options=no enable_systemd=yes enable_werror=no enable_win32_dll=yes enable_x509_alt_username=no with_crypto_library=openssl with_gnu_ld=yes with_mem_check=no with_sysroot=no