Hold Tight...

0 %
Warith AL Maawali
Driving cybersecurity excellence
Innovator behind Linux Kodachi
  • Residence:
    ::0
  • Uptime Binary:
    101110
  • Mantra:
    Innovate, Secure, Repeat
ONS
EEDS
NSSG
Visual basic
Gambas
PHP
Delphi
Bash
  • Cybersecurity
  • Generative AI
  • Cloud & Automation
  • Cryptocurrency

Effortlessly Set Up Ntopng: Monitor Your Network in Minutes

03/11/2024

Automate Ntopng Installation with This Bash Script

Setting up Ntopng, a high-performance network traffic monitoring tool, on a Debian-based system can be intricate. This Bash script simplifies the process, automating installation and configuration to ensure a seamless setup experience.

What is Ntopng?

Ntopng is an advanced network traffic analyzer that provides real-time insights into your network’s performance and usage. It monitors bandwidth, detects anomalies, and enhances network security, making it an invaluable tool for administrators.

Key Features of the Script

  • Root Privileges Check: Ensures the script is executed with the necessary permissions.
  • Network Interface Detection: Automatically identifies the primary network interface for monitoring.
  • Repository Configuration: Adds the official Ntopng repository and imports the GPG key for package verification.
  • Dependency Installation: Installs Ntopng along with essential tools like pfring-dkms, nprobe, n2disk, and cento.
  • Configuration Setup: Configures Ntopng with detected network interfaces and custom settings.
  • Service Management: Enables and starts the Ntopng service, ensuring it’s active and accessible.
  • Error Handling: Includes robust checks to handle issues during installation.

How It Works

  1. Detects Network Interface:
  • Identifies the primary network interface to monitor.
  • Falls back to common private network ranges if detection fails.
  1. Configures the Repository:
  • Adds the official Ntopng repository.
  • Updates the package list and verifies repository keys.
  1. Installs Ntopng and Dependencies:
  • Downloads and installs necessary packages, including nprobe and pfring-dkms.
  • Cleans up any previous installations for a fresh start.
  1. Configures Ntopng Settings:
  • Sets the network interface and local networks for monitoring.
  • Configures the service to listen on a non-standard port for security.
  1. Manages the Service:
  • Enables Ntopng to start at boot.
  • Restarts the service to apply configurations and ensures it is active.
  1. Provides Access Information:
  • Displays the web interface URL and default credentials for easy login.

Quick Start

  1. Download and Make Executable:
   chmod +x ntopng-setup.sh
  1. Run the Script:
   sudo ./ntopng-setup.sh
  1. Follow Prompts:
  • Let the script detect your network settings.
  • Watch as it installs and configures Ntopng automatically.
  1. Access Ntopng:
  • Open your browser and navigate to http://<your-server-ip>:4400.
  • Log in with the default credentials (admin/admin) and change the password immediately.

Benefits

  • Time-Saving: Automates a complex installation and configuration process.
  • Enhanced Security: Runs the web interface on a non-standard port with customizable settings.
  • Reliable: Includes error handling to ensure a smooth installation experience.
  • Customizable: Allows adjustments to the network settings and monitored interfaces.

Conclusion

This Bash script takes the hassle out of setting up Ntopng on Debian-based systems. By automating the process, it ensures a quick and reliable installation, letting you focus on monitoring your network. Start gaining real-time insights into your network performance today with this powerful tool!

ShellScript
#!/bin/bash
# =========================================
# Ntopng Setup Script
# =========================================
#
# Version: 1.0.1
# Author: Warith AL Maawali
#
# Contact:
# - Discord: https://discord.gg/KEFErEx
# - Twitter: http://twitter.com/warith2020
# - LinkedIn: http://www.linkedin.com/in/warith1977
# - Website: https://www.digi77.com
#
# Copyright (c) 2024 Warith AL Maawali
#
# Description:
# This script automates the installation and configuration of Ntopng on Debian-based systems.
# It performs the following tasks:
# - Ensures root privileges
# - Detects the primary network interface
# - Updates system packages
# - Configures the Ntopng repository
# - Installs Ntopng and its dependencies
#
# License:
# Dual-licensed under:
# 1. Apache License 2.0 (personal, non-commercial use)
# 2. Commercial license (for corporate/organizational use)
# Contact author for commercial licensing details.
#
# Usage:
# sudo ./ntopng-setup.sh
#
# Requirements:
# - Debian-based Linux distribution
# - Root/sudo privileges
# - Active internet connection
# =========================================

# Global configuration variables with descriptive comments
NTOPNG_PORT="4400"                     # Web interface port (non-standard for security)
NTOPNG_CONF="/etc/ntopng/ntopng.conf"   # Main configuration file location
NTOPNG_START="/etc/ntopng/ntopng.start" # Startup configuration file location

# Main installation and configuration function
function install_ntopng() {
  # Network interface detection with progressive fallbacks
  # First try: Get default route interface
  INTERFACE=$(ip route | grep default | awk '{print $5}' | head -n1)

  # Second try: Find first active non-virtual interface
  if [ -z "$INTERFACE" ]; then
    INTERFACE=$(ip link show | grep -v "lo\|docker\|veth\|br-\|vir" |
      grep "UP" | head -n1 | awk -F: '{print $2}' | tr -d '[:space:]')
  fi

  # Exit if no valid interface found
  if [ -z "$INTERFACE" ]; then
    echo "===================="
    echo "ERROR: No valid network interface detected"
    echo "===================="
    exit 1
  fi

  # Detect and configure local networks
  # Excludes virtual interfaces and uses common private ranges as fallback
  LOCAL_NETWORKS=$(ip route | grep -v "default\|docker\|br-\|vir" |
    awk '{print $1}' | tr '\n' ',' | sed 's/,$//')

  # Fallback to common private network ranges if detection fails
  if [ -z "$LOCAL_NETWORKS" ]; then
    LOCAL_NETWORKS="192.168.0.0/24,10.0.0.0/8,172.16.0.0/12"
  fi

  # Clean existing installation if present
  if dpkg -l | grep -q "apt-ntop"; then
    echo "===================="
    echo "Removing existing Ntopng installation..."
    echo "===================="
    # Remove all related packages
    sudo apt-get purge -y apt-ntop
    sudo apt-get autoremove -y

    # Clean up repository keys
    echo "Removing existing repository keys..."
    sudo apt-key del "$(sudo apt-key list | grep -i ntop -B 1 | head -n 1 | awk '{print $9}')" 2>/dev/null || true
  fi

  # Repository configuration
  echo "===================="
  echo "Configuring package repositories..."
  echo "===================="
  if ! grep -q "contrib" /etc/apt/sources.list; then
    sudo sed -i '/^deb\|^deb-src/ s/$/ contrib/' /etc/apt/sources.list
  fi

  # Download and install repository package
  echo "Installing Ntopng repository..."
  if ! wget https://packages.ntop.org/apt/bookworm/all/apt-ntop.deb ||
    ! mv apt-ntop.deb /tmp/ ||
    ! sudo apt install /tmp/apt-ntop.deb; then
    echo "ERROR: Repository configuration failed"
    exit 1
  fi

  # Install Ntopng and dependencies
  echo "Installing Ntopng packages..."
  sudo apt-get clean all
  sudo apt-get update
  if ! sudo apt-get install -y pfring-dkms nprobe ntopng n2disk cento ntap; then
    echo "ERROR: Package installation failed"
    exit 1
  fi
  # Create the config directory
  echo "Creating Ntopng config directory..."
  sudo mkdir -p $(dirname $NTOPNG_CONF)

  # Configure main settings
  echo "Configuring Ntopng..."
  sudo tee $NTOPNG_CONF >/dev/null <<EOL
-i=$INTERFACE
-w=$NTOPNG_PORT
EOL

  # Configure network settings
  sudo tee $NTOPNG_START >/dev/null <<EOL
--local-networks "$LOCAL_NETWORKS"
--interface 1
EOL

  # Service management and validation
  echo "Enabling and starting services..."
  if ! sudo systemctl enable ntopng; then
    echo "ERROR: Failed to enable ntopng service"
    exit 1
  fi

  if ! sudo systemctl restart ntopng; then
    echo "ERROR: Failed to restart ntopng service"
    exit 1
  fi

  if ! sudo systemctl is-active ntopng >/dev/null 2>&1; then
    echo "ERROR: ntopng service is not active"
    exit 1
  fi

  # Installation completion message
  echo "===================="
  echo "Installation successful!"
  ss -tnlp | grep ":$NTOPNG_PORT"
  echo "Access Ntopng at: http://localhost:$NTOPNG_PORT"
  echo "Default credentials: admin/admin"
  echo "Please change the password after first login"
  echo "===================="

  # Cleanup
  rm -f /tmp/apt-ntop.deb
}

# Check for root privileges
if [ "$EUID" -ne 0 ]; then
  echo "ERROR: This script must be run as root"
  exit 1
fi

# Execute main installation function
install_ntopng
ShellScript
Posted in Tech BlogTags:
© 2024 Warith AL Maawali. All Rights Reserved.
Stay Secure, Stay Assured.