iptables is refusing to apply policy changes because it believes the built-in chains (INPUT, FORWARD, OUTPUT) have been modified when they haven’t, leading to errors like "iptables: Policy modification failed: Invalid argument (line 1)" or "iptables: Policy modification failed: iptables-restore: unable to interpret rule set".

Here are the common reasons this happens and how to fix them:

1. Corrupted iptables State File: The most frequent culprit is a corrupted or improperly formatted iptables state file, often found at /etc/iptables/rules.v4 or /etc/sysconfig/iptables. This file is what iptables-restore reads to set up the rules. If it contains syntax errors, invalid characters, or is incomplete, iptables will reject policy changes.

  • Diagnosis: Examine the contents of your iptables state file:

    sudo cat /etc/iptables/rules.v4
    

    Look for any obviously malformed lines, stray characters, or missing sections.

  • Fix: The safest bet is to reset the iptables rules to a known good state and then re-save them.

    sudo iptables -F  # Flush all rules
    sudo iptables -X  # Delete all non-builtin chains
    sudo iptables -t nat -F
    sudo iptables -t nat -X
    sudo iptables -t mangle -F
    sudo iptables -t mangle -X
    sudo iptables -P INPUT ACCEPT # Set default policy to ACCEPT
    sudo iptables -P FORWARD ACCEPT
    sudo iptables -P OUTPUT ACCEPT
    sudo service iptables save # Or 'iptables-save > /etc/iptables/rules.v4'
    

    This command sequence clears all existing rules, deletes custom chains, resets default policies to ACCEPT (which is safe for initial recovery), and then saves this clean state.

  • Why it works: By flushing all rules and resetting default policies, you create a clean slate. Saving this state overwrites the corrupted file with a valid, empty configuration, allowing subsequent policy changes to be applied.

2. Incorrect Default Policy Configuration: Sometimes, the default policy itself is specified incorrectly in the state file, or a tool attempting to set the policy is using invalid syntax. For example, trying to set a policy to something other than ACCEPT, DROP, or REJECT without proper context.

  • Diagnosis: Again, inspect the state file, specifically looking for lines like:

    *filter
    :INPUT DROP [0:0]
    :FORWARD DROP [0:0]
    :OUTPUT DROP [0:0]
    

    Ensure the policies are one of the three allowed values.

  • Fix: Manually edit the state file (e.g., /etc/iptables/rules.v4) to ensure each built-in chain has a valid default policy:

    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    COMMIT
    

    Then, reload the rules:

    sudo iptables-restore < /etc/iptables/rules.v4
    
  • Why it works: This directly corrects the invalid policy definition in the state file, ensuring iptables-restore can parse it and apply the intended policy.

3. Invalid Rule Syntax Interfering with Policy: A malformed rule before the policy definition in the state file can cause iptables-restore to fail entirely, preventing it from even reaching the policy lines. This is especially true if the malformed rule is on a line that iptables-restore interprets as a policy command due to its formatting.

  • Diagnosis: Carefully scan the state file line by line, paying close attention to lines that start with : followed by INPUT, FORWARD, or OUTPUT, and lines that do not appear to be valid iptables commands (e.g., missing arguments, invalid characters).

  • Fix: Correct the syntax of the offending rule. For instance, if you have a rule like:

    -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT
    

    and it’s malformed (e.g., missing --dport or has an invalid protocol), fix it to be correct. If you can’t immediately identify the faulty rule, temporarily comment out suspect rules by adding # at the beginning of the line in the state file, then try iptables-restore again. Once you find the problematic rule, either fix it or remove it.

  • Why it works: iptables-restore processes the rules sequentially. A syntax error halts the process. By correcting or removing the invalid rule, you allow iptables-restore to parse the entire file, including the policy definitions.

4. Using iptables-save and iptables-restore with Different iptables Versions: While less common, using iptables-save from one version of iptables (e.g., iptables-legacy) and iptables-restore from another (e.g., iptables-nft) can lead to parsing issues, especially with newer features or syntax. The state file format can differ subtly.

  • Diagnosis: Check your installed iptables package and its version. On Debian/Ubuntu: dpkg -l | grep iptables. On RHEL/CentOS: rpm -qa | grep iptables. Pay attention if you have iptables-nft installed alongside iptables-legacy.

  • Fix: Ensure you are using consistent tools. If you intend to use the nftables backend (which is the modern default on many systems), make sure your iptables commands are actually invoking iptables-nft. You might need to explicitly use iptables-nft-save and iptables-nft-restore or ensure your iptables symlinks point to the nft version. Alternatively, if you need the legacy backend, ensure iptables-legacy is installed and preferred.

    # Example for forcing nftables backend
    sudo update-alternatives --set iptables /usr/sbin/iptables-nft
    sudo service iptables save
    
  • Why it works: Consistency in the iptables backend and its associated tools ensures that the state file format generated by save is compatible with the format expected by restore.

5. iptables-restore Called with Incorrect Options: If you’re manually running iptables-restore and provide it with options that conflict with how the policy is defined or expected, it can fail. For instance, trying to restore a file that contains only rules without specifying the table (-t) can cause issues if the file is meant for a specific table other than filter.

  • Diagnosis: Review the command you are using to apply your iptables rules. Look for how iptables-restore is invoked.

  • Fix: Ensure iptables-restore is called correctly. If your state file contains rules for multiple tables (filter, nat, mangle), you must restore them separately or ensure the file is structured correctly with *<table> headers.

    # Example: Restoring only filter table rules
    sudo iptables-restore -t filter < /etc/iptables/rules.v4
    # Or if the file has *filter, *nat etc. headers, a single restore should work
    sudo iptables-restore < /etc/iptables/rules.v4
    
  • Why it works: This ensures that iptables-restore is operating on the correct table(s) and processing the state file content as intended, preventing misinterpretation of policy or rule commands.

6. Systemd Unit File Errors: If iptables is managed by a systemd service (e.g., iptables.service), errors in the systemd unit file itself, or incorrect arguments passed to iptables-restore by the service, can manifest as iptables errors.

  • Diagnosis: Check the status and logs of the iptables systemd service.

    sudo systemctl status iptables.service
    sudo journalctl -u iptables.service
    

    Look for any errors related to command execution or file paths.

  • Fix: Edit the systemd service file (often found at /usr/lib/systemd/system/iptables.service or /etc/systemd/system/iptables.service.d/override.conf) to correct any faulty commands or paths. For example, ensure the ExecStart line correctly points to iptables-restore and the state file.

    # Example snippet from a systemd service file
    ExecStart=/sbin/iptables-restore /etc/iptables/rules.v4
    

    After editing, reload systemd and restart the service:

    sudo systemctl daemon-reload
    sudo systemctl restart iptables.service
    
  • Why it works: Correcting the systemd service unit ensures that the iptables-restore command is invoked with the proper arguments and the correct state file, allowing it to apply the rules and policies successfully.

After applying these fixes, the next error you might encounter, if your policies were previously set to DROP or REJECT and you’ve reset them to ACCEPT for recovery, is the inability to connect to services due to overly permissive firewall rules.

Want structured learning?

Take the full Iptables course →