Blog | G5 Cyber Security

Secure SQL Access: IP Whitelisting

TL;DR

Allowing direct SQL connections from the internet is risky. This guide shows you how to restrict access to specific, trusted IPs using your database server’s firewall and configuration settings. We’ll cover common databases like MySQL/MariaDB and PostgreSQL.

Steps

  1. Understand the Risks
  • Identify Trusted IPs
  • MySQL/MariaDB Configuration
  • bind-address = 192.168.1.10
    bind-address = 10.0.0.5
  • Important: Each IP needs its own bind-address line.
  • Restart the MySQL server for changes to take effect. Example commands:
  • MySQL/MariaDB Firewall Rules
  • sudo iptables -A INPUT -p tcp --dport 3306 -s 192.168.1.10 -j ACCEPT
    sudo iptables -A INPUT -p tcp --dport 3306 -s 10.0.0.5 -j ACCEPT
    sudo iptables -A INPUT -p tcp --dport 3306 -j DROP
  • Important: Ensure the rules are ordered correctly – allow trusted IPs *before* dropping all other connections.
  • Save your firewall rules to make them persistent across reboots. (e.g., sudo iptables-save > /etc/iptables/rules.v4)
  • PostgreSQL Configuration
  • listen_addresses = '192.168.1.10, 10.0.0.5'
  • Edit the pg_hba.conf file to control client authentication based on IP address. Add lines like these:
  • host   all             all             192.168.1.10/32            md5
    host   all             all             10.0.0.5/32               md5
  • The /32 specifies a single IP address. Use appropriate CIDR notation for ranges.
  • Restart the PostgreSQL server:
  • sudo systemctl restart postgresql
  • PostgreSQL Firewall Rules
  • Testing
  • Regular Review
  • Exit mobile version