Windows客户端安装
1) 下载安装OpenVPN客户端工具
创建客户端配置文件 ../OpenVPN/config/client.ovpn 内容如下
# cat client.ovpn client dev tun proto tcp remote 203.195.xxx.xxx 1194 # OpenVPN服务器的外网IP和端口 resolv-retry infinite nobind persist-key persist-tun ca ca.crt ;cert client1.crt # client1的证书 ;key client1.key # client1的密钥 ns-cert-type server tls-auth ta.key 1 # 客户端是1,服务器是0 comp-lzo verb 3 auth-user-pass # 启动User/Pass登录的方式
2) 拷贝client.ovpn文件到win本地openvpn客户端 ../OpenVPN/config/目录下
从服务器上拷贝证书/秘钥 “ca.crt、ta.key” 到本地openvpn客户端的config/目录下
然后用管理员身份打开OpenVPN客户端,点击连接。切记,一定要用管理员身份打开,否则客户端将无法执行服务端push推送过来的route路由添加指令
ok,如果一切顺利,就会看到 "Mon Apr 11 16:26:12 2016 MANAGEMENT: >STATE:1460363172,CONNECTED,SUCCESS,10.8.0.6,10.2.8.45",客户端分配到的ip,10.8.0.6
打开CMD,ipconfig /all 可以看到分配的虚拟ip
Linux 客户端安装
1) 下载客户端工具 # yum install -y openvpn 2) 创建客户端配置文件 client.conf # cat /etc/openvpn/conf/client.conf 添加如下内容 client dev tun proto tcp remote 203.195.xxx.xxx 1194 # OpenVPN服务器的外网IP和端口 resolv-retry infinite nobind persist-key persist-tun ca ca.crt ns-cert-type server tls-auth ta.key 1 # 客户端是1,服务器是0 comp-lzo verb 3 auth-user-pass # 启动User/Pass登录的方式 3) 启动客户端 # openvpn --daemon --config /etc/openvpn/conf/client.conf > /var/log/openvpn_client.log # 后台守护进程模式启动 Username: test # 输入用户名 Password: 123456 # 输入密码 # ps -ef | grep -v grep | grep openvpn # 查看启动进程
Linux客户端启动脚本 expect.sh
#!/usr/bin/expect # FileName: expect.sh # Auth: Loren # Date: 2016-04-26 10:00 # 定义账号密码 set user test set pass 123456 # 启动命令 spawn /usr/local/sbin/openvpn --daemon --config /etc/openvpn/config/client.conf > /var/log/openvpn_client.log set timeout 30 # 检测到指定内容 expect "Username:" # 输入账号 send "$user\n" set timeout 30 # 检测输入密码 expect "Password:" # 输入密码 send "$pass\n" set timeout 30 # 退出 expect eof
# expect expect.sh # 执行脚本
Linux客户端启动、停止、重启及状态脚本 openvpn_client.sh
#!/bin/bash # FileName vpn_client_status.sh # Auth: Loren # Date: 2016-04-26 10:00 # 客户端配置文件 CONF_FILE=‘/etc/openvpn/conf/client.conf‘ # log文件 LOG_FILE=‘/var/log/openvpn_client.log‘ # pid文件 PID_FILE=‘/tmp/openvpn_client.pid‘ # 启动脚本 EXPECT=‘/etc/openvpn/expect.sh‘ function start() { #/usr/local/sbin/openvpn --daemon --config $CONF_FILE > $LOG_FILE expect $EXPECT > /dev/null ps -ef | grep -v grep | grep openvpn > /dev/null [ $? -eq 0 ] && echo -e ‘Start OpenVPN \033[32msuccessfully\033[0m!‘ || echo -e ‘Start OpenVPN \033[31mFailing\033[0m!‘ echo `ps -ef | grep -v grep | grep openvpn | awk ‘{print $2}‘` > $PID_FILE } function stop() { ps -ef | grep -v grep | grep openvpn | awk ‘{print $2}‘ | xargs -I {} kill -9 {} > /dev/null ps -ef | grep -v grep | grep openvpn > /dev/null [ $? -eq 0 ] && echo -e ‘Stop OpenVPN is \033[31mFailing\033[0m!‘ || echo -e ‘Stop OpenVPN is \033[32mOK\033[0m!‘ rm -f $PID_FILE } function status() { ps -ef | grep -v grep | grep openvpn > /dev/null [ $? -eq 0 ] && echo -e ‘OpenVPN is \033[32mRunning ...\033[0m‘ || echo -e ‘OpenVPN is \033[31mStopped\033[0m!‘ } #### main case $1 in start) start ;; stop) stop ;; restart) stop start ;; status) status ;; *) echo "Error! Usage sh $0 (start|stop|restart|status)" ;; esac
sh openvpn_client start #启动vpn sh openvpn_client stop # 停止vpn sh openvpn_client restart # 重启vpn
时间: 2024-09-28 20:42:04