{"id":2413,"date":"2015-11-05T13:36:48","date_gmt":"2015-11-05T11:36:48","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/configure-iptables-centos\/"},"modified":"2015-11-05T13:36:48","modified_gmt":"2015-11-05T11:36:48","slug":"configure-iptables-centos","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/configure-iptables-centos\/","title":{"rendered":"How to configure iptables on CentOS"},"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. If you are using CentOS 7, you should look into configuring firewalld, which combines the functionality of iptables and ip6tables, though it\u2019s possible to still use iptables just the same.<\/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\">On CentOS and other Red Hat variants, iptables often come with some pre-configured rules, check the current iptable rules 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 prot opt source destination\nACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED\nACCEPT icmp -- anywhere anywhere\nACCEPT all -- anywhere anywhere\nACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh\nREJECT all -- anywhere anywhere reject-with icmp-host-prohibited\n\nChain FORWARD (policy ACCEPT)\ntarget prot opt source destination\nREJECT all -- anywhere anywhere reject-with icmp-host-prohibited\n\nChain OUTPUT (policy ACCEPT)\ntarget prot opt source 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;\">.\u00a0<span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\"><em>Input<\/em>\u00a0is for any connections coming to your cloud server,\u00a0the <em>output<\/em>\u00a0is for any leaving traffic, and\u00a0<em>forward is<\/em>\u00a0for any pass-through.<\/span> Each chain also has its\u00a0<em>policy<\/em>\u00a0setting, which determines how the traffic is handled if it doesn\u2019t match any specific rules. By default,<\/span> 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\u00a0iptables, you should first add the rules for allowed inbound traffic for the services you require. Iptables can track the state of the connection, so use the command below to allow established connections to continue.<\/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\u00a0protocol\u00a0uses 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\">If you restart your cloud server, all of these iptables configurations will be wiped. To prevent this, save the rules\u00a0to a file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo iptables-save &gt; \/etc\/sysconfig\/iptables<\/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\/sysconfig\/iptables\n# Add the new rules keeping the current ones\nsudo iptables-restore -n &lt; \/etc\/sysconfig\/iptables<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">CentOS offers a system service called iptables to automate the restore at reboot. However, it does not come in the default configuration and needs to be installed manually.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo yum install iptables-services<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once installed, start and enable the service.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl start iptables\nsudo systemctl enable iptables<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Afterwards, you can simply save the current rules using the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo service iptables save<\/pre>\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 tcp 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, to delete the second rule on the input chain, use this command<\/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. Remember to save the rules to a file before flushing the 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\">Afterward, 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 iptable flushed, your server could be vulnerable to attacks. Secure your system with an alternative method while disabling iptables, even temporarily.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"author":3,"featured_media":27372,"comment_status":"open","ping_status":"closed","template":"","community-category":[259,253],"class_list":["post-2413","tutorial","type-tutorial","status-publish","has-post-thumbnail","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2413","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=2413"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2413\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=2413"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=2413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}