Skip to main content
Category

Server Admin

Server Management

Script to add ngosang trackers to qbittorrent-nox

By Server Admin No Comments
#!/bin/bash

# Define file paths and URL
CONFIG_FILE="/root/.config/qBittorrent/qBittorrent.conf"
TEMP_FILE="/tmp/qBittorrent.conf.tmp"
URL="https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_best.txt"

# Step 1: Stop qBittorrent service
echo "Stopping qBittorrent service..."
systemctl stop qbittorrent-nox
if [ $? -ne 0 ]; then
    echo "Warning: Failed to stop qbittorrent-nox. Proceeding anyway, but this may cause issues if the service is running."
fi

# Ensure the configuration directory exists
mkdir -p "$(dirname "$CONFIG_FILE")"

# Step 2: Fetch new trackers from the URL into an array
mapfile -t new_trackers < <(curl -s "$URL" | grep -v '^$' | grep -v '^#')

# Check if trackers were fetched
if [ ${#new_trackers[@]} -eq 0 ]; then
    echo "Error: No trackers fetched from $URL."
    # Restart service even on failure to restore state
    systemctl start qbittorrent-nox
    exit 1
fi

# Step 3: Join new trackers with literal \n\n (overwriting existing trackers)
if [ ${#new_trackers[@]} -gt 0 ]; then
    # Build the string with literal \n\n separators
    combined_trackers="${new_trackers[0]}"  # First tracker without prefix
    for ((i=1; i<${#new_trackers[@]}; i++)); do
        combined_trackers="${combined_trackers}\\n\\n${new_trackers[$i]}"
    done
    # Escape any existing backslashes in tracker URLs to ensure they’re preserved
    combined_trackers=$(echo "$combined_trackers" | sed 's/\\/\\\\/g')
else
    combined_trackers=""
fi

# Step 4: Update the configuration file, overwriting existing trackers
if [ -f "$CONFIG_FILE" ]; then
    if grep -q '^\[BitTorrent\]' "$CONFIG_FILE"; then
        if grep -q '^Session\\AdditionalTrackers=' "$CONFIG_FILE"; then
            # Replace existing Session\AdditionalTrackers with new trackers
            sed "s|^Session\\\\AdditionalTrackers=.*|Session\\\\AdditionalTrackers=$combined_trackers|" "$CONFIG_FILE" > "$TEMP_FILE" && mv "$TEMP_FILE" "$CONFIG_FILE"
        else
            # Add Session\AdditionalTrackers under [BitTorrent]
            sed "/^\[BitTorrent\]/a Session\\\\AdditionalTrackers=$combined_trackers" "$CONFIG_FILE" > "$TEMP_FILE" && mv "$TEMP_FILE" "$CONFIG_FILE"
        fi
    else
        # Append [BitTorrent] section with trackers using printf
        {
            cat "$CONFIG_FILE" 2>/dev/null
            printf '\n[BitTorrent]\nSession\\AdditionalTrackers=%s\n' "$combined_trackers"
        } > "$TEMP_FILE" && mv "$TEMP_FILE" "$CONFIG_FILE"
    fi
else
    # Create new file with [BitTorrent] and trackers using printf
    printf '[BitTorrent]\nSession\\AdditionalTrackers=%s\n' "$combined_trackers" > "$CONFIG_FILE"
fi

# Step 5: Start qBittorrent service
echo "Starting qBittorrent service..."
systemctl start qbittorrent-nox
if [ $? -ne 0 ]; then
    echo "Error: Failed to start qbittorrent-nox. Please check the service status manually."
    exit 1
fi

echo "Trackers overwritten successfully in $CONFIG_FILE and qBittorrent service restarted." 

Ubuntu : Change default SSH Port

By Server Admin No Comments

Changing the port in “/etc/ssh/sshd_config” no longer works.

The port can be changed in ‘/lib/systemd/system/ssh.socket’ but that won’t survive SSHD update.

Follow these steps:

mkdir /lib/systemd/system/ssh.socket.d

nano /lib/systemd/system/ssh.socket.d/port.conf
[Socket]
ListenStream=
ListenStream=2222

systemctl daemon-reload
systemctl restart ssh
systemctl restart sshd

Verify: netstat -luntp | grep 2222