{"id":2407,"date":"2015-11-05T13:45:59","date_gmt":"2015-11-05T11:45:59","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/configure-iptables-debian\/"},"modified":"2015-11-05T13:45:59","modified_gmt":"2015-11-05T11:45:59","slug":"configure-iptables-debian","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/configure-iptables-debian\/","title":{"rendered":"How to configure iptables on Debian"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The\u00a0user-space application program iptables allows configuring the tables provided by the Linux kernel firewall and the chains and rules it stores. The kernel module currently used for iptables only applies to IPv4 traffic. To configure firewall rules for IPv6 connections, use ip6tables, which respond to the same command structures as iptables.<\/p>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Listing current rules<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Debian servers do not implement any restrictions by default, but for future reference, check the current iptable rules by using the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo iptables -L<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will print out a list of three chains, <em>input<\/em>, <em>forward<\/em> and <em>output<\/em>, like the empty rules table example output below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Chain INPUT (policy ACCEPT)\ntarget&nbsp;&nbsp;&nbsp;&nbsp; prot opt source&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; destination\nChain FORWARD (policy ACCEPT)\ntarget&nbsp;&nbsp;&nbsp;&nbsp; prot opt source&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; destination\nChain OUTPUT (policy ACCEPT)\ntarget&nbsp;&nbsp;&nbsp;&nbsp; prot opt source&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; destination<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The chain names indicate which traffic the rules in each list will be applied to<span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\">. <\/span><span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\">The\u00a0<em>input<\/em>\u00a0is for any connections coming to your cloud server, the output is for any leaving traffic, and the forward is for any pass-through.<\/span> Each chain also has its\u00a0policy\u00a0setting, which determines how the traffic is handled if it doesn\u2019t match any specific rules. By default, it\u2019s set to <em>accept<\/em>.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/signup.upcloud.com\/\">Try UpCloud for free!<\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Adding rules<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Firewalls can commonly be configured in one of two ways: either set the default rule to accept and then block any unwanted traffic with specific rules, or use the rules to define allowed traffic and block everything else. The latter is often the recommended approach, as it allows preemptively blocking traffic rather than having to reactively reject connections that should not be attempting to access your cloud server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To begin using&nbsp;iptables, you should first add the rules for allowed inbound traffic for the services you require. Iptables can track the state of the connection, use the command below to allow established connections.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can check that the rule was added using the same <em>sudo iptables -L<\/em> as before.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, traffic to a specific port will be allowed to enable SSH connections with the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo iptables -A INPUT -p tcp --dport ssh -j ACCEPT<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <em>ssh<\/em> in the command translates to port number 22, which the protocol uses by default. The same command structure can also be used to allow traffic to other ports. To enable access to an HTTP web server, use the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After adding all the allowed rules you require, change the input policy to drop.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><span style=\"color: #ff0000;\">Warning:<\/span> Changing the default rule to drop will permit only specifically accepted connections. Before changing the default rule, make sure you\u2019ve enabled at least SSH, as shown above.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"color: #ff0000;\">sudo iptables -P INPUT DROP<\/span><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The same policy rules can also be defined for other chains by entering the chain name and selecting either DROP or ACCEPT.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Saving and restoring rules<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now, if you were to restart your cloud server, all of these iptables configurations would be wiped. To prevent this, save the rules\u00a0to a file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo sh -c 'iptables-save &gt; \/etc\/iptables\/rules.v4'<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can then simply restore the saved rules by reading your saved file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Overwrite the current rules\nsudo iptables-restore &lt; \/etc\/iptables\/rules.v4\n# Add the new rules keeping the current ones\nsudo iptables-restore -n &lt; \/etc\/iptables\/rules.v4<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can automate the restore process at reboot by installing an additional package for iptables, which takes over the loading of the saved rules. To this with the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo aptitude install iptables-persistent<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After the installation, the initial setup will ask you to save the current rules for IPv4 and IPv6. Just select <em>Yes<\/em> and press enter for both.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you make further changes to your iptables rules, remember to save them again using the same command as above. The iptables-persistent looks for the files <em>rules.v4<\/em> and <em>rules.v6<\/em> under <em>\/etc\/iptables<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These are just a few simple commands you can use with iptables, which is capable of much more. Read on to check on some of the other options available for more advanced control over iptable rules.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced rule setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As per basic firewall behaviour, the rules are read in the order they are listed on each chain, which means you\u2019ll need to put the rules in the correct order. Appending new rules adds them to the end of the list. You can add new rules to a specific list position by inserting them using <em>iptables -I &lt;index><\/em> -command, where the <em>&lt;index><\/em> is the order number in which you wish to insert the rule. To know which index number to enter, use the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo iptables -L --line-numbers<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Chain INPUT (policy DROP)\n num target prot opt source   destination\n 1   ACCEPT all  --  anywhere anywhere ctstate RELATED,ESTABLISHED\n 2   ACCEPT tcp  --  anywhere anywhere , dpt:ssh\n 3   ACCEPT tcp  --  anywhere anywhere tcp dpt:http<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The number at the beginning of each rule line indicates the position in the chain. To insert a new rule above a specific existing rule, simply use the index number of that existing rule. For example, to insert a new rule at the top of the chain, use the following command with index number 1.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you wish to remove an existing rule from a certain chain, use the delete command with the parameter <em>-D<\/em>. The easiest way to select the deletion rule is to use the abovementioned index numbers. For example, use this command to delete the second rule on the input chain.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo iptables -D INPUT 2<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It\u2019s also possible to flush all rules of a specific chain or even all the iptables using the -F -parameter. This is useful if you suspect iptables is interfering with your attempted network traffic or you simply wish to start configuring again from a clean table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><span style=\"color: #ff0000;\">Warning:<\/span> Make sure you set the default rule to ACCEPT before flushing any chain.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"color: #ff0000;\">sudo iptables -P INPUT ACCEPT<\/span><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Afterwards, you can go ahead and clear other rules. Remember to save the rules to a file before flushing the table in case you want to restore the configuration later.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Clear input chain\nsudo iptables -F INPUT\n# Flush the whole iptables\nsudo iptables -F<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With the <tt>iptable<\/tt> flushed, your server could be vulnerable to attacks. Secure your system with an alternative method while disabling iptables, even temporarily.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","community-category":[259,253],"class_list":["post-2407","tutorial","type-tutorial","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/types\/tutorial"}],"author":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/comments?post=2407"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2407\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=2407"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=2407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}