accept<\/em>.<\/p>\n\n\n\nAdding rules<\/h2>\n\n\n\n
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 by using the rules to define allowed traffic and blocking everything else. The latter is often the recommended approach, as it allows pre-emptively blocking traffic, rather than having to reactively reject connections that should not be attempting to access your cloud server.<\/p>\n\n\n\n
To begin using iptables, 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 continue.<\/p>\n\n\n\n
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT<\/pre>\n\n\n\nYou can check that the rule was added using the same sudo iptables -L<\/em> as before.<\/p>\n\n\n\nNext, allow traffic to a specific port to enable SSH connections with the following.<\/p>\n\n\n\n
sudo iptables -A INPUT -p tcp --dport ssh -j ACCEPT<\/pre>\n\n\n\nThe ssh<\/em> in the command translates to the port number 22, which the protocol uses by default. The same command structure can be used to allow traffic to other ports as well. To enable access to an HTTP web server, use the following command.<\/p>\n\n\n\nsudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT<\/pre>\n\n\n\nAfter adding all the allowed rules you require, change the input policy to drop.<\/p>\n\n\n\n
Warning:<\/span> Changing the default rule to drop will permit only specifically accepted connection. Make sure you\u2019ve enabled at least SSH as shown above before changing the default rule.<\/p>\n\n\n\nsudo iptables -P INPUT DROP<\/span><\/pre>\n\n\n\nThe same policy rules can be defined for other chains as well by entering the chain name and selecting either DROP or ACCEPT.<\/p>\n\n\n\n
Saving and restoring rules<\/h2>\n\n\n\n
Now if you were to restart your cloud server all of these iptables configurations would be wiped. To prevent this, save the rules to a file.<\/p>\n\n\n\n
sudo iptables-save > \/etc\/sysconfig\/iptables<\/pre>\n\n\n\nYou can then simply restore the saved rules by reading the file you saved.<\/p>\n\n\n\n
# Overwrite the current rules\nsudo iptables-restore < \/etc\/sysconfig\/iptables\n# Add the new rules keeping the current ones\nsudo iptables-restore -n < \/etc\/sysconfig\/iptables<\/pre>\n\n\n\nTo automate the restore at reboot CentOS offers a system service by the same name, iptables. However, it does not come in the default configuration and needs to be installed manually.<\/p>\n\n\n\n
sudo yum install iptables-services<\/pre>\n\n\n\nOnce installed, start and enable the service.<\/p>\n\n\n\n
sudo systemctl start iptables\nsudo systemctl enable iptables<\/pre>\n\n\n\nAfterwards, you can simply save the current rules using the following command.<\/p>\n\n\n\n
sudo service iptables save<\/pre>\n\n\n\nThese 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
Advanced rule setup<\/h2>\n\n\n\n
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 position of the list by inserting them using iptables -I <index><\/em> -command, where the <index><\/em> is the order number you wish to insert the rule. To know which index number to enter, use the following command.<\/p>\n\n\n\nsudo iptables -L --line-numbers<\/pre>\n\n\n\nChain 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\nThe 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 to the top of the chain, use the following command with index number 1.<\/p>\n\n\n\n
sudo iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT<\/pre>\n\n\n\nIf you wish to remove an existing rule from a certain chain, use the delete command with the parameter -D<\/em>. The easiest way to select the rule for deletion is to use the index numbers explained above. For example to delete the second rule on the input chain, use this command<\/p>\n\n\n\nsudo iptables -D INPUT 2<\/pre>\n\n\n\nIt\u2019s also possible to flush all rules of a specific chain or even the whole iptables using the -F<\/em> -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\nWarning:<\/span> Make sure you set the default rule to ACCEPT before flushing any chain.<\/p>\n\n\n\nsudo iptables -P INPUT ACCEPT<\/span><\/pre>\n\n\n\nAfterwards, you can go ahead with clearing 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
# Clear input chain\nsudo iptables -F INPUT\n# Flush the whole iptables\nsudo iptables -F<\/pre>\n\n\n\nWith the iptable flushed, your server could be vulnerable to attacks. Make sure to secure your system with an alternative method while disabling iptables even temporarily.<\/p>\n","protected":false},"featured_media":27372,"comment_status":"open","ping_status":"closed","template":"","community-category":[121,111],"class_list":["post-24631","tutorial","type-tutorial","status-publish","has-post-thumbnail","hentry","community-category-security","community-category-networking"],"acf":[],"_links":{"self":[{"href":"https:\/\/studiogo.tech\/upcloudold\/wp-json\/wp\/v2\/tutorial\/24631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/studiogo.tech\/upcloudold\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/studiogo.tech\/upcloudold\/wp-json\/wp\/v2\/types\/tutorial"}],"replies":[{"embeddable":true,"href":"https:\/\/studiogo.tech\/upcloudold\/wp-json\/wp\/v2\/comments?post=24631"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studiogo.tech\/upcloudold\/wp-json\/wp\/v2\/media\/27372"}],"wp:attachment":[{"href":"https:\/\/studiogo.tech\/upcloudold\/wp-json\/wp\/v2\/media?parent=24631"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/studiogo.tech\/upcloudold\/wp-json\/wp\/v2\/community-category?post=24631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}