Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
VGSOURCES
Get listed

DebianPATCH 2026.10.16

How to Write iptables Block Rules and Save Them in rules.v4

By VGSources Team 10 min read

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To block an address or service and keep the decision after a reboot, add a narrowly matched DROP rule to the correct chain, place it after the accepts that must continue working, test the entire ruleset, save the known-good state to /etc/iptables/rules.v4, and enable a startup loader such as netfilter-persistent. A command entered with iptables changes the running kernel rules only; Debian documentation notes that rules are otherwise flushed at the next reboot.

This guide covers IPv4 filter rules. It shows how to identify the host’s iptables backend, choose INPUT, OUTPUT, or FORWARD, avoid locking yourself out, build a complete rules.v4 file, and verify that the saved policy is loaded again.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What rules.v4 is—and why a command alone is not enough

iptables rules are evaluated by the kernel while packets traverse the filter table. A command such as sudo iptables -A INPUT -s 203.0.113.10 -j DROP takes effect immediately, but it does not automatically become a boot-time configuration. The file /etc/iptables/rules.v4 is a persistent, text representation of the IPv4 rules that a startup service can restore.

Debian’s iptables documentation states: “As IP-Tables are not persistent, they will be deleted (“flushed”) with the next reboot.” The practical consequence is that a block which appears to work during a session can vanish after a restart unless a persistence mechanism loads it again.

#1 Best Overall
Sale
Gaoye Blue Light Blocking Glasses | Anti UV Ray, For Computers & Gaming, Square Frame, Eyewear for Men and Women
  • BLUE LIGHT BLOCKING GLASSES: Our blue light glasses for women boast UV400 protection, blocking harmful blue rays from computer screens. With these bluelight glasses women, you can enjoy gaming, reading, or watching TV with no worries of eye strain.
  • EYE RELIEF: Our blue light glasses are engineered to reduce blurry vision or strain caused by prolonged screen time. These womens blue light glasses aid in better sleep by preventing blue light from disrupting your natural sleep cycle, helping you fall asleep faster and enjoy a more restful night's sleep, making them the perfect christmas present.
  • COMFORTABLE: Crafted from durable material, our trendy blue light glasses for woman or blue light glasses men ensure long-lasting comfort. The high-transmittance lenses accessories, gamer gifts for men, help keep your eyes relaxed. Perfect stocking stuffers, gifts for gamers men, christmas gifts for men, halloween costumes.
  • LIGHTWEIGHT: These clear blue light glasses feature a lightweight frame, meaning they’ll stay comfy for long-term wear. Perfect as blue blockers glasses for women or mens blue light glasses, these non-prescription fake glasses for men can be worn throughout the day. The blue light blocking glasses women feature polycarbonate HD lenses, which are anti-reflective and restore true color.
  • WORRY-FREE PURCHASE: If you experience any issues, our support team is ready to assist you quickly. These are the perfect costume glasses for cosplay dressing up.

Think of the workflow as two separate states:

  • Running state: the rules currently installed in the kernel, inspected with iptables -L and exported with iptables-save.
  • Persistent state: the file that startup integration reads, normally /etc/iptables/rules.v4.

Saving before the rules have been tested merely preserves a mistake. Always make the running state correct first, then export that known-good state.

Inspect the active rules and identify the backend

Start from a shell with administrative privileges and record what is already active:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo iptables -L -v --line-numbers
sudo iptables-save

The listing shows chains, targets, packet and byte counters, and line numbers. The saved output shows the complete table-oriented form that can be restored later. Keep a copy of this output before editing so you know what policy you started with.

Legacy iptables versus an nftables-backed command

On contemporary Debian systems, the command named iptables may be the traditional legacy implementation or an iptables-nft compatibility layer. Debian warns that nftables has been the default firewall framework since Debian 10 (Buster). The command syntax can look identical while the underlying packet-filtering backend differs.

Do not assume that a recipe written for an older Debian release describes your host exactly. Determine which implementation is selected by the system’s alternatives and package configuration, and check the semantics against the iptables version installed on the machine. The legacy Debian manpage cited in older guidance is not a substitute for the version-specific documentation on your host.

The persistence path discussed here remains the Debian package convention for IPv4 rules, but you should avoid mixing an independently managed nftables ruleset with an iptables ruleset without understanding which service owns the firewall. A rule that is visible in one tool but not in the active backend is not a reliable control.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the chain that sees the traffic

A correct match in the wrong chain never blocks the packet you intended. Select the chain according to the packet’s direction relative to this machine:

Rank #2
Windex Gaming Cleaning Wipes, Pre-Moistened Wipes to Instantly Clean all Gaming Gear and Electronics, Resealable Packaging for a Freshness Lock, 25 count (Pack of 1)
  • WINDEX GAMING WIPES: effortlessly clean all your gaming gear and gaming electronics in one easy step with our new Windex Gaming Wipes
  • AMMONIA FREE: our ammonia-free formula is safe to use on all of your favorite gaming equipment like consoles, headsets, controllers, screens, keyboards, and more
  • PRE-MOISTENED WIPES: each wipe instantly removes dust, dirt, and stubborn smudges and fingerprints leaving a streak-free shine
  • ANTI-STATIC: great for cleaning and wiping down your gaming gear and electronics leaving behind no visible residue
  • COMPACT RESEALABLE PACKAGE: store your pack of Windex Gaming Wipes anywhere in your home for whenever a quick and easy wipedown is needed
Chain Use it for Typical match
INPUT Traffic entering this host for a local process Block a remote address connecting to a service on this machine
OUTPUT Traffic generated by this host and leaving it Prevent this machine from sending to a destination address
FORWARD Traffic routed through this host rather than delivered locally Control packets passing between interfaces on a router or gateway

These chains are evaluated independently. An inbound connection to a daemon belongs in INPUT; a local program’s connection attempt belongs in OUTPUT; traffic merely routed onward belongs in FORWARD. If you are unsure, inspect interfaces and the machine’s routing role before adding a rule.

Put safety accepts before a blocking rule

iptables processes rules in order. Once a packet matches a terminating target such as ACCEPT or DROP, later rules in that chain do not get to change the result. A broad drop placed at the top can therefore cut off administration, while an accept placed first can make a later drop unreachable.

A practical order for an INPUT chain is:

  1. Accept loopback traffic with -i lo.
  2. Accept packets belonging to already established or related connections using conntrack state.
  3. Accept the management service, such as SSH on TCP port 22, from the addresses or networks that actually administer the host.
  4. Add narrow service-specific blocks, such as a single source address or destination port.
  5. Only then consider a broad default policy, and only when every required service has an explicit preceding accept.

This is not a universal policy file. It is an ordering model. Existing rules may already provide some of these accepts, and replacing them blindly can remove access that the host needs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Write narrowly scoped DROP rules

DROP discards packets that match. The examples below use the documentation address 203.0.113.10; substitute the address or port that is appropriate for your environment.

Block one source address from reaching local services

sudo iptables -A INPUT -s 203.0.113.10 -j DROP

This appends a rule to INPUT matching packets whose source address is 203.0.113.10. It affects traffic entering the host, not traffic that the host initiates and not packets being routed between other interfaces.

Block a TCP destination port on this host

sudo iptables -A INPUT -p tcp --dport 25 -j DROP

The protocol selector limits the match to TCP and --dport 25 selects the destination service port. A UDP service on the same number would require its own UDP rule if it also needs to be blocked.

Prevent this host from contacting an address

sudo iptables -A OUTPUT -d 203.0.113.10 -j DROP

This controls locally generated traffic whose destination is the specified address. It does not prevent another machine from contacting this host.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
LISEN Cell Phone Stand Phone Holder for Desk, Office Decor Gadgets for Men | Office Gifts for Women, iPhone Stand, Gadgets for Men, Office Decor, Gaming Desk Accessories for Samsung Fold 8, Switch, iPad
  • Ergonomic & Adjustable Design: Fight "text neck" and improve your posture. Designed for comfort, it offers an adjustable height (7.1in-8.5in) (1.4in adjustable range) and angle (5°-85°) Please note: cell phone stand is only 9 inches tall—the length of a standard pencil
  • Without blocking subtitles Perfect for office desk Tiktok Face Time Zoom video—this is the ultimate kitchen desk essential for phones! Perfect viewing experience while reducing neck back strain, A must-have for gamers streamers desk, and anyone who values both style and support!
  • Sleek & Stable Design fits Otterbox Case: Say goodbye to bent cables, tangles, and tip-overs! LISEN’s stylish adjustable desk phone holder features a built-in charging hole and smart cable management, keeping your phone secure and your workspace clutter-free cool gadgets for men
  • ANTI-SLIP DESIGN free hands: The under 10.00 items easy gadgets back and the bottom of this kitchen essentials for women are fully covered by anti-skid silicone, which LISEN stand up desk decor can provide maximum protection device from any scratches and slides
  • Your Perfect Viewing Companion:If your product arrives damaged or breaks within 365 days, reach out to LISEN for a quick and hassle-free solution. Struggling with assembly, like tightening the screw? We’ve got your back! Your experience matters to us. (Product including: phone Holder stand*1, screw*1, hexagon screwdriver*1, specification*1)

Decide whether an address or a service is the real requirement

  • Use a source-address match when one sender must be denied regardless of which local service it targets.
  • Use a destination-port match when the service must be closed to incoming clients but other services should remain reachable.
  • Use an output destination match when the concern is where this host connects, rather than who connects to it.
  • Use the forwarding chain only when this machine is routing the traffic.

Keep matches as specific as the requirement allows. A narrow rule is easier to audit and less likely to interfere with unrelated traffic.

Build a complete rules.v4 file

A persistent file is not just a list of isolated commands. It has a table header, built-in chain policies, rules, and a terminating COMMIT. A minimal filter-table example that preserves loopback, established connections, and SSH before blocking one source is:

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p tcp --dport 22 -j ACCEPT
-A INPUT -s 203.0.113.10 -j DROP
COMMIT

The counters in the chain declarations start at zero in this example. The three built-in policies are shown as ACCEPT so that the file is intentionally conservative; changing a policy to DROP is a separate design decision that requires accepts for every service and management path you need.

Do not copy this sample as a blanket firewall without adapting it. Confirm the actual management port, interface layout, existing state rules, and services on the host. A rule file should represent the complete policy you intend to load, not only the one line that motivated the change.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Apply and test the complete ruleset safely

When the file is ready, restore it into the running firewall:

sudo iptables-restore < /etc/iptables/rules.v4

This command changes the active rules immediately. It is therefore a test of the whole file, not merely a syntax exercise. If you are connected remotely, keep a second management session open. One session can verify connectivity while the other applies the change, giving you a path to correct an ordering mistake before losing access.

Check the result and counters

sudo iptables -L -v --line-numbers

Confirm that:

  • the intended chain contains the rule;
  • the rule appears after the required loopback, established/related, and management accepts;
  • the target is DROP and the source, protocol, and port are exactly the values you intended;
  • packet and byte counters increase when matching traffic is generated; and
  • unrelated services still work.

A counter of zero does not prove that the rule is wrong; it may simply mean that no matching traffic has arrived yet. Conversely, a counter increasing on an unexpected rule is evidence that an earlier match is handling the traffic before it reaches the block.

Rank #4
Ledagic IC+RGBW USB Powered Led Light Bar with App & Remote Control, 2 Pack
  • Easy Installation: Our gaming lights have been designed to enhance your entertainment experience. Simply position the led light bar next to your TV or game console to create a game room environment with ease. They come with USB power and a 60 inch cord for direct mounting and mobility
  • Audio Syncing & Extensive Mode: The built-in high-sensitivity music sensor allows you to sync the TV led lights to your favorite music. Lights react and move to the beat of music, film and game audio, creating an incredible immersive sound and light experience – and all without the need for an additional hub. You can choose from 64+ patterns, including strobe, illusion, jump, energy, gradient, running, chasing, fluttering mode etc. Each mode can be applied to different scenes
  • 64+ Chasing IC+RGB & W Effect: The independent control design of IC+RGB and white all-in-one multifunctional rgbw led bar lights monitor backlight allows you to easily switch the led lights for desk between IC+RGB and white to enjoy a variety of lighting effects, you can choose white lighting mode with high brightness for better illumination when working, etc. Choose IC+RGB lighting mode when you need to create an entertaining atmosphere for the light show, 16 million colours of Color changing rgb light bar let you dance to the beat and create a dynamic party atmosphere
  • Dynamic and Dimmable Lights: 1%-full brightness free brightness adjustment gives you the flexibility to adjust dimmable led light bar for room decorations or a variety of occasions
  • USB Powered: USB light bars for room can be powered by powernank, usb charger, TV, Monitor, laptop PC mainframe, making behind TV led lighting bar easy to install in any location.TV led lights are powered by the TV usb interface / PC mainframe, can be turned on/off synchronously with TV / PC

Save the known-good running state to rules.v4

After testing, export the active IPv4 rules to the Debian persistence path:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo iptables-save | sudo tee /etc/iptables/rules.v4

The pipe sends the complete current ruleset to tee, which has administrative privileges and writes the file. This avoids the common shell-redirection mistake in which sudo applies only to iptables-save while the unprivileged shell attempts to open the destination. A root-only redirection is another valid way to write the same file.

Save only after you have verified the running state. If you save first and discover that SSH was blocked or a needed accept was ordered too late, the persistence file will faithfully preserve that failure and reload it at the next boot.

Enable boot-time loading with netfilter-persistent

The Debian persistence package supplies startup integration for saved netfilter rules. Its netfilter-persistent command operates through plugins:

  • netfilter-persistent save asks the plugins to save the currently loaded rules to persistent storage.
  • netfilter-persistent start loads the persistent rules during a service start, including boot-time startup when the package integration is enabled.
  • netfilter-persistent stop and flush provide lifecycle operations defined by the command’s manual.

With the Debian package installed, use the package’s startup integration rather than assuming that merely creating /etc/iptables/rules.v4 is sufficient. The package template documents that the current IPv4 rules can be saved to this path and loaded automatically at startup; it also notes automatic saving during package installation and directs ongoing updates to iptables-save(8).

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If you change rules interactively later, either run netfilter-persistent save or repeat the explicit iptables-save | sudo tee /etc/iptables/rules.v4 export. Otherwise the running firewall and the file can diverge.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Verify a manual reload and a reboot

Before relying on a reboot, exercise the same load operation that startup will use:

Best Value
Sale
HUANUO FlowLift™ Dual Monitor Stand, Fully Adjustable Gaming Monitor Desk Mount for 13–32″ Computer Screens, Full Motion VESA 75x75/100x100 with C-Clamp & Grommet Base, Each Arm Holds 4.4 to 19.8 lbs
  • Compatible with Wide Screens - To ensure compatibility with the dual monitor mount, your each monitor must meet three conditions at the same time: First, computer screens size range: 13 to 32 inches. Second, screen weight range: 4.4 to 19.8 lbs. Third, the back of the monitor screen must have VESA mounting holes with a pitch of 75x75mm or 100x100mm.
  • Regarding the compatibility with desks - Your desk must meet three conditions at the same time: First, desk material: Only wooden desks are recommended, plastic or glass desks cannot be used. Second, desk thickness range: 0.59" - 3.54". Third, the bottom of the desk should not have any cross beams or panels, as this will interfere with installation. We recommend carefully checking that your desk and monitors meets all above conditions before purchasing.
  • Dual C-Clamp Hold - Worried your dual monitors might wobble or slip? Our upgraded base uses a larger platform plus a dual C-clamp structure to lock the dual monitor arm firmly to your desk. Each arm safely keeps your screens steady while you type, click and game—no shaking, no sliding, just a clean and secure setup you can trust every day. It also provides Grommet Mounting installation choice, both options ensure stable and secure fixation for your 0.59" - 3.54" desk.
  • Full-Motion Adjustment For Comfortable View - Pull the screen closer when you’re deep in a spreadsheet, push it back to watch videos, or rotate to portrait for coding — moving everything smoothly with just one hand. The monitor stand offers +85°/-50° tilt, ±90° swivel and 360° rotation. Raise your monitor up to 15.75″ to support a healthy sitting posture. Whether you’re working from home, gaming through the night, or switching between video calls and documents, getting the screens to your natural line of sight helps relieve neck, shoulder and back strain so you can stay focused longer with less fatigue.
  • Keep Your Desk Organized: By lifting both screens off the desktop, this dual monitor stand opens up valuable space for your keyboard, notebook, docking station or a simple, clutter-free work area. Built-in cable management guides wires along the arms, keeping cords out of sight and out of the way. Enjoy a tidy, modern workstation that looks as good as it feels to use.
  1. Restore the file with sudo iptables-restore < /etc/iptables/rules.v4.
  2. Inspect the result with sudo iptables -L -v --line-numbers.
  3. Confirm the block rule, ordering, targets, and counters.
  4. After a planned reboot, run the listing again and verify that the same rules are present.

The post-reboot check matters because a rule can work interactively while the persistence service is disabled, misconfigured, or loading a different file. Verification should examine the complete chain rather than looking only for the one DROP line.

Troubleshoot the failures that matter most

Remote administration stopped working

The usual cause is a broad drop before the management accept, or a source-specific drop that also matches your administration address. Use the still-open management session or console access to restore a known-good file, then inspect line numbers and move the necessary SSH or other management accept ahead of the blocking rule. Do not keep appending exceptions indefinitely; rebuild the order so the intended decision is obvious.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The rule is present but traffic is not blocked

Check the chain first. Incoming local traffic belongs in INPUT, locally generated traffic in OUTPUT, and routed traffic in FORWARD. Then check the match fields: source versus destination address, TCP versus UDP, and destination port. Finally inspect counters. If an earlier ACCEPT matches, the packet will never reach a later DROP.

The rule disappears after reboot

Confirm that the tested rules were exported to exactly /etc/iptables/rules.v4 and that the persistence package’s startup integration is enabled. Run netfilter-persistent start manually and inspect the chains afterward. If the file loads manually but not at boot, the problem is service integration rather than the DROP syntax.

The file and the command output do not agree

The file may be stale because rules were edited interactively after the last save. Treat the active rules shown by iptables-save as the source for a new snapshot, test that snapshot with iptables-restore, and save again only after verification.

Backend behavior is unexpected

Recheck whether the host uses legacy iptables or an iptables-nft compatibility layer. Debian’s nftables-default warning applies from Debian 10 Buster onward, while older manpages may describe legacy behavior. Use the installed command and package documentation as the authority for that host instead of assuming that a rule written for another release maps identically.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Command, file, and policy approaches compared

Decision Advantages Risk or limitation
Immediate command-line rule Fast way to change the running firewall and observe the result. Not persistent by itself; the rule can be flushed at reboot.
Declarative rules.v4 file Records the complete IPv4 policy in a repeatable form and can be restored as one unit. A malformed or overly broad file can affect many chains at once; test before saving it as the known-good state.
Manual restore Useful for controlled reloads and recovery with iptables-restore. Someone must run it after a reboot or change unless startup integration is configured.
Boot-time netfilter-persistent loading Restores the saved rules automatically through package startup integration. It loads what was saved; stale or incorrect contents are reproduced reliably.
Legacy iptables versus iptables-nft compatibility Both expose familiar iptables commands on systems that support them. The underlying backend and version affect semantics and administration; identify the implementation first.
Narrow match-specific DROP Limits collateral impact to a source, destination, protocol, or port. Requires precise matching and the correct chain.
Broad chain policy Can provide a deny-by-default posture when every required accept is explicitly defined first. Easy to lock out administration or break services if ordering and exceptions are incomplete.

A repeatable checklist

  1. Inspect sudo iptables -L -v --line-numbers and export a baseline with sudo iptables-save.
  2. Identify whether the host uses legacy iptables or an iptables-nft compatibility layer.
  3. Choose INPUT, OUTPUT, or FORWARD from the packet’s path.
  4. Preserve loopback, established/related traffic, and management accepts before adding a drop.
  5. Write the narrowest matching rule for the source, destination, protocol, or port.
  6. Place the complete policy in /etc/iptables/rules.v4 and restore it.
  7. Test from a second management session when working remotely.
  8. Inspect order, targets, and counters with iptables -L -v --line-numbers.
  9. Save the verified active state with iptables-save | sudo tee /etc/iptables/rules.v4 or netfilter-persistent save.
  10. Ensure startup integration is enabled, then verify the same rules after a reload and reboot.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

DebianiptablesLinux firewallnetfilter-persistentnetwork securityrules.v4

Leave a reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.