Open or close a port in iptables on a Linux node, for every address or for one address only. The rules are saved, so they stay after a reboot.
iptables is the firewall built into Linux. It comes installed on almost every Linux distribution, Ubuntu and CentOS included. Allow only the ports you need, to everyone or to one address, and the node stays safer.
Traffic reaches a service on the node only when both its security group and iptables allow it. E2E recommends security groups alone, because a firewall inside the node can conflict with their rules. If you hit such a conflict, use Allow All Traffic on the node’s Security Group tab while you sort it out. Allow All Traffic attaches a security group that opens every port. Detach it as soon as you have found the conflict. See Security Group.
Firewall is a different product: an appliance your traffic passes through, not rules inside one node. See Firewall.
Before you begin
Section titled “Before you begin”You need:
- A Linux node running Ubuntu or CentOS, with iptables installed.
- A user with sudo access, signed in to the node over SSH. See Connect to a Linux node.
If a rule cuts off your SSH session, you can still sign in from the node’s console. See Open a node’s console.
List the current rules
Section titled “List the current rules”To see the rules iptables uses now, run:
sudo iptables -LThe output lists every rule, grouped into chains. INPUT holds the rules for traffic coming in to the node, FORWARD for traffic passing through it, and OUTPUT for traffic going out. Each chain shows its policy, which applies to traffic that no rule matches. On this example node, INPUT drops incoming traffic by default and accepts SSH, HTTP, HTTPS, port 1167, and the Zabbix agent:
root@e2e:~# iptables -LChain INPUT (policy DROP)target prot opt source destinationACCEPT all -- anywhere anywhere state RELATED,ESTABLISHEDACCEPT all -- anywhere anywhereACCEPT all -- anywhere anywhereACCEPT icmp -- anywhere anywhereACCEPT tcp -- anywhere anywhere state NEW tcp dpt:sshACCEPT tcp -- anywhere anywhere state NEW tcp dpt:httpACCEPT tcp -- anywhere anywhere state NEW tcp dpt:httpsACCEPT tcp -- anywhere anywhere state NEW tcp dpt:1167ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:zabbix-agentChain FORWARD (policy DROP)target prot opt source destinationChain OUTPUT (policy ACCEPT)target prot opt source destinationroot@e2e:~#Back up the rules
Section titled “Back up the rules”Before you change anything, save a copy of the current rules. If a change causes a problem, you can put the old rules back from this file:
sudo iptables-save > IPtablesbackup.txtThe file is written to the folder you are in.
Open a port
Section titled “Open a port”To accept incoming connections to a port from any address, run this, with the port number in
place of <port>:
sudo iptables -A INPUT -p tcp --dport <port> -j ACCEPTFor example, to open port 8080 for Tomcat to everyone, run:
sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPTRun sudo iptables -L -n to see the new rule at the end of the INPUT chain. The -n option
shows port numbers. Without it, iptables shows a port by its service name from /etc/services
where it has one, such as ssh for port 22:
root@e2e:~# iptables -L -nChain INPUT (policy DROP)target prot opt source destinationACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHEDACCEPT all -- 0.0.0.0/0 0.0.0.0/0ACCEPT all -- 0.0.0.0/0 0.0.0.0/0ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:443ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:1167ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:10050ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080Chain FORWARD (policy DROP)target prot opt source destinationChain OUTPUT (policy ACCEPT)target prot opt source destinationroot@e2e:~#Open a port to one address only
Section titled “Open a port to one address only”To keep the port closed to everyone except one address, run this instead. Put the address you
allow in place of <ip-address>, such as 203.0.113.10, and the port number in place of
<port>:
sudo iptables -A INPUT -p tcp -s <ip-address> --dport <port> -j ACCEPTClose a port you opened
Section titled “Close a port you opened”To remove a rule you added, run the same command with -D in place of -A. Put the port number
in place of <port>:
sudo iptables -D INPUT -p tcp --dport <port> -j ACCEPTBlock an outgoing port
Section titled “Block an outgoing port”To stop the node from connecting out to a port on any address, run this, with the port number in
place of <port>:
sudo iptables -A OUTPUT -p tcp --dport <port> -j DROPTo block the port only toward one address, run this. Put that address in place of
<ip-address>, such as 203.0.113.10, and the port number in place of <port>:
sudo iptables -A OUTPUT -p tcp -d <ip-address> --dport <port> -j DROPSave the rules so that they stay after a reboot
Section titled “Save the rules so that they stay after a reboot”The rules you add stay in force only until the node restarts. To keep them, save them. Ubuntu saves them with iptables-persistent, which loads the saved rules again at every restart. CentOS saves them with the iptables save command.
On Ubuntu, install iptables-persistent once, before you save:
sudo apt install iptables-persistentOn Ubuntu 14.04, save and reload the rules with:
sudo /etc/init.d/iptables-persistent save
sudo /etc/init.d/iptables-persistent reloadOn Ubuntu 16.04 and later, use:
sudo netfilter-persistent save
sudo netfilter-persistent reloadOn CentOS, as root, use:
service iptables saveCheck the rules
Section titled “Check the rules”Run sudo iptables -L once more. Each rule you added shows in its chain. An opened port is an
ACCEPT line in INPUT. A blocked outgoing port is a DROP line in OUTPUT. A
rule you removed is gone from the list.
Restore the rules from the backup
Section titled “Restore the rules from the backup”If a change causes a problem, put back the rules you saved before you started:
sudo iptables-restore < IPtablesbackup.txtThe node now accepts only the ports you chose, from everyone or from one address, and keeps the rules after a reboot. Open the same ports in the node’s security group too, so traffic reaches them; see Security Group. For help with a port that does not answer, see Troubleshooting node networking and connectivity.