diff --git a/README.md b/README.md index d5f68b1..5775570 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,56 @@ # OpenVPNInstaller +A simple bash script to automate the installation and configuration of OpenVPN on Linux systems. + +## Description + +This script automates the process of installing OpenVPN and configuring it with your VPN configuration file (.ovpn). It handles the installation of OpenVPN, copying your configuration file to the correct location, and setting up the service to start automatically. + +## Requirements + +- Linux operating system +- Root/sudo privileges +- An existing .ovpn configuration file +- Internet connection for package installation + +## Installation + +1. Clone or download this repository +2. Make the script executable: +```bash +chmod +x install.sh +``` + +## Usage + +1. Run the script with sudo: +```bash +sudo ./install.sh +``` + +2. When prompted, enter the full path to your .ovpn configuration file + +The script will: +- Install OpenVPN if not already installed +- Copy your .ovpn file to `/etc/openvpn/client/` +- Start the OpenVPN service +- Enable the service to start automatically on boot + +## Notes + +- The script must be run with root privileges +- Make sure your .ovpn file is valid and contains all necessary configuration +- The VPN connection will be established automatically after installation + +## Troubleshooting + +If you encounter any issues: +1. Verify that your .ovpn file is valid +2. Check the OpenVPN service status: +```bash +sudo systemctl status openvpn-client@[your-config-name] +``` +3. Check the OpenVPN logs: +```bash +sudo journalctl -u openvpn-client@[your-config-name] +``` diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..68e7036 --- /dev/null +++ b/install.sh @@ -0,0 +1,106 @@ +#!/bin/bash + +# Exit on error +set -e + +# Function to print colored output +print_message() { + echo -e "\033[1;32m$1\033[0m" +} + +print_error() { + echo -e "\033[1;31m$1\033[0m" +} + +# Check if running on Linux +if [[ "$OSTYPE" != "linux-gnu"* ]]; then + print_error "This script is only compatible with Linux systems." + exit 1 +fi + +# Add a check to see if the user is root +if [ "$EUID" -ne 0 ]; then + print_error "Please run as root." + echo "sudo ./install.sh" + exit 1 +fi + +# Check if apt-get is available +if ! command -v apt-get &> /dev/null; then + print_error "This script requires apt-get package manager." + print_error "Please use a Debian-based distribution or modify the script for your package manager." + exit 1 +fi + +# Check if systemd is available +if ! command -v systemctl &> /dev/null; then + print_error "This script requires systemd." + exit 1 +fi + +print_message "Updating package lists..." +sudo apt-get update + +# Check if OpenVPN is already installed +if ! command -v openvpn &> /dev/null; then + print_message "Installing OpenVPN..." + sudo apt-get install -y openvpn +else + print_message "OpenVPN is already installed." +fi + +# Ask user for ovpn file location +read -p "Enter the path to your .ovpn file: " ovpn_path + +# Check if the file exists +if [ ! -f "$ovpn_path" ]; then + print_error "File does not exist: $ovpn_path" + exit 1 +fi + +# Check if the file is a valid .ovpn file +if [[ ! "$ovpn_path" =~ \.ovpn$ ]]; then + print_error "The file must have a .ovpn extension." + exit 1 +fi + +# Create the client directory if it doesn't exist +if [ ! -d "/etc/openvpn/client" ]; then + print_message "Creating OpenVPN client directory..." + sudo mkdir -p /etc/openvpn/client +fi + +# Get filename without path and change extension to .conf +filename=$(basename "$ovpn_path") +config_name="${filename%.ovpn}" +conf_filename="$config_name.conf" + +# Check if a configuration with the same name already exists +if [ -f "/etc/openvpn/client/$conf_filename" ]; then + read -p "A configuration with this name already exists. Overwrite? (y/n): " overwrite + if [[ ! "$overwrite" =~ ^[Yy]$ ]]; then + print_error "Installation aborted." + exit 1 + fi +fi + +print_message "Copying and converting configuration file..." +sudo cp "$ovpn_path" "/etc/openvpn/client/$conf_filename" + +# Set proper permissions +sudo chmod 600 "/etc/openvpn/client/$conf_filename" +sudo chown root:root "/etc/openvpn/client/$conf_filename" + +print_message "Starting OpenVPN service..." +if ! sudo systemctl start "openvpn-client@$config_name"; then + print_error "Failed to start OpenVPN service." + print_error "Please check your configuration file and try again." + exit 1 +fi + +print_message "Enabling OpenVPN service to start on boot..." +sudo systemctl enable "openvpn-client@$config_name" + +print_message "Installation completed successfully!" +print_message "Your OpenVPN connection should now be active." +print_message "You can check the status with: sudo systemctl status openvpn-client@$config_name" \ No newline at end of file