Write a script that rootlessly creates a temporary wireguard container using a .conf…
- User
Write a script that rootlessly creates a temporary wireguard container using a .conf then runs the user supplied command-line inside.
- Assistant
Here's a script that creates a rootless WireGuard container using Podman (a rootless container engine) to run a user-supplied command through a WireGuard VPN connection. The script uses a provided WireGuard configuration file (.conf) and ensures proper cleanup of resources.
#!/bin/bash # Script to create a rootless WireGuard container and run a command inside it # Exit on any error set -e # Function to display usage usage() { echo "Usage: $0 <wireguard-config.conf> <command> [args...]" echo "Example: $0 /path/to/wg.conf curl https://ifconfig.me" exit 1 } # Check if Podman is installed if ! command -v podman &> /dev/null; then echo "Error: Podman is not installed. Please install Podman to use this script." exit 1 fi # Check if at least two arguments are provided if [ $# -lt 2 ]; then usage fi # WireGuard config file WG_CONF="$1" shift # Check if the config file exists if [ ! -f "$WG_CONF" ]; then echo "Error: WireGuard config file '$WG_CONF' not found." exit 1 fi # Command to run inside the container USER_COMMAND="$@" # Temporary container name CONTAINER_NAME="wg-temp-$(date +%s)" # Function to clean up the container cleanup() { echo "Cleaning up container..." podman rm -f "$CONTAINER_NAME" > /dev/null 2>&1 } # Trap script exit to ensure cleanup trap cleanup EXIT echo "Starting WireGuard container..." # Create and start the container podman run --rm -it \ --name "$CONTAINER_NAME" \ --cap-add=NET_ADMIN \ --cap-add=NET_RAW \ --volume "$WG_CONF:/etc/wireguard/wg0.conf:ro" \ --sysctl net.ipv4.ip_forward=1 \ --sysctl net.ipv6.conf.all.forwarding=1 \ docker.io/alpine:latest \ /bin/sh -c " # Install WireGuard tools apk add --no-cache wireguard-tools bash iproute2 && # Start WireGuard wg-quick up /etc/wireguard/wg0.conf && # Execute the user command exec $USER_COMMAND " # The trap will handle cleanup automaticallyExplanation of the Script
-
Shebang and Error Handling:
- The script starts with
#!/bin/bashto indicate it's a Bash script. set -eensures the script exits immediately if any command fails.
- The script starts with
-
Usage Function:
- The
usagefunction displays help text if the script is not used correctly.
- The
-
Dependencies Check:
- The script checks if Podman is installed, as it is required for rootless container management.
-
Argument Handling:
- The first argument (
$1) is the path to the WireGuard configuration file (.conf). - The remaining arguments (
"$@") are the command and its arguments to run inside the container.
- The first argument (
-
Container Setup:
- A unique container name is generated using a timestamp to avoid conflicts.
- The container is created using the
alpineimage for minimal size. - Necessary capabilities (
NET_ADMIN,NET_RAW) are added to allow WireGuard to manage network interfaces. - The WireGuard configuration file is mounted into the container at
/etc/wireguard/wg0.conf. - Sysctl settings are applied to enable IP forwarding (required for VPN functionality).
-
Container Execution:
- Inside the container, the script installs WireGuard tools (
wireguard-tools), starts the VPN usingwg-quick, and then executes the user-supplied command. - The
execensures the user command replaces the shell process, allowing proper signal handling.
- Inside the container, the script installs WireGuard tools (
-
Cleanup:
- A
cleanupfunction is defined to remove the container when the script exits. - The
trapcommand ensures the cleanup function is called on script exit, whether successful or due to an error.
- A
Prerequisites
- Podman: Install Podman on your system. On most Linux distributions, you can install it with your package manager (e.g.,
sudo apt install podmanon Debian/Ubuntu orsudo dnf install podmanon Fedora). - WireGuard Configuration: You need a valid WireGuard
.conffile, typically provided by your VPN provider or self-hosted WireGuard server.
Usage
- Save the script as
wg-container.sh. - Make it executable:
chmod +x wg-container.sh - Run the script with a WireGuard configuration file and a command:
This example will start a WireGuard VPN connection and run./wg-container.sh /path/to/wg.conf curl https://ifconfig.mecurl https://ifconfig.meto display your public IP address through the VPN.
Notes
- Rootless Operation: This script uses Podman, which runs containers without root privileges. Ensure your user has the necessary permissions to run Podman (e.g., subuid/subgid mappings are set up).
- Security: Be cautious with the WireGuard configuration file, as it contains sensitive information (e.g., private keys).
- Error Handling: The script exits on errors and ensures cleanup, but you may want to add additional error handling depending on your use case.
- Customization: You can modify the base image (e.g., use
ubuntuinstead ofalpine) or add additional tools inside the container by modifying theapk addcommand.
This script provides a convenient way to run commands through a WireGuard VPN without requiring root privileges or persistent container state.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.