info
- OpenVPN
- Google Authenticator
- pam
install OpenVPN
yum install openvpn
install Google Authenticator
yum install -y gcc make autoconf automake libtool pam-devel gitgit clone https://github.com/google/google-authenticator-libpam.gitcd google-authenticator-libpam./bootstrap.sh && ./configure && make && make installcp -a /usr/local/lib/security/pam_google_authenticator.so /lib64/security/pam_google_authenticator.so
Google Authenticator for user(default user openvpn)
mkdir /etc/google-authgoogle-authenticator# set up as you wish, save image and/or codes.Entor to yes by defaultmv ~/.google_authenticator /etc/google-auth/openvpnchown -R openvpn /etc/google-auth
Add pam.conf
vim /etc/pam.d/openvpn
auth requisite /usr/local/lib/security/pam_google_authenticator.so secret=/etc/google-auth/${USER} user=openvpnaccount [success=2 new_authtok_reqd=done default=ignore] pam_unix.soaccount [success=1 new_authtok_reqd=done default=ignore] pam_winbind.soaccount requisite pam_deny.soaccount required pam_permit.so
config the openvpn
config firewalld
#disable firewalldsystemctl stop firewalldsystemctl mask firewalld#use iptablessystemctl enable iptablessystemctl start iptablesiptables -F #clear all ruleiptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADEiptables-save > /etc/sysconfig/iptables # iptables 规则持久化保存#enable ip forwardecho net.ipv4.ip_forward = 1 >> /etc/sysctl.confsysctl -p # 这一步一定得执行,否则不会立即生效。
config certificate
yum -y install easy-rsacp -r /usr/share/easy-rsa/ /etc/openvpn/cd /etc/openvpn/easy-rsa/<easy-rsa 版本号>/ # 查看 easy-rsa 版本号:yum info easy-rsavim vars # 没这个文件的话新建,填写如下内容(变量值根据实际情况随便填写):export KEY_COUNTRY="***"export KEY_PROVINCE="***"export KEY_CITY="***"export KEY_ORG="***"export KEY_EMAIL="***"source ./vars # 使变量生效./easyrsa init-pki #初始化 pki 相关目录./easyrsa build-ca nopass #生成 CA 根证书, 输入 Common Name,名字随便起。./easyrsa build-server-full server nopass./easyrsa gen-dh #创建Diffie-Hellman,这可能得等一小会儿openvpn --genkey --secret ta.key #creat tls keymkdir /etc/openvpn/server/certs && cd /etc/openvpn/server/certs/cp /etc/openvpn/easy-rsa/3/pki/dh.pem ./ # SSL 协商时 Diffie-Hellman 算法需要的 keycp /etc/openvpn/easy-rsa/3/pki/ca.crt ./ # CA 根证书cp /etc/openvpn/easy-rsa/3/pki/issued/server.crt ./ # open VPN 服务器证书cp /etc/openvpn/easy-rsa/3/pki/private/server.key ./ # open VPN 服务器证书 keycp /etc/openvpn/easy-rsa/3/ta.key ./ # tls-auth key
config server.conf
port 1194 # 监听的端口号proto udp # 服务端用的协议,udp 能快点,所以我选择 udpdev tunca /etc/openvpn/server/certs/ca.crt # CA 根证书路径cert /etc/openvpn/server/certs/server.crt # open VPN 服务器证书路径key /etc/openvpn/server/certs/server.key # open VPN 服务器密钥路径,This file should be kept secretdh /etc/openvpn/server/certs/dh.pem # Diffie-Hellman 算法密钥文件路径tls-auth /etc/openvpn/server/certs/ta.key 0 # tls-auth key,参数 0 可以省略,如果不省略,那么客户端# 配置相应的参数该配成 1。如果省略,那么客户端不需要 tls-auth 配置server 10.8.0.0 255.255.255.0 # 该网段为 open VPN 虚拟网卡网段,不要和内网网段冲突即可。open VPN 默认为 10.8.0.0/24push "dhcp-option DNS 8.8.8.8" # DNS 服务器配置,可以根据需要指定其他 nspush "dhcp-option DNS 8.8.4.4"push "redirect-gateway def1" # 客户端所有流量都通过 open VPN 转发,类似于代理开全局compress lzoduplicate-cn # 允许一个用户多个终端连接keepalive 10 120comp-lzopersist-keypersist-tunuser openvpn # open VPN 进程启动用户,openvpn 用户在安装完 openvpn 后就自动生成了group openvpnlog /var/log/openvpn/server.log # 指定 log 文件位置log-append /var/log/openvpn/server.logstatus /var/log/openvpn/status.logverb 3explicit-exit-notify 1
start & Test The status
systemctl start openvpn@servernetstat -nlp #ensure The udp port 1194 be opening
config Client
clientproto udpdev tunremote ***.***.***.*** 1194ca "C:\\path\\ca.crt"#cert "C:\\path\\test.crt"#key "C:\\path\\test.key"tls-auth "C:\\path\\ta.key" 1remote-cert-tls serverpersist-tunpersist-keycomp-lzoverb 3cipher AES-256-CBCremote-cert-tls servercomp-lzoauth-user-passauth-nocachereneg-sec 0
now just for fun
