一武契、docker-compose 配置(先不要啟動)
version: "3"
services:
openvpn:
# network_mode: host # 設(shè)置為 host 模式網(wǎng)絡(luò)
container_name: openvpn
image: kylemanna/openvpn:latest
cap_add:
- NET_ADMIN
ports:
- "11194:1194/udp"
# environment:
# - TZ=Asia/Shanghai
# - SET_CONTAINER_TIMEZONE=true
# - CONTAINER_TIMEZONE=Asia/Shanghai
volumes:
- ./conf:/etc/openvpn
- /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
restart: always
二、生成配置文件
- 初始化將$OVPN_DATA包含配置文件和證書的容器荡含。容器將提示
輸入密碼
以保護(hù)新生成的證書頒發(fā)機(jī)構(gòu)使用的私鑰
OVPN_DATA="./conf"
docker volume create --name $OVPN_DATA
docker run -v $OVPN_DATA:/etc/openvpn --log-driver=none --rm kylemanna/openvpn ovpn_genconfig -u udp://VPN.SERVERNAME.COM
docker run -v $OVPN_DATA:/etc/openvpn --log-driver=none --rm -it kylemanna/openvpn ovpn_initpki
三咒唆、獲取客戶端配置文件
# 執(zhí)行命令的時候和 docker-compose.yml 文件在同目錄
export CLIENTNAME="your_client_name"
mkdir conf/client
docker-compose run --rm openvpn easyrsa build-client-full $CLIENTNAME nopass
docker-compose exec openvpn ovpn_getclient $CLIENTNAME > conf/client/$CLIENTNAME.ovpn
# 客戶端啟動命令
openvpn --daemon --config /etc/openvpn/your_client_name.conf --log-append /var/log/openvpn.log
# 服務(wù)端轉(zhuǎn)發(fā)配置及防火墻配置
/etc/sysctl.conf
net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -s 10.10.100.0/24 -o eth0 -j MASQUERADE
四、配置用戶名密碼認(rèn)證
- 獲取認(rèn)證shell腳本
wget http://openvpn.se/files/other/checkpsw.sh
chmod +x checkpsw.sh
- 修改 server 端配置文件释液,添加內(nèi)容
### client-cert-not-required # 是否配置客戶端TLS認(rèn)證
auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env
script-security 3 # 2.4版本 需要配置為3全释,不然會認(rèn)證失敗
username-as-common-name
- 創(chuàng)建用戶名密碼
echo "jeff 123456" >psw-file
- 客戶端添加以下內(nèi)容
auth-user-pass
五、固定 vpn 客戶端獲取的地址
- 修改 server 端配置文件误债,添加下面一行
client-config-dir ccd
- 配置 IP 地址
在 ccd 目錄下創(chuàng)建一個 $CLIENTNAME 文件浸船,內(nèi)容如下
ifconfig-push 172.16.1.100 255.255.0.0
六、配置LDAP登錄
- 修改server 端配置文件
# 刪除
auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env
script-security 3
# 添加
plugin /usr/lib/openvpn/openvpn-auth-ldap.so "/etc/openvpn/ldap.conf
# ldap.conf 內(nèi)容
<LDAP>
URL ldap://127.0.0.1:1389
BindDN cn=readonly,dc=lcs,dc=com
Password LYmo1BrpttFE
Timeout 15
TLSEnable no
FollowReferrals no
</LDAP>
<Authorization>
BaseDN "ou=Users,dc=lcs,dc=com"
SearchFilter "(&(cn=%u)(memberof=cn=develop,ou=Groups,dc=lcs,dc=com))"
RequireGroup false
<Group>
BaseDN "ou=Groups,dc=lcs,dc=com"
SearchFilter "cn=develop"
MemberAttribute memberUid
</Group>
</Authorization>
七寝蹈、配置LDAP+MFA登錄
sudo apt install openvpn-auth-ldap libnss-ldapd libpam-google-authenticator
參考文檔:https://blog.thinkbox.dev/posts/0001-openvpn-ldap-auth/
八李命、配置文件說明
####################### server #############################
server 10.10.100.0 255.255.255.0
verb 3
key /etc/openvpn/pki/private/sh-vpn.amaxtest.com.key
ca /etc/openvpn/pki/ca.crt
cert /etc/openvpn/pki/issued/sh-vpn.amaxtest.com.crt
dh /etc/openvpn/pki/dh.pem
tls-auth /etc/openvpn/pki/ta.key
key-direction 0
keepalive 10 60
persist-key
persist-tun
proto udp
# Rely on Docker to do port mapping, internally always 1194
port 11194
dev tun0
status /tmp/openvpn-status.log
user nobody
group nogroup
comp-lzo no
### Route Configurations Below
# 服務(wù)端以下網(wǎng)絡(luò)不走vpn網(wǎng)絡(luò)
route 192.168.4.0 255.255.252.0 net_gateway
### Push Configurations Below
# push "block-outside-dns"
push "comp-lzo no"
####################### client #############################
client
nobind
dev tun
remote-cert-tls server
remote xxxx 1194 udp
<key>
</cert>
<ca>
</ca>
key-direction 1
<tls-auth>
</tls-auth>
# 不添加 vpn 路由
route-nopull
# 客戶端添加以下路由走vpn網(wǎng)絡(luò)
route 10.10.100.0 255.255.255.0 vpn_gateway
route 192.168.4.0 255.255.252.0 vpn_gateway