

Iptables allows you to filter packets based on an IP address or a range of IP addresses. It should return with the results below which means all TCP protocol connections from the specified ports will be accepted: Filtering Packets Based on Source It’s time to check if the rules have been appended in iptables: sudo iptables -L -v Sudo iptables -A INPUT -p tcp -dport 443 -j ACCEPT Sudo iptables -A INPUT -p tcp -dport 80 -j ACCEPT You can execute these commands one by one: sudo iptables -A INPUT -p tcp -dport 22 -j ACCEPT To do this, we need to specify the protocol ( -p) and the corresponding port ( –dport). Next, we want http (port 80), https (port 443), and ssh (port 22) connections to work as usual.

Enabling Connections on HTTP, SSH, and SSL Port The command above will make sure that the connections between a database and a web application on the same machine are working properly. It is utilized for all communications on the localhost. To allow traffic on localhost, type this command: sudo iptables -A INPUT -i lo -j ACCEPTįor this iptables tutorial, we use lo or loopback interface. For this iptables tutorial, we are going to use the INPUT chain as an example. Once you understand the basic syntax, you can start configuring the firewall to give more security to your server. If you want to use all of them, you must write the command in this order: sudo iptables -A -i -p -s -dport -j You need to insert this every time you make a new rule.

Pkts bytes target prot opt in out source destinationĬhain FORWARD (policy ACCEPT 0 packets, 0 bytes)Ĭhain OUTPUT (policy ACCEPT 0 packets, 0 bytes) Below is the example output: Chain INPUT (policy ACCEPT 0 packets, 0 bytes) Here, the -L option is used to list all the rules, and -v is for showing the info in a more detailed format. Check the status of your current iptables configuration by running:.Execute the following command one by one:.If you don’t know, you can read our SSH tutorial. However, if you don’t have it in Ubuntu/Debian system by default, follow the steps below: Iptables comes pre-installed in most Linux distributions. Lastly, we will guide you to make persistent changes in iptables. Secondly, we are going to show you how to define the rules. First, you will learn how to install the tool on Ubuntu. We will divide this iptables tutorial into three steps. How to Install and Use Iptables Linux Firewall If you want to set up a firewall for the ipv6 protocol, you will need to use ip6tables instead. Important! iptables rules only apply to ipv4. If you own Hostinger VPS, you can get the SSH login details on the Servers tab of hPanel. You can establish the connection through PuTTY (Windows) or terminal shell (Linux, macOS). OUTPUT – filter packets that are going out from your server.īefore we begin this guide, make sure you have SSH root or sudo access to your machine that runs on Ubuntu 16.04 or up.FORWARD – filters incoming packets that will be forwarded somewhere else.INPUT – controls incoming packets to the server.In this iptables tutorial, we are going to work with one of the default tables, called filter. RETURN – stops the packet from traversing through a chain and tell it to go back to the previous chain.DROP – will not let the packet pass through.ACCEPT – will allow the packet to pass through.When a packet matches a rule, it is given a target, which can be another chain or one of these special values: These tables contain sets of rules, called chains, that will filter incoming and outgoing data packets. It will monitor traffic from and to your server using tables. Simply put, iptables is a firewall program for Linux. How to Install and Use Iptables Linux Firewall.What is Iptables, and How Does It Work?.
