本文章基于 https://blog.csdn.net/qq_42761569/article/details/132209725 博客文章进行编写,感谢原作者的创作付出!
Centos7安装OpenVpn客户端
1、安装openvpn
sudo yum install -y openvpn
sudo yum -y install epel-release
若出现 没有可用软件包 openvpn。请运行以下命令启用 EPEL 仓库
sudo yum install -y epel-release sudo yum clean all sudo yum makecache sudo yum install -y openvpn
- 最终默认安装在目录 /etc/openvpn
2、配置
上传.ovpn文件
把客户端的配置文件.ovpn上传到/etc/openvpn/client目录下
创建日志目录
cd /etc/openvpn/
mkdir logs
创建快捷操作脚本
创建脚本
vi vpn-client-operator.sh
脚本内容
#/bin/bash
:<<!
【脚本说明】
1、此脚本用于操作某一程序;
2、需要配置程序的启动命令;
3、支持服务启动、停止、重启、查看状态、查看日志、更新配置文件;
!
# 进程名称
operate=$1
# 配置程序
dir_home=$(cd $(dirname $0);pwd)
app="openvpn --daemon --cd $dir_home/client"
pid_1=`ps -ef | grep "$app" | grep -v grep | awk '{print $2}'`
conf_file=$(cd $dir_home/client;ls)
log_file=$dir_home/logs/openvpn.log
# 提示信息
msg='Please input params 【<run|kil|res|sta|log>】'
# 定制化shell输出
function custom_print(){
echo -e "\033[5;34m ***** \033[0m"
echo -e "\033[32m $@ ! \033[0m"
echo -e "\033[5;34m ***** \033[0m"
}
# 启动命令
function run(){
run_cmd="$app --config $conf_file --log-append $log_file"
$run_cmd
}
# 启动服务
if [[ $operate = "run" || $operate = "start" ]]; then
if [[ ! $pid_1 ]]; then
run
msg='Start success'
custom_print $msg
else
msg='The service is already running'
custom_print $msg
fi
# 停止服务
elif [[ $operate = "kil" || $operate = "stop" ]]; then
if [[ $pid_1 ]]; then
kill -9 $pid_1
msg='Stopped success'
custom_print $msg
else
# 服务早已停止或未启动
msg='The service is already down'
custom_print $msg
fi
# 重启服务
elif [[ $operate = "res" || $operate = "restart" ]]; then
if [[ $pid_1 ]]; then
kill -9 $pid_1
fi
run
msg='Restart success'
custom_print $msg
# 查看服务运行状态
elif [[ $operate = "sta" || $operate = "status" ]]; then
if [[ $pid_1 ]]; then
# 黄底蓝字
echo -e "\033[43;34m RUNNING \033[0m"
else
# 蓝底黑字
echo -e "\033[44;30m STOPPED \033[0m"
fi
# 查看服务运行日志
elif [[ $operate = "log" ]]; then
if [[ -e $log_file ]]; then
tail -f $log_file
else
msg="No logs have been generated so far"
custom_print $msg
fi
else
custom_print $msg
fi
执行脚本
bash vpn-client-operator.sh run
评论区