Skip to content

Instantly share code, notes, and snippets.

@MohamedElashri
Last active November 15, 2024 21:53
Show Gist options
  • Save MohamedElashri/b452c12e7ce908459447b73ddd99f5b6 to your computer and use it in GitHub Desktop.
Save MohamedElashri/b452c12e7ce908459447b73ddd99f5b6 to your computer and use it in GitHub Desktop.
install open snitch on debiad based linux distributions.

OpenSnitch Manager Script

A bash script to manage OpenSnitch installation, uninstallation, and repair on Linux systems. This script provides automated installation of both the OpenSnitch service and UI components, with proper desktop integration and autostart configuration.

Supported Distributions

  • Debian-based systems (Debian, Ubuntu, Linux Mint, Pop!_OS)
  • RHEL-based systems (CentOS, Fedora)
  • Arch Linux

Installation

  1. Download the script:

  2. Make it executable:

chmod +x opensnitch.sh

Usage

The script supports three main operations:

Install OpenSnitch

sudo ./opensnitch.sh --install

Uninstall OpenSnitch

sudo ./opensnitch.sh --uninstall

Repair Partial Installation

sudo ./opensnitch.sh --repair

Troubleshooting

If you encounter any issues:

  1. Ensure you have root privileges
  2. Check your internet connection
  3. Verify your system's package manager is supported
  4. Use the --repair option if you have a partial installation
#!/bin/bash
# Color definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARNING]${NC} $1" >&2
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
# Helper functions
command_exists() {
command -v "$1" >/dev/null 2>&1
}
ensure_wget() {
if ! command_exists wget; then
log_info "Installing wget..."
apt update && apt install -y wget || {
log_error "Failed to install wget"
exit 1
}
fi
}
ensure_curl() {
if ! command_exists curl; then
log_info "Installing curl..."
apt update && apt install -y curl || {
log_error "Failed to install curl"
exit 1
}
fi
}
# Check if script is run as root
check_root() {
if [ "$EUID" -ne 0 ]; then
log_error "This script must be run as root"
exit 1
fi
}
# Detect package manager
detect_package_manager() {
if command -v apt &> /dev/null; then
echo "apt"
elif command -v dnf &> /dev/null; then
echo "dnf"
elif command -v yum &> /dev/null; then
echo "yum"
elif command -v pacman &> /dev/null; then
echo "pacman"
else
echo "unknown"
fi
}
# Check installation status of OpenSnitch components
check_installation_status() {
local service_installed=false
local ui_installed=false
local status=""
# Check service
if systemctl list-unit-files | grep -q opensnitch || systemctl list-unit-files | grep -q opensnitchd; then
service_installed=true
fi
# Check UI
if command -v opensnitch-ui &> /dev/null; then
ui_installed=true
fi
# Return status code based on installation state
if $service_installed && $ui_installed; then
echo "both"
elif $service_installed; then
echo "service_only"
elif $ui_installed; then
echo "ui_only"
else
echo "none"
fi
}
# Setup desktop integration
setup_desktop_integration() {
log_info "Setting up desktop integration..."
# Create desktop entry
cat > /usr/share/applications/opensnitch-ui.desktop << EOF
[Desktop Entry]
Version=1.0
Type=Application
Name=OpenSnitch
Comment=Application firewall
Exec=opensnitch-ui
Icon=opensnitch-ui
Categories=System;Security;Monitor;Network;
Keywords=firewall;security;network;
StartupNotify=true
Terminal=false
EOF
# Create autostart entry
mkdir -p /etc/xdg/autostart
cp /usr/share/applications/opensnitch-ui.desktop /etc/xdg/autostart/
# Ensure proper permissions
chmod 644 /usr/share/applications/opensnitch-ui.desktop
chmod 644 /etc/xdg/autostart/opensnitch-ui.desktop
log_info "Desktop integration completed"
}
# Install OpenSnitch
install_opensnitch() {
log_info "Starting OpenSnitch installation..."
# Ensure required tools are installed
ensure_wget
ensure_curl
local pkg_manager=$(detect_package_manager)
case $pkg_manager in
"apt")
log_info "Detected Debian-based system"
# Create temporary directory
local temp_dir=$(mktemp -d)
cd "$temp_dir" || {
log_error "Failed to create temporary directory"
exit 1
}
# Determine system architecture
local arch=$(dpkg --print-architecture)
log_info "Detected architecture: $arch"
# Get latest version number
log_info "Detecting latest version..."
latest_version=$(curl -s https://api.github.com/repos/evilsocket/opensnitch/releases/latest | grep -Po '"tag_name": "\K.*?(?=")' || echo "v1.6.6")
if [ -z "$latest_version" ]; then
latest_version="v1.6.6" # Fallback version if API call fails
fi
log_info "Latest version: $latest_version"
# Remove 'v' prefix from version for package name
version_number=${latest_version#v}
# Download packages
log_info "Downloading OpenSnitch packages..."
wget "https://github.com/evilsocket/opensnitch/releases/download/${latest_version}/opensnitch_${version_number}-1_${arch}.deb" || {
log_error "Failed to download OpenSnitch package"
rm -rf "$temp_dir"
exit 1
}
wget "https://github.com/evilsocket/opensnitch/releases/download/${latest_version}/python3-opensnitch-ui_${version_number}-1_all.deb" || {
log_error "Failed to download OpenSnitch UI package"
rm -rf "$temp_dir"
exit 1
}
# Install dependencies
log_info "Installing dependencies..."
apt update
apt install -y python3-pip python3-dev python3-wheel python3-slugify || {
log_error "Failed to install dependencies"
rm -rf "$temp_dir"
exit 1
}
# Install the packages
log_info "Installing OpenSnitch packages..."
apt install -y ./*.deb || {
log_error "Failed to install OpenSnitch packages"
rm -rf "$temp_dir"
exit 1
}
# Cleanup
cd - > /dev/null
rm -rf "$temp_dir"
;;
"dnf")
log_info "Detected Fedora-based system"
dnf install -y opensnitch opensnitch-ui || {
log_error "Failed to install OpenSnitch"
exit 1
}
;;
"yum")
log_info "Detected RHEL-based system"
yum install -y opensnitch opensnitch-ui || {
log_error "Failed to install OpenSnitch"
exit 1
}
;;
"pacman")
log_info "Detected Arch-based system"
pacman -S --noconfirm opensnitch || {
log_error "Failed to install OpenSnitch"
exit 1
}
;;
*)
log_error "Unsupported package manager"
exit 1
;;
esac
# Enable and start the service
log_info "Enabling and starting OpenSnitch service..."
systemctl enable --now opensnitch || {
log_warn "Failed to enable opensnitch service, trying opensnitchd..."
systemctl enable --now opensnitchd || {
log_error "Failed to enable both opensnitch and opensnitchd services"
exit 1
}
}
# Start the service
systemctl start opensnitch || systemctl start opensnitchd || {
log_error "Failed to start OpenSnitch service"
exit 1
}
# Setup desktop integration
setup_desktop_integration
log_info "OpenSnitch installation completed successfully"
log_info "You can now launch the UI using: opensnitch-ui"
log_info "The UI will also start automatically on next login"
}
# Repair partial installation
repair_installation() {
local status=$(check_installation_status)
local pkg_manager=$(detect_package_manager)
case $status in
"both")
log_info "Both service and UI are properly installed"
return 0
;;
"service_only")
log_info "UI is missing. Installing UI component..."
case $pkg_manager in
"apt")
# Create temporary directory for UI package
local temp_dir=$(mktemp -d)
cd "$temp_dir" || {
log_error "Failed to create temporary directory"
exit 1
}
# Get latest version
latest_version=$(curl -s https://api.github.com/repos/evilsocket/opensnitch/releases/latest | grep -Po '"tag_name": "\K.*?(?=")' || echo "v1.6.6")
if [ -z "$latest_version" ]; then
latest_version="v1.6.6"
fi
version_number=${latest_version#v}
# Download and install UI package
wget "https://github.com/evilsocket/opensnitch/releases/download/${latest_version}/python3-opensnitch-ui_${version_number}-1_all.deb" || {
log_error "Failed to download OpenSnitch UI package"
rm -rf "$temp_dir"
exit 1
}
apt install -y ./python3-opensnitch-ui*.deb || {
log_error "Failed to install OpenSnitch UI"
rm -rf "$temp_dir"
exit 1
}
# Cleanup
cd - > /dev/null
rm -rf "$temp_dir"
;;
"dnf")
dnf install -y opensnitch-ui || {
log_error "Failed to install OpenSnitch UI"
exit 1
}
;;
"yum")
yum install -y opensnitch-ui || {
log_error "Failed to install OpenSnitch UI"
exit 1
}
;;
"pacman")
log_error "Partial installation not supported on Arch Linux"
exit 1
;;
esac
setup_desktop_integration
log_info "UI installation completed"
;;
"ui_only")
log_info "Service is missing. Installing service component..."
case $pkg_manager in
"apt")
# Create temporary directory for service package
local temp_dir=$(mktemp -d)
cd "$temp_dir" || {
log_error "Failed to create temporary directory"
exit 1
}
# Get latest version and architecture
latest_version=$(curl -s https://api.github.com/repos/evilsocket/opensnitch/releases/latest | grep -Po '"tag_name": "\K.*?(?=")' || echo "v1.6.6")
if [ -z "$latest_version" ]; then
latest_version="v1.6.6"
fi
version_number=${latest_version#v}
local arch=$(dpkg --print-architecture)
# Download and install service package
wget "https://github.com/evilsocket/opensnitch/releases/download/${latest_version}/opensnitch_${version_number}-1_${arch}.deb" || {
log_error "Failed to download OpenSnitch service package"
rm -rf "$temp_dir"
exit 1
}
apt install -y ./opensnitch*.deb || {
log_error "Failed to install OpenSnitch service"
rm -rf "$temp_dir"
exit 1
}
# Cleanup
cd - > /dev/null
rm -rf "$temp_dir"
;;
"dnf")
dnf install -y opensnitch || {
log_error "Failed to install OpenSnitch service"
exit 1
}
;;
"yum")
yum install -y opensnitch || {
log_error "Failed to install OpenSnitch service"
exit 1
}
;;
"pacman")
log_error "Partial installation not supported on Arch Linux"
exit 1
;;
esac
# Enable and start the service
systemctl enable --now opensnitch || {
log_warn "Failed to enable opensnitch service, trying opensnitchd..."
systemctl enable --now opensnitchd || {
log_error "Failed to enable both opensnitch and opensnitchd services"
exit 1
}
}
# Start the service
systemctl start opensnitch || systemctl start opensnitchd || {
log_error "Failed to start OpenSnitch service"
exit 1
}
log_info "Service installation completed"
;;
"none")
log_info "No components installed. Running full installation..."
install_opensnitch
;;
esac
}
# Uninstall OpenSnitch
uninstall_opensnitch() {
log_info "Starting OpenSnitch uninstallation..."
# Stop and disable the service first
log_info "Stopping and disabling OpenSnitch service..."
systemctl stop opensnitch 2>/dev/null || systemctl stop opensnitchd 2>/dev/null
systemctl disable opensnitch 2>/dev/null || systemctl disable opensnitchd 2>/dev/null
local pkg_manager=$(detect_package_manager)
case $pkg_manager in
"apt")
log_info "Removing OpenSnitch packages..."
apt remove --purge -y opensnitch python3-opensnitch-ui || {
log_error "Failed to remove OpenSnitch"
exit 1
}
apt autoremove -y
;;
"dnf")
dnf remove -y opensnitch opensnitch-ui || {
log_error "Failed to remove OpenSnitch"
exit 1
}
;;
"yum")
yum remove -y opensnitch opensnitch-ui || {
log_error "Failed to remove OpenSnitch"
exit 1
}
;;
"pacman")
pacman -R --noconfirm opensnitch || {
log_error "Failed to remove OpenSnitch"
exit 1
}
;;
*)
log_error "Unsupported package manager"
exit 1
;;
esac
# Clean up configuration files
log_info "Cleaning up configuration files..."
rm -rf /etc/opensnitchd 2>/dev/null
rm -rf ~/.config/opensnitch 2>/dev/null
# Clean up desktop integration files
log_info "Cleaning up desktop integration..."
rm -f /usr/share/applications/opensnitch-ui.desktop
rm -f /etc/xdg/autostart/opensnitch-ui.desktop
log_info "OpenSnitch uninstallation completed successfully"
}
# Show usage
show_usage() {
echo "Usage: $0 [--install|--uninstall|--repair]"
echo "Options:"
echo " --install Install OpenSnitch"
echo " --uninstall Uninstall OpenSnitch"
echo " --repair Check and repair partial installation"
}
# Main script logic
main() {
# Check if any argument is provided
if [ $# -eq 0 ]; then
show_usage
exit 1
fi
# Check root permissions
check_root
case "$1" in
--install)
local status=$(check_installation_status)
if [ "$status" = "both" ]; then
log_warn "OpenSnitch is already fully installed"
exit 0
elif [ "$status" != "none" ]; then
log_warn "Partial installation detected. Use --repair to fix"
exit 1
fi
install_opensnitch
;;
--uninstall)
local status=$(check_installation_status)
if [ "$status" = "none" ]; then
log_warn "OpenSnitch is not installed"
exit 0
fi
uninstall_opensnitch
;;
--repair)
repair_installation
;;
*)
show_usage
exit 1
;;
esac
}
# Execute main function with all passed arguments
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment