Secure Your Bitcoin Node with Linux Firewall Configuration
Security for Decentralized Infrastructure
Part of Pasifika.xyz Web3 Tech Hub
This guide provides step-by-step instructions for implementing Linux OS firewalls on Bitcoin nodes in your Decentralized Physical Infrastructure Network (DePIN) system. A proper firewall configuration is essential for securing your Bitcoin node against unauthorized access while maintaining network connectivity.
Before configuring your firewall, ensure your system is up-to-date and has all necessary packages installed.
# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y
# CentOS/RHEL
sudo yum update -y
# Ubuntu/Debian
sudo apt install iptables-persistent netfilter-persistent fail2ban -y
# CentOS/RHEL
sudo yum install iptables-services fail2ban -y
Start with basic iptables configuration to set up the foundation for your firewall rules.
# Flush all existing rules
sudo iptables -F
sudo iptables -X
sudo iptables -t nat -F
sudo iptables -t nat -X
# Set restrictive defaults
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
# Allow local loopback (essential for system operation)
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A OUTPUT -o lo -j ACCEPT
Setting default policies to DROP will block all incoming connections not explicitly allowed. Make sure you have a way to access your system if something goes wrong, such as a console connection or out-of-band management.
Configure firewall rules specifically for Bitcoin node operations.
# Bitcoin mainnet P2P port (8333)
sudo iptables -A INPUT -p tcp --dport 8333 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
sudo iptables -A OUTPUT -p tcp --sport 8333 -m conntrack --ctstate ESTABLISHED -j ACCEPT
# Bitcoin testnet P2P port (18333) - if using testnet
sudo iptables -A INPUT -p tcp --dport 18333 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
sudo iptables -A OUTPUT -p tcp --sport 18333 -m conntrack --ctstate ESTABLISHED -j ACCEPT
# Bitcoin RPC port (8332) - restrict to localhost and specific IPs
sudo iptables -A INPUT -p tcp --dport 8332 -s 127.0.0.1 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 8332 -s 192.168.1.0/24 -j ACCEPT # Adjust to your network
# Allow outbound connections to other Bitcoin nodes
sudo iptables -A OUTPUT -p tcp --dport 8333 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
sudo iptables -A INPUT -p tcp --sport 8333 -m conntrack --ctstate ESTABLISHED -j ACCEPT
Configure secure administrative access to your Bitcoin node.
# Replace YOUR_ADMIN_IP with your actual admin IP addresses
# Multiple IPs can be added with separate rules
sudo iptables -A INPUT -p tcp --dport 22 -s YOUR_ADMIN_IP -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
# Example for MEIDECC admin network
sudo iptables -A INPUT -p tcp --dport 22 -s 192.168.100.0/24 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
# Allow ping for network diagnostics (rate limited)
sudo iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
Always restrict SSH access to specific trusted IP addresses whenever possible. If you need remote access from various locations, consider setting up a VPN and only allowing SSH access through the VPN.
Special considerations for Bitcoin nodes operating in Pacific Island environments with potentially unstable network conditions.
# Increase connection tracking timeouts for unstable connections
echo 'net.netfilter.nf_conntrack_tcp_timeout_established = 7200' >> /etc/sysctl.conf
echo 'net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120' >> /etc/sysctl.conf
sudo sysctl -p
# Limit new connections per minute (adjust based on your needs)
sudo iptables -A INPUT -p tcp --dport 8333 -m recent --set --name bitcoin_connect
sudo iptables -A INPUT -p tcp --dport 8333 -m recent --update --seconds 60 --hitcount 10 --name bitcoin_connect -j DROP
Pacific Island networks often face challenges with limited bandwidth, higher latency, and occasional connectivity disruptions. These optimizations help your Bitcoin node maintain stable connections despite these challenges, ensuring your node remains a reliable part of the Bitcoin network.
Implement protection against common denial-of-service attack methods.
# Protect against SYN flood attacks
sudo iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j ACCEPT
sudo iptables -A INPUT -p tcp --syn -j DROP
# Detect and block port scanning attempts
sudo iptables -N PORTSCAN
sudo iptables -A PORTSCAN -m limit --limit 1/s --limit-burst 4 -j LOG --log-prefix "Port Scan: "
sudo iptables -A PORTSCAN -j DROP
sudo iptables -A INPUT -p tcp --tcp-flags ALL FIN,URG,PSH -j PORTSCAN
sudo iptables -A INPUT -p tcp --tcp-flags ALL ALL -j PORTSCAN
sudo iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j PORTSCAN
Set up Fail2Ban to protect against brute force attacks on SSH and Bitcoin RPC.
Create /etc/fail2ban/jail.local
:
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
[bitcoin-rpc]
enabled = true
port = 8332
filter = bitcoin-rpc
logpath = /home/bitcoin/.bitcoin/debug.log
maxretry = 5
bantime = 7200
Create /etc/fail2ban/filter.d/bitcoin-rpc.conf
:
[Definition]
failregex = ^.*ThreadRPCServer.*Error.*.*$
ignoreregex =
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
sudo systemctl status fail2ban
Set up logging to monitor firewall activity and potential security incidents.
# Log dropped packets (limited to prevent log spam)
sudo iptables -A INPUT -m limit --limit 2/min -j LOG --log-prefix "IPTables-Dropped: " --log-level 4
Create /usr/local/bin/firewall-monitor.sh
:
#!/bin/bash
# Simple firewall monitoring script for Pasifika DePIN nodes
LOG_FILE="/var/log/firewall-monitor.log"
DATE=$(date '+%Y-%m-%d %H:%M:%S')
# Check for dropped connections
DROPPED_COUNT=$(grep "IPTables-Dropped" /var/log/syslog | wc -l)
# Check Bitcoin node connectivity
BITCOIN_CONNECTIONS=$(bitcoin-cli getconnectioncount 2>/dev/null || echo "ERROR")
# Log status
echo "[$DATE] Dropped packets: $DROPPED_COUNT | Bitcoin connections: $BITCOIN_CONNECTIONS" >> $LOG_FILE
# Alert if no Bitcoin connections (adjust path as needed)
if [ "$BITCOIN_CONNECTIONS" = "0" ] || [ "$BITCOIN_CONNECTIONS" = "ERROR" ]; then
echo "[$DATE] ALERT: Bitcoin node connectivity issue" >> $LOG_FILE
fi
Make it executable and add to cron:
sudo chmod +x /usr/local/bin/firewall-monitor.sh
echo "*/5 * * * * /usr/local/bin/firewall-monitor.sh" | sudo crontab -
Ensure your firewall rules persist across system reboots.
# Ubuntu/Debian
sudo iptables-save > /etc/iptables/rules.v4
# CentOS/RHEL
sudo service iptables save
# Ubuntu/Debian
sudo systemctl enable netfilter-persistent
# CentOS/RHEL
sudo systemctl enable iptables
Verify that your firewall configuration is working correctly.
# Check if Bitcoin node can connect to peers
bitcoin-cli getconnectioncount
# Check peer information
bitcoin-cli getpeerinfo | grep addr
# View current rules
sudo iptables -L -n -v
# Watch firewall logs
sudo tail -f /var/log/syslog | grep IPTables
# Watch fail2ban logs
sudo tail -f /var/log/fail2ban.log
Regular maintenance tasks to keep your firewall effective.
# Emergency rule flush (requires console access)
sudo iptables -F
sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT
Solutions for common issues with firewall configuration.
sudo fail2ban-client status sshd
sudo systemctl status sshd
# Check rule hit counts
sudo iptables -L -n -v
# Reset hit counters
sudo iptables -Z
# Temporary rule addition (for testing)
sudo iptables -I INPUT 1 -s YOUR_IP -j ACCEPT
# Remove specific rule by line number
sudo iptables -D INPUT [line_number]
Follow these best practices to maintain a secure Bitcoin node environment:
This firewall setup provides a robust security foundation for Bitcoin nodes while accounting for the unique challenges of Pacific Island connectivity. The configuration balances security with the need for reliable Bitcoin network participation.
Organizations implementing Bitcoin infrastructure should adjust IP ranges and monitoring parameters according to their specific network topology and requirements.
Properly secured Bitcoin nodes not only protect individual infrastructure but also contribute to the overall security and health of the Bitcoin network. This is especially important for emerging technology hubs in the Pacific Islands region.