环境准备
关闭selinux
- 临时生效
[root@hxj ~]# setenforce 0
setenforce: SELinux is disabled
- 永久生效
sed -i '/^SELINUX=/c\SELINUX=disabled' /etc/selinux/config
安装epel源
rpm -ivh http://mirrors.sohu.com/fedora-epel/7/x86_64/e/epel-release-7-9.noarch.rpm
安装openvpn
yum安装openvpn
yum -y install openssl openssl-devel lzo openvpn easy-rsa vim
生成密钥
cd /usr/share/easy-rsa/2.0/
修改目录下vars文件
export KEY_COUNTRY="CN"
export KEY_PROVINCE="HuBei"
export KEY_CITY="WuHan"
export KEY_ORG="hxjagf"
export KEY_EMAIL="hxjagf@qq.com"
export KEY_OU="hxjagf"
创建密钥文件,一路回车,但遇到yes or no 的时候要输入Y
source ./vars
./clean-all
./build-ca
./build-key-server cty_vpnserver
./build-dh
./build-key cloud
将生成的密钥文件复制到openvpn的安装目录
cp -rf keys/ /etc/openvpn/
创建vpn配置文件
vim /etc/openvpn/server.conf
;local a.b.c.d
port 1194
proto udp
dev tun
ca keys/ca.crt
cert keys/cty_vpnserver.crt
key keys/cty_vpnserver.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.99.0 255.255.255.0"
push "route 192.168.98.0 255.255.255.0"
client-to-client
duplicate-cn
keepalive 10 120
comp-lzo
persist-key
persist-tun
status openvpn-status.log
log-append openvpn.log
verb 3
cipher AES-256-CBC
防火墙配置
由于centos7用firewalld服务替代了iptables服务,所有先要安装iptables
yum install -y iptables-services
systemctl enable iptables
systemctl stop firewalld
systemctl start iptables
iptables -F
iptables -L
开启转发
sed -i '/net.ipv4.ip_forward/s/0/1/' /etc/sysctl.conf
sysctl -p
或者
vim /proc/sys/net/ipv4/ip_forward # 添加 net.ipv4.ip_forward = 1
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o ens32 -j MASQUERADE #开启nat转发
iptables -I INPUT -p tcp --dport 1194 -m comment --comment "openvpn" -j ACCEPT #针对tcp端口
iptables -I INPUT -p udp --dport 1194 -m comment --comment "openvpn" -j ACCEPT #针对udp端口
service iptables save #保存iptables配置
启动服务
systemctl start openvpn@server.service #启动服务
systemctl status openvpn@server.service #查看服务状态
systemctl enable openvpn@server.service #开机自启
配置用户密码认证
修改server.conf配置文件,添加下面代码
script-security 3
auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env
# client-cert-not-required #启用后,就关闭证书认证,只通过账号密码认证
username-as-common-name
创建认证脚本,放到/etc/openvpn/ 目录
[root@localhost openvpn]# cat checkpsw.sh
#!/bin/sh
###########################################################
# checkpsw.sh (C) 2004 Mathias Sundman <mathias@openvpn.se>
#
# This script will authenticate OpenVPN users against
# a plain text file. The passfile should simply contain
# one row per user with the username first followed by
# one or more space(s) or tab(s) and then the password.
PASSFILE="/etc/openvpn/psw-file"
LOG_FILE="/var/log/openvpn-password.log"
TIME_STAMP=`date "+%Y-%m-%d %T"`
###########################################################
if [ ! -r "${PASSFILE}" ]; then
echo "${TIME_STAMP}: Could not open password file \"${PASSFILE}\" for reading." >> ${LOG_FILE}
exit 1
fi
CORRECT_PASSWORD=`awk '!/^;/&&!/^#/&&$1=="'${username}'"{print $2;exit}' ${PASSFILE}`
if [ "${CORRECT_PASSWORD}" = "" ]; then
echo "${TIME_STAMP}: User does not exist: username=\"${username}\", password=\"${password}\"." >> ${LOG_FILE}
exit 1
fi
if [ "${password}" = "${CORRECT_PASSWORD}" ]; then
echo "${TIME_STAMP}: Successful authentication: username=\"${username}\"." >> ${LOG_FILE}
exit 0
fi
echo "${TIME_STAMP}: Incorrect password: username=\"${username}\", password=\"${password}\"." >> ${LOG_FILE}
exit 1
创建账号密码文件
[root@localhost openvpn]# cat psw-file
test 123456
添加权限控制
给账号分配固定IP
- 配置文件中添加
client-config-dir /etc/openvpn/ccd
- 给每个账号创建配置文件
echo "ifconfig-push 10.8.0.17 10.8.0.18" > ccd/zhangsan
效果如下
[root@localhost openvpn]# ls ccd/
hxj lzh test zhangsan
注意ip只能配套对应下表的地址集
[ 1, 2] [ 5, 6] [ 9, 10] [ 13, 14] [ 17, 18]
[ 21, 22] [ 25, 26] [ 29, 30] [ 33, 34] [ 37, 38]
[ 41, 42] [ 45, 46] [ 49, 50] [ 53, 54] [ 57, 58]
[ 61, 62] [ 65, 66] [ 69, 70] [ 73, 74] [ 77, 78]
[ 81, 82] [ 85, 86] [ 89, 90] [ 93, 94] [ 97, 98]
[101,102] [105,106] [109,110] [113,114] [117,118]
[121,122] [125,126] [129,130] [133,134] [137,138]
[141,142] [145,146] [149,150] [153,154] [157,158]
[161,162] [165,166] [169,170] [173,174] [177,178]
[181,182] [185,186] [189,190] [193,194] [197,198]
[201,202] [205,206] [209,210] [213,214] [217,218]
[221,222] [225,226] [229,230] [233,234] [237,238]
[241,242] [245,246] [249,250] [253,254]
添加iptables规则
以下为两个例子,基本涵盖所有需求,根据具体情况做修改
- 允许10.8.0.13访问192.168.99.101服务器,其他的全部拒绝
- 允许10.8.0.17访问192.168.98.0/24的所有ip,其他的全部拒绝
iptables -A FORWARD -i tun0 -s 10.8.0.13/32 -d 192.168.99.101 -j ACCEPT
iptables -A FORWARD -s 10.8.0.13/32 -j DROP
iptables -A FORWARD -i tun0 -s 10.8.0.17/32 -d 192.168.98.0/24 -j ACCEPT
iptables -A FORWARD -s 10.8.0.17/32 -j DROP
service iptables save
最后附上windows客户端配置文件
client
dev tun
proto udp
remote 27.18.17.241 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert cloud.crt
key cloud.key
comp-lzo
verb 3
auth-user-pass cloudpw.txt