0


Centos7、Ubuntu、Debian一键脚本安装Openvpn3-Client,可设置开机自启(全网原创)

声明:脚本为100%原创,参考并优化了官网教程,转载请署名来源

官网教程:https://openvpn.net/cloud-docs/owner/connectors/connector-user-guides/openvpn-3-client-for-linux.html

1.本地新建脚本文件

如本地没有vim,请先安装 Centos: yum install vim Ubuntu: apt install vim

vim openvpn3_client.sh

输入i进入编辑模式,直接复制以下代码黏贴进去,不用担心代码超长。

写入内容后按Esc退出编辑状态,再输入:wq保存退出。

#!/bin/bash
set -e

# 声明
echo -e "安装方法参考官网 https://openvpn.net/cloud-docs/owner/connectors/connector-user-guides/openvpn-3-client-for-linux.html"
echo -e "最后编辑时间 2023/12/28"

# 检查操作系统
echo -e "Check operating system version...\n"
echo -e "目前仅支持Centos 7  Ubuntu 18.04 20.04 22.04  Debian 10 11(未测试)"

MYOS=""
if [ -f /etc/os-release ]; then
    source /etc/os-release
    if [ "$ID" != "" ] && [ "${VERSION_ID%%.*}" != "" ]; then
        MYOS="$ID ${VERSION_ID%%.*}"
    fi
fi

# 输出os结果
if [ "$MYOS" == "" ]; then
    echo -e "您的操作系统: 无法识别\n"
else
    echo -e "您的操作系统: $MYOS\n"
fi

# 操作面板
while true; do
    echo ""
    echo "请选择操作:"
    echo "1. 安装Openvpn3 Client"
    echo "2. 卸载Openvpn3 Client"
    echo "3. 导入配置文件"
    echo "4. 删除配置文件"
    echo "5. 配置开机自启 [新增/删除]"
    echo "6. 离开"

    read -p "请输入选项数字: " choice

    case $choice in
        1)
            echo "############################################################"
            echo "Openvpn3 Client开始安装"
            echo "############################################################"
            # centos 7
            if ["$ID" == "centos" ]; then
                echo "yum update"
                yum update

                echo "sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm"
                sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

                echo "sudo yum install -y https://packages.openvpn.net/openvpn-openvpn3-epel-repo-1-1.noarch.rpm"
                sudo yum install -y https://packages.openvpn.net/openvpn-openvpn3-epel-repo-1-1.noarch.rpm

                echo "sudo yum install openvpn3-client"
                sudo yum install openvpn3-client

                echo "############################################################"
                echo "Openvpn3 Client安装完成"
                echo "############################################################"
            fi
            # ubuntu 18.04 20.04 22.04  Debian 10 11
            if [ "$ID" == "ubuntu" ] || [ "$ID" == "debian" ]; then
                echo "sudo mkdir -p /etc/apt/keyrings && curl -fsSL https://packages.openvpn.net/packages-repo.gpg | sudo tee /etc/apt/keyrings/openvpn.asc"
                sudo mkdir -p /etc/apt/keyrings && curl -fsSL https://packages.openvpn.net/packages-repo.gpg | sudo tee /etc/apt/keyrings/openvpn.asc

                echo "DISTRO=\$(lsb_release -c | awk '{print \$2}')"
                DISTRO=$(lsb_release -c | awk '{print $2}')

                echo 'echo "deb [signed-by=/etc/apt/keyrings/openvpn.asc] https://packages.openvpn.net/openvpn3/debian $DISTRO main" | sudo tee /etc/apt/sources.list.d/openvpn-packages.list'
                echo "deb [signed-by=/etc/apt/keyrings/openvpn.asc] https://packages.openvpn.net/openvpn3/debian $DISTRO main" | sudo tee /etc/apt/sources.list.d/openvpn-packages.list

                echo "sudo apt update"
                sudo apt update

                echo "sudo apt install openvpn3"
                sudo apt install openvpn3

                echo "############################################################"
                echo "Openvpn3 Client安装完成"
                echo "############################################################"
            fi
            ;;
        2)
            echo "############################################################"
            echo "Openvpn3 Client开始卸载"
            echo "############################################################"
            # centos 7
            if ["$ID" == "centos" ]; then
                echo "sudo yum remove openvpn3-client"
                sudo sudo yum remove openvpn3-client

                echo "sudo yum remove openvpn-openvpn3-epel-repo"
                sudo sudo yum remove openvpn-openvpn3-epel-repo

                echo "sudo yum remove epel-release"
                sudo sudo yum remove epel-release
            fi
            # ubuntu 18.04 20.04 22.04  Debian 10 11
            if [ "$ID" == "ubuntu" ] || [ "$ID" == "debian" ]; then
                echo "sudo apt uninstall openvpn3"
                sudo apt uninstall openvpn3
                echo "剩余部分不构成影响, 如有需要请手动删除或重置"
            fi
            echo "############################################################"
            echo "Openvpn3 Client卸载完成"
            echo "############################################################"
            ;;
        3)
            read -ep "请输入配置文件路径: " cpath
            # 检查文件是否存在
            if [ -f "$cpath" ]; then
                echo "文件存在!"
                read -p "请输入存储名称 [MainOvpn]: " cname
                cname=${cname:-MainOvpn}
                openvpn3 config-import --config "${cpath}" --name "${cname}" --persistent
                echo -e "导入完成!\n"
                openvpn3 configs-list
            else
                echo -e "文件不存在或不可访问!\n"
            fi
            ;;
        4)  
            openvpn3 configs-list
            read -p "请输入配置存储名称: " cname
            echo -e "tips: YES要大写\n"
            openvpn3 config-remove --config "${cname}"

            echo -e "删除完成!\n"
            openvpn3 configs-list
            ;;
        5)
            read -p "请输入服务名称 [openvpn3-client]: " sname
            sname=${sname:-openvpn3-client}
            # 检查文件是否存在
            if [ -f "/etc/systemd/system/${sname}.service" ]; then
                read -p "服务已存在!是否要移除该服务 [yes/No]: " remove_service
                if [ "$remove_service" == "yes" ] || [ "$remove_service" == "y" ]; then
                    # 处理移除服务的逻辑
                    echo -e "\n开始禁用服务 ${sname}"
                    systemctl stop "${sname}.service"
                    systemctl disable "${sname}.service"
                    echo -e "\n开始删除服务 ${sname}"
                    rm "/etc/systemd/system/${sname}.service"
                fi
            else
                read -p "请输入配置存储名称 [MainOvpn]: " cname
                cname=${cname:-MainOvpn}
                
                echo -e "\n开始创建 Systemd 服务单元文件"
                cat <<EOF | sudo tee "/etc/systemd/system/${sname}.service" >/dev/null
[Unit]
Description=OpenVPN 3 Auto Start
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/openvpn3 session-start --config "${cname}"

# 如果需要重试或自动重启,可以添加以下指令
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target
EOF
                echo -e "服务单元文件创建成功 /etc/systemd/system/${sname}.service"
                echo -e "您的服务名为 ${sname}.service"
                # 启用并启动自定义的 Systemd 服务
                echo -e "\n开始启用服务 ${sname}"
                echo -e "\n如Openvpn3 Client中设置了-互联网流量转发-, 软件启动后很可能导致ssh、远程连接断开!!!"
                read -p "并因为开机自启无法重置, 是否接受该风险 [yes/No]" append_risk
                if [ "$append_risk" == "yes" ] || [ "$append_risk" == "y" ]; then
                    sudo systemctl start "${sname}.service"
                    sudo systemctl enable "${sname}.service"
                fi
            fi
            ;;
        6)
            echo "退出"
            break  # 结束循环,退出脚本
            ;;
        *)
            echo "无效选项,请重新选择"
            ;;
    esac
done

2.添加执行权限

chmod +x openvpn3_client.sh

3.运行脚本

./openvpn3_client.sh

4.安装步骤

  • 先安装Openvpn3 Client客户端
  • 再导入Openvpn配置文件 (由Openvpn Server端生成,不懂的小伙伴网上搜Openvpn Server安装)
  • 最后设置开机自启,会自动在/etc/systemd/system中生成服务单元文件,随开机启动

5.脚本运行错误原因

Windows格式文件的换行符为\r\n ,而Unix&Linux文件的换行符为\n.dos2unix是将Windows格式文件转换为Unix、Linux格式的实用命令。 dos2unix命令其实就是将文件中的\r\n 转换为\n。

Centos安装指令:

$ sudo apt install dos2unix

Debian、Ubuntu安装指令:

$ sudo apt-get install dos2unix

使用dos2unix

$ dos2unix [options] <files>
标签: linux centos bash

本文转载自: https://blog.csdn.net/weixin_52096670/article/details/135277286
版权归原作者 Dawn0days 所有, 如有侵权,请联系我们删除。

“Centos7、Ubuntu、Debian一键脚本安装Openvpn3-Client,可设置开机自启(全网原创)”的评论:

还没有评论