You are SSH'd into a remote Linux server that has no console access, and you are about to run `iptables -P INPUT DROP`. What is the risk, and how do you make that change without locking yourself out?
answer
- the policy catches everything you did not think of
- your own session is one of those packets
- flushing does not undo a policy
- arm something that undoes it unattended
- test with a second connection, not the current one
basics
~20 sThe policy takes effect instantly for every packet that matches no rule, so unless rules already accept your SSH traffic, the session dies and you cannot reconnect. Put those accept rules in first, verify them with a second session, and arm an automatic rollback before changing the policy.
solid answer
~60 sThe default policy applies to any packet that reaches the end of the chain without matching a terminating rule — including the packets carrying the session you are typing into. Set it to DROP on a chain with no accept rule for SSH and both your current session and every reconnect attempt die at once, with no way back in. So the order is fixed: insert the rule that keeps existing connections flowing and an accept for the SSH port at the top of INPUT, confirm with `iptables -L INPUT -n -v` that their counters are moving, open a **second** SSH session to prove a new connection still succeeds, and only then set the policy. Before the risky step I take `iptables-save > /root/fw-good.v4` — the dump records chain policies too, so restoring it undoes the policy change as well — and schedule an unattended `iptables-restore` a few minutes out as a dead-man switch, cancelling it once I am satisfied. And remember `iptables -F` flushes rules but leaves the policy, so flushing a DROP-policy chain is the classic instant lockout.
code
bash · 6 linesiptables -I INPUT 1 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -I INPUT 2 -p tcp --dport 22 -j ACCEPT
iptables -L INPUT -n -v --line-numbers
iptables-save > /root/fw-good.v4
echo 'iptables-restore < /root/fw-good.v4' | at now + 5 minutes
iptables -P INPUT DROPgo deeper
Know that the default policy applies immediately to anything no rule matched, and that you must allow SSH before setting a chain's policy to DROP on a remote box.
Explain the ordering — insert the accept rules at the top, verify their counters, then set the policy — and why flushing a chain does not reset its policy.
Demonstrate real remote-change discipline: a saved known-good dump, an armed unattended rollback, verification via a second connection, and atomic application of the whole ruleset rather than a sequence of commands.
Own the standard: how firewall changes reach a fleet, what rollback every change must carry, and why hosts that can only be recovered by a data-centre ticket represent a risk to be designed out rather than worked around.
## Why the policy is the dangerous knob Every built-in chain has a default policy, set with `-P` and visible in the header line of an `iptables -L -v` listing, for example `Chain INPUT (policy DROP 12 packets, 900 bytes)`. The policy is not a rule; it is the verdict applied to a packet that has traversed the entire chain without matching any terminating rule. That makes `-P INPUT DROP` categorically different from adding a rule. A rule affects the traffic it matches. A policy affects *everything you have not thought about* — including, on a chain that does not yet accept SSH, the packets of the session you are typing into. There is no confirmation, no grace period, and the change is effective for the very next packet. If the box has no serial console, no cloud console and no out-of-band management, you have just made it unreachable and someone has to file a ticket with a data centre. ## The two rules that must exist first Before the policy, the chain needs to accept the traffic you depend on: 1. **Traffic belonging to connections that are already up.** Without it, even a correctly placed SSH accept rule can leave existing sessions hanging, and every other outbound-initiated exchange the host relies on breaks at the same time. The conventional form is `-m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT`. 2. **New SSH connections**, so you can get back in: `-p tcp --dport 22 -j ACCEPT`, adjusted if `sshd` listens elsewhere. Both go at the *top* of the chain — insert with `-I`, not `-A` — because a rule below an existing catch-all never runs. And restrict the SSH accept by source address only when you are certain of the path you arrive by; a source restriction written from memory, against a NAT or VPN egress address that has since changed, is itself a common way to lock yourself out. ## Flush is not a rollback The single most common self-inflicted lockout is not `-P` at all — it is `iptables -F` on a host whose policy is already DROP. `-F` removes rules. It does **not** reset chain policies. So the command people reach for to "clear the firewall and start again" strips away every accept rule and leaves the DROP policy standing, which is a maximally closed firewall applied instantly. If you ever need to open a host up from the command line, set the policies first and flush second: ```bash iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT iptables -F ``` ## The dead-man switch The technique that makes remote firewall work genuinely safe is an unattended rollback armed before the change and cancelled after it. Dump the known-good state, schedule a restore a few minutes out, make the change, verify, then cancel the rollback: ```bash iptables-save > /root/fw-good.v4 echo 'iptables-restore < /root/fw-good.v4' | at now + 5 minutes # note the job id # ... make the change, then verify from a second session ... atrm <jobid> # cancel once satisfied ``` This works precisely because `iptables-save` records chain policies as `:INPUT ACCEPT [0:0]` lines alongside the rules. Restoring the dump therefore restores the pre-change policy as well as the pre-change rules — it is a complete rollback, not a partial one. (`at` requires `atd` to be running; a detached `sleep` followed by the same restore does the same job where it is not.) ## Apply the whole ruleset at once A sequence of individual `iptables` commands passes through every intermediate state, and one of those intermediate states may be closed. Building the intended ruleset as a file and applying it with `iptables-restore` avoids that: the table is committed atomically, so there is no window where the policy has changed but the accept rules have not. ## Verify with a second session, not with optimism Your current SSH session is the worst possible test, because it is already established and may keep working under rules that block every new connection. The only meaningful check is opening a *new* connection from a second terminal while the first stays open as your lifeline. Watch the counters on the SSH accept rule with `iptables -L INPUT -n -v --line-numbers` and confirm they increment when that second session connects. ## Two smaller details worth knowing `iptables -w` makes the command wait for the xtables lock rather than failing with "Another app is currently holding the xtables lock" — worth having in any script that might race with other firewall writers, because a half-applied ruleset caused by a failed command is exactly the state you are trying to avoid. And if the host has a serial or cloud provider console, confirm it works *before* you need it. A console you have never logged into is not a safety net; it is an assumption.
- Why is `iptables -F` on a host with a DROP policy such a common way to lock yourself out?Because `-F` flushes rules but leaves chain policies untouched. Flushing therefore removes every accept rule while the DROP policy stays in force, which is the most closed firewall the host can have, applied instantly. To open a host up from the command line, set the policies to ACCEPT first and flush afterwards.
- Your current SSH session keeps working after the change. Why is that not proof the change is safe?Because an established session can keep flowing under rules that block every new connection — that is exactly what an accept for existing connections does. The only meaningful test is opening a second, brand-new SSH session from another terminal while the first stays open as a lifeline, and watching the accept rule's counters increment as it connects.
- What makes an `iptables-save` dump a complete rollback artefact rather than just a list of rules?It records each built-in chain's default policy on its `:CHAIN POLICY [pkts:bytes]` line as well as every rule. Restoring the dump therefore puts the policy back to what it was alongside the rules, and restore replaces the table atomically, so the rollback lands in one step with no half-open intermediate state.
- When would you build the ruleset as a file and apply it with `iptables-restore` instead of issuing individual commands?Whenever more than one rule or a policy is changing at once. A sequence of individual commands passes through every intermediate state, and one of them may be closed enough to cut you off; restore commits the table atomically so no such window exists. It is also idempotent, which matters when the same change is applied by automation across a fleet.
Changing the default policy over SSH is like changing the lock on a door while standing inside the room. Cut a key first, check it turns from the outside, and have someone ready to open the door in five minutes if you go quiet.
saying these in an interview costs you the question
- Sets the policy first and adds accept rules afterwards
- Uses iptables -F to "reset" a firewall with a DROP policy
- Treats the still-working current SSH session as proof
- Appends the SSH accept below an existing catch-all rule
- Assumes a console exists without ever having logged into it