Score:1

does adding a rule to iptables mean it takes effect immediately?

in flag

I run sudo iptables -A INPUT -p tcp -m tcp --dport 2222 -j ACCEPT in bash and then tried to connect to that machine via another machine on the same LAN / subnet via port 2222 with PuTTY and it didn't work.

Maybe I need to restart iptables after I add the rule for it to take affect? If so how would I do that?

Here's the output of iptables -S:

-P INPUT ACCEPT
-P FORWARD DROP
-P OUTPUT ACCEPT
-N DOCKER
-N DOCKER-ISOLATION-STAGE-1
-N DOCKER-ISOLATION-STAGE-2
-N DOCKER-USER
-A INPUT -p tcp -m tcp --dport 2222 -j ACCEPT
-A FORWARD -j DOCKER-USER
-A FORWARD -j DOCKER-ISOLATION-STAGE-1
-A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -o docker0 -j DOCKER
-A FORWARD -i docker0 ! -o docker0 -j ACCEPT
-A FORWARD -i docker0 -o docker0 -j ACCEPT
-A FORWARD -o br-4f5770ea8905 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -o br-4f5770ea8905 -j DOCKER
-A FORWARD -i br-4f5770ea8905 ! -o br-4f5770ea8905 -j ACCEPT
-A FORWARD -i br-4f5770ea8905 -o br-4f5770ea8905 -j ACCEPT
-A DOCKER -d 172.18.0.2/32 ! -i br-4f5770ea8905 -o br-4f5770ea8905 -p tcp -m tcp --dport 3306 -j ACCEPT
-A DOCKER -d 172.18.0.3/32 ! -i br-4f5770ea8905 -o br-4f5770ea8905 -p tcp -m tcp --dport 8080 -j ACCEPT
-A DOCKER -d 172.18.0.3/32 ! -i br-4f5770ea8905 -o br-4f5770ea8905 -p tcp -m tcp --dport 443 -j ACCEPT
-A DOCKER -d 172.18.0.7/32 ! -i br-4f5770ea8905 -o br-4f5770ea8905 -p tcp -m tcp --dport 6379 -j ACCEPT
-A DOCKER -d 172.18.0.8/32 ! -i br-4f5770ea8905 -o br-4f5770ea8905 -p tcp -m tcp --dport 3306 -j ACCEPT
-A DOCKER -d 172.18.0.10/32 ! -i br-4f5770ea8905 -o br-4f5770ea8905 -p tcp -m tcp --dport 3306 -j ACCEPT
-A DOCKER -d 172.18.0.3/32 ! -i br-4f5770ea8905 -o br-4f5770ea8905 -p tcp -m tcp --dport 80 -j ACCEPT
-A DOCKER-ISOLATION-STAGE-1 -i docker0 ! -o docker0 -j DOCKER-ISOLATION-STAGE-2
-A DOCKER-ISOLATION-STAGE-1 -i br-4f5770ea8905 ! -o br-4f5770ea8905 -j DOCKER-ISOLATION-STAGE-2
-A DOCKER-ISOLATION-STAGE-1 -j RETURN
-A DOCKER-ISOLATION-STAGE-2 -o docker0 -j DROP
-A DOCKER-ISOLATION-STAGE-2 -o br-4f5770ea8905 -j DROP
-A DOCKER-ISOLATION-STAGE-2 -j RETURN
-A DOCKER-USER -j RETURN

Here's the output of sudo iptables --line-numbers -L INPUT:

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:2222
in flag
[This answer from several years back](https://askubuntu.com/a/243180/1222991) is still valid today. Generally, if you want something to take effect immediately, `ufw` is the tool you need. It will record the rule change to `iptables` and apply quietly in the background.
Doug Smythies avatar
gn flag
Yes, added iptables rules take effect immediately. We would need you entire iptables rule set posted to be be able to try to help further. But, it might be that you need to insert, rather than append, that rule at an earlier stage.
in flag
@DougSmythies - I added the entire iptables rule set. Thanks!
in flag
@matigo - I tried `sudo ufw allow 2222` without success. I'll read that answer iab - gotta go run some quick errands. Thanks!
Doug Smythies avatar
gn flag
Your iptables rule set is not the problem. Are you sure that computer is listening on port 2222?
Score:6
in flag

Yes, adding rules via the iptables command takes effect immediately.

Presumably you want to add an ACCEPT rule for the port because you want to override rule that blocks all or most ports.

However, you have added the rule with -A which would append the rule to the table. Since you already have a blocking rule (using something like DROP or REJECT), the new rule would be added after that, making it ineffective.

If you want this to work, you need to either insert the rule (-I #) before the blocking rule, or add the rule to the correct position in a config file and reload all rules. (Or use something like ufw or firewalld to do this for you). You can get a numbered list of rules with iptables --line-numbers -L INPUT and insert your new rule at or before the position of your blocking rule.

If the assumption that you have a blocking rule is wrong, then you need to go back and check if anything even has the port open. You can use netstat -nl | grep 2222 or ss -nlt | grep 2222 and if it isn't listed, then there is nothing listening on the port.

From the output you added to your question, the INPUT table is basically empty (except for your accept rule) and -P INPUT ACCEPT says accept anything that doesn't match rules in the input table.

in flag
Here's my `sudo iptables -S` output: https://pastebin.com/4We0DhaP . Nothing looks like a blocking rule to me but I'm afraid to say that I really don't know iptables very well..
in flag
`sudo iptables --line-numbers -L INPUT` returns https://pastebin.com/VcVveE8W
user10489 avatar
in flag
It looks like you don't have any blocking rules on input and the port you are trying to open in the firewall is already open. I think you are fixing the wrong problem, and you need to explain what you are trying to accomplish instead. Probably nothing is listening on the port.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.