This guide helps you diagnose and fix common iptables issues on a CentOS VPS, from rules that won’t save to accidentally locking yourself out. You’ll learn systematic checks and practical fixes to restore your firewall quickly.
What You’ll Need
- Root or sudo access to your CentOS VPS (via SSH or the hosting provider’s console).
- Basic familiarity with the command line.
- If you’ve lost SSH access, use your provider’s out-of-band console (e.g., VNC or web terminal) to recover.
Step-by-Step
- Check if iptables is actually running. Run
systemctl status iptables(orservice iptables statuson older CentOS 6). If it’s inactive or failed, start it withsystemctl start iptablesand enable it withsystemctl enable iptables. - List current rules. Use
iptables -L -n -vto see all rules with line numbers and packet counts. Add--line-numbersfor easier editing. If the output is empty or unexpected, you may have flushed rules or loaded the wrong table. - Check specific tables. iptables has multiple tables: filter (default), nat, mangle, and raw. View them with
iptables -t nat -L -n -v,iptables -t mangle -L -n -v, etc. Many issues are caused by rules in the nat table (e.g., for port forwarding) that are missing or misordered. - Verify the default policy. Look at the INPUT, FORWARD, and OUTPUT chain policies. If INPUT policy is DROP and you have no allow rule for SSH (port 22), you’ll be locked out. The policy is shown at the top of each chain.
- Test connectivity after each change. After modifying rules, test from another machine:
pingfor ICMP,sshfor port 22, or usenc -vz your_server_ip 80for HTTP. If you lose SSH, use the console to revert. - Check rule order. iptables processes rules top-down; the first match wins. If you have a broad DROP rule before a specific ACCEPT, the ACCEPT never runs. Insert specific rules with
-I(insert at top) instead of-A(append to bottom). - Look for typos and syntax errors. A common mistake is using
--dportwithout-p tcpor-p udp. For example,iptables -A INPUT -p tcp --dport 22 -j ACCEPTis correct; omitting-p tcpwill cause an error. - Check if rules are being saved. On CentOS 6, use
service iptables saveto persist. On CentOS 7+, you must installiptables-servicesand then useservice iptables save. If you’re using firewalld, it may override iptables; disable it withsystemctl stop firewalldandsystemctl disable firewalld. - Inspect logs. iptables can log dropped packets if you have a LOG rule. Add one temporarily:
iptables -I INPUT -j LOG --log-prefix "IPT_DROP: "and then checkjournalctl -kortail -f /var/log/messages. Remove the rule after testing. - Test with a simple rule set. If nothing works, flush all rules (
iptables -F) and set a permissive policy (iptables -P INPUT ACCEPT) to confirm the problem is with your rules, not the kernel or network.
Common Mistakes
- Forgetting to enable IP forwarding for NAT or port forwarding. Run
sysctl net.ipv4.ip_forward=1and make it permanent in/etc/sysctl.conf. - Using the wrong interface name. On VPS, interfaces are often
eth0, but could beens3orvenet0. Check withip a. - Blocking loopback. Always allow loopback:
iptables -A INPUT -i lo -j ACCEPT. Blocking it can break local services. - Not saving rules after testing. You may fix the issue but lose it on reboot. Always save after a successful change.
- Conflicting with firewalld. CentOS 7+ uses firewalld by default. If you’re mixing both, they can interfere. Stick to one.
Tips
- Use a script to manage rules. Write your rules in a shell script and run it. This makes it easy to revert and version control.
- Add a cron job to save rules regularly. For example,
@reboot /usr/sbin/iptables-restore < /etc/sysconfig/iptables. - Test with a temporary rule that auto-reverts. Use
at now + 5 minutesto schedule a flush in case you lose connectivity. - Use iptables-save and iptables-restore. These commands dump and load rules in a format that's easy to edit and backup.
- Keep a backup of your working rules.
iptables-save > /root/iptables.backupbefore making changes.
Troubleshooting
If the above steps don't resolve your issue:
- You're still locked out. Use your hosting provider's console (VNC, KVM, etc.) to log in locally. Then flush rules and start over. If you can't access the console, contact your provider's support.
- Rules are correct but traffic still drops. Check if there are other firewalls (e.g., cloud provider security groups, fail2ban, or cPanel's firewall). Disable them temporarily to isolate.
- Changes don't persist after reboot. Ensure iptables-services is installed and enabled. On CentOS 7+, run
systemctl enable iptablesandsystemctl start iptables. Also check if firewalld is masking it. - iptables command not found. Install it with
yum install iptables-services(CentOS 7) oryum install iptables(older). - Kernel module issues. In rare cases, modules like
ip_tablesmay not load. Check withlsmod | grep ip_tables. If missing, trymodprobe ip_tables.
Frequently Asked Questions
How do I allow SSH after I accidentally blocked it?
Use your VPS console to log in locally, then run iptables -I INPUT -p tcp --dport 22 -j ACCEPT and save. If you can't, contact your provider.
Why do my rules disappear after reboot?
iptables rules are not persistent by default. Install iptables-services (CentOS 7+) and run service iptables save, or use iptables-restore in a startup script.
Can I use both firewalld and iptables?
Technically yes, but they manage the same kernel netfilter, so they can conflict. It's best to disable one. If you prefer iptables, stop and disable firewalld.
How do I open a port for a specific IP only?
Use -s to specify source IP: iptables -A INPUT -p tcp --dport 22 -s 192.168.1.100 -j ACCEPT. Place this before any deny rules.
What's the difference between -I and -A?
-I inserts a rule at the top (or a specified position) of a chain; -A appends it to the bottom. Since iptables processes rules in order, -I is often used to prioritize a rule.
How do I see packet counts for my rules?
Use iptables -L -n -v. The counters show how many packets and bytes have matched each rule. This helps identify if a rule is being used.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Secure Boot Battlefield 6 ASUS: Fix & Enable Guide
- Best Tech Websites to Follow for Pros & Beginners
- Second Wind Quest Walkthrough: Where Winds Meet6.00 min/km
- Enshrouded Weapon Upgrade Guide: How to Improve Gear5.00 min/km
- Connect Phone to Controller for Cloud Gaming
- Python Search Highlighting: Snippets & Matches