{"id":1555,"date":"2019-04-17T11:23:01","date_gmt":"2019-04-17T05:53:01","guid":{"rendered":"https:\/\/www.coimbatorewebhosting.com\/blog\/?p=1555"},"modified":"2019-05-15T16:37:53","modified_gmt":"2019-05-15T11:07:53","slug":"firewall-rules-commands-iptables","status":"publish","type":"post","link":"https:\/\/coimbatorewebhosting.com\/blog\/firewall-rules-commands-iptables\/","title":{"rendered":"Linux Common Firewall Rules and Commands in iptables"},"content":{"rendered":"<h4 class=\"fittexted_for_content_h4\">Linux Common Firewall Rules and Commands in iptables<\/h4>\n<p>What is firewall?<\/p>\n<p>A firewall is a network security device that monitors incoming and outgoing network traffic and decides whether to allow or block specific traffic based on a defined set of security rules.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1557 size-full\" src=\"https:\/\/www.coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2019\/04\/iptables.jpg\" alt=\"Firewall Rules\" width=\"650\" height=\"300\" \/><\/p>\n<p>Here we are going to show you some linux common <a href=\"https:\/\/www.cisco.com\/c\/en_in\/index.html\" target=\"_blank\" rel=\"noopener nofollow\">firewall<\/a> rules and commands in iptables. Iptables is a useful command line utility for configuring Linux kernel firewall. Iptables contains five tables: raw, filter, nat, mangle and security. Each table consist of chains. A chain is a list of firewall rules which are followed in order. Let\u2019s get started with some common firewall rules and commands in iptables.<\/p>\n<p>Log in to your VPS via SSH as <a href=\"https:\/\/www.coimbatorewebhosting.com\/blog\/how-to-install-zabbix-on-centos\/\" title=\"user\" alt=\"user\">user<\/a> root<\/p>\n<pre>ssh root@IP_Address -p Port_number<\/pre>\n<pre>ssh root@123.12.122.141 -p 22<\/pre>\n<p>Installing iptables is very easy. If you have an Ubuntu VPS or a Debian VPS, run the following commands:<\/p>\n<pre>apt-get update\r\napt-get upgrade\r\napt-get install iptables iptables-persistent<\/pre>\n<p>If there is CentOS\u00a0 installed on your VPS, run the following commands:<\/p>\n<pre>yum clean all\r\nyum update\r\nyum install iptables<\/pre>\n<p>That\u2019s it, now you should have successfully installed iptables on your server.<\/p>\n<p><strong>Now, Lets see the common firewall rules in iptables<\/strong><\/p>\n<p>Listed below are examples about common firewall rules.<br \/>\nAccept all ESTABLISHED and RELATED packets:<\/p>\n<pre>iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT<\/pre>\n<p>Allow HTTP and HTTPS connections from anywhere:<\/p>\n<pre>iptables -A INPUT -p tcp --dport 80 -j ACCEPT\r\niptables -A INPUT -p tcp --dport 443 -j ACCEPT<\/pre>\n<p>Allow access on port 21 from a specific IP address only (e.g. 192.168.1.111) and block access from all other IPs to the server (e.g. server IP 192.168.1.100) :<\/p>\n<pre>iptables -A INPUT -s 192.168.1.111 -d 192.168.1.100 -p tcp -m state --state NEW -m <a href=\"https:\/\/www.coimbatorewebhosting.com\/blog\/how-to-configure-pop3-imap-email-account-in-thunderbird\/\" title=\"tcp\" alt=\"tcp\">tcp<\/a> --dport 21 -j ACCEPT\r\niptables -A INPUT -d 192.168.1.100 -p tcp -m tcp --dport 21 -j DROP\r\niptables-save<\/pre>\n<p>Block an IP address (e.g. 192.168.1.19):<\/p>\n<pre>iptables -A INPUT -s 192.168.1.19 -j DROP<\/pre>\n<p>Block an IP range and reject all packets (e.g. 192.168.1.0\/24):<\/p>\n<pre>iptables -A INPUT -s 192.168.1.0\/24 -j REJECT<\/pre>\n<p>To block outgoing traffic to a port, (e.g. port 123), use:<\/p>\n<pre>iptables -A OUTPUT -p tcp --dport 123 -j DROP<\/pre>\n<p><strong>Common iptables commands<\/strong><\/p>\n<p>List all rules in all chains in verbose mode and display the IP addresses and port numbers instead host names and services, including the interface name, the rule options (if any), and the TOS masks:<\/p>\n<pre>iptables -nvL | less<\/pre>\n<pre>Chain INPUT (policy ACCEPT 17M packets, 3161M bytes)\r\n pkts bytes target     prot opt          in     out     source               destination\r\n  90M   18G cP-Firewall-1-INPUT  all  --  *      *       0.0.0.0\/0            0.0.0.0\/0\r\n\r\nChain FORWARD (policy ACCEPT 0 packets, 0 bytes)\r\n pkts bytes target     prot opt           in     out     source               destination\r\n    0     0 cP-Firewall-1-INPUT  all  --  *      *       0.0.0.0\/0            0.0.0.0\/0\r\n\r\nChain OUTPUT (policy ACCEPT 16M packets, 5107M bytes)\r\n pkts bytes target     prot opt in     out  source     destination\r\n    0     0 ACCEPT     tcp  --  *      *   0.0.0.0\/0  0.0.0.0\/0  multiport dports 25,465,587 owner GID match 32006\r\n18618 9100K ACCEPT     tcp  --  *      *   0.0.0.0\/0  0.0.0.0\/0  multiport dports 25,465,587 owner GID match 12\r\n    0     0 ACCEPT     tcp  --  *      *   0.0.0.0\/0  127.0.0.1  multiport dports 25,465,587 owner UID match 32001\r\n10686  946K ACCEPT     tcp  --  *      *   0.0.0.0\/0  0.0.0.0\/0  multiport dports 25,465,587 owner UID match 0\r\n\r\nChain cP-Firewall-1-INPUT (2 references)\r\n pkts bytes target     prot opt in     out     source               destination\r\n   39  2264 ACCEPT     tcp  --  *      *       0.0.0.0\/0            0.0.0.0\/0           state NEW tcp dpt:993\r\n   54  2872 ACCEPT     tcp  --  *      *       0.0.0.0\/0            0.0.0.0\/0           state NEW tcp dpt:53\r\n 7509  450K ACCEPT     tcp  --  *      *       0.0.0.0\/0            0.0.0.0\/0           state NEW tcp dpt:21\r\n 557K   34M ACCEPT     tcp  --  *      *       0.0.0.0\/0            0.0.0.0\/0           state <a href=\"https:\/\/www.coimbatorewebhosting.com\/blog\/change-windows-10-dns-servers\/\" title=\"NEW tcp\" alt=\"NEW tcp\">NEW tcp<\/a> dpt:443\r\n19655 1142K ACCEPT     tcp  --  *      *       0.0.0.0\/0            0.0.0.0\/0           state NEW tcp dpt:80\r\n 1057 43388 ACCEPT     tcp  --  *      *       0.0.0.0\/0            0.0.0.0\/0           state NEW tcp dpt:8080\r\n 7533  452K ACCEPT     tcp  --  *      *       0.0.0.0\/0            0.0.0.0\/0           state NEW tcp dpt:143\r\n  382 16664 ACCEPT     tcp  --  *      *       0.0.0.0\/0            0.0.0.0\/0           state NEW tcp dpt:22\r\n2871K  173M ACCEPT     tcp  --  *      *       0.0.0.0\/0            0.0.0.0\/0           state NEW tcp dpt:995\r\n23539 1284K ACCEPT     tcp  --  *      *       0.0.0.0\/0            0.0.0.0\/0           state NEW tcp dpt:110\r\n 8353  500K ACCEPT     tcp  --  *      *       0.0.0.0\/0            0.0.0.0\/0           state NEW tcp dpt:25\r\n   71  3680 ACCEPT     tcp  --  *      *       0.0.0.0\/0            0.0.0.0\/0           state NEW tcp dpt:465\r\n 519K   31M ACCEPT     tcp  --  *      *       0.0.0.0\/0            0.0.0.0\/0           state NEW tcp dpt:3306\r\n  132  9948 ACCEPT     udp  --  *      *       0.0.0.0\/0            0.0.0.0\/0           state NEW udp dpt:53\r\n\r\n\r\n<\/pre>\n<p>To display rules in chains with rule numbers, use:<\/p>\n<pre>iptables -nvL --line-numbers<\/pre>\n<p>This is useful if you want to delete a rule (e.g. delete rule number 9 from the INPUT chain):<\/p>\n<pre>iptables -D INPUT 9<\/pre>\n<p>Or, add a rule between two existing rules (e.g. add a firewall rule between rules number 2 and 3):<\/p>\n<pre>iptables -I OUTPUT 3 -d 127.0.0.1\/32 -p tcp -m multiport --dports 25,465,587 -m owner --uid-owner 201 -j ACCEPT\r\n\r\n<\/pre>\n<p>In order to list all commands that were used to create the currently used iptables rules, use the following command:<\/p>\n<pre>iptables -S\r\n\r\n<\/pre>\n<p>This command is useful if you need to edit or delete some firewall rules.<\/p>\n<pre>-P INPUT ACCEPT\r\n-P FORWARD ACCEPT\r\n-P OUTPUT ACCEPT\r\n-N cP-Firewall-1-INPUT\r\n-A INPUT -j cP-Firewall-1-INPUT\r\n-A FORWARD -j cP-Firewall-1-INPUT\r\n-A OUTPUT -p tcp -m multiport --dports 25,465,587 -m owner --gid-owner mailman -j ACCEPT\r\n-A OUTPUT -p tcp -m multiport --dports 25,465,587 -m owner --gid-owner mail -j ACCEPT\r\n-A OUTPUT -d 127.0.0.1\/32 -p tcp -m multiport --dports 25,465,587 -m owner --uid-owner cpanel -j ACCEPT\r\n-A OUTPUT -p tcp -m multiport --dports 25,465,587 -m owner --uid-owner root -j ACCEPT\r\n-A cP-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 993 -j ACCEPT\r\n-A cP-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 53 -j ACCEPT\r\n-A cP-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT\r\n-A cP-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT\r\n-A cP-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT\r\n-A cP-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT\r\n-A cP-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 143 -j ACCEPT\r\n-A cP-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT\r\n-A cP-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 995 -j ACCEPT\r\n-A cP-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 110 -j ACCEPT\r\n-A cP-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 25 -j ACCEPT\r\n-A cP-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 465 -j ACCEPT\r\n-A cP-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT\r\n-A cP-Firewall-1-INPUT -p udp -m state --state NEW -m udp --dport 53 -j ACCEPT\r\n<\/pre>\n<p><strong>Clear all firewall rules:<\/strong><\/p>\n<pre>iptables -F\r\n<\/pre>\n<p>Use \u2018iptables -h | less\u2019 for more information on all iptables command options.<\/p>\n<p>Hope this article helps you, please share your valuable comments to improve us.<\/p>\n<p>To setup the Firewall on Dedicated Linux Server <a href=\"https:\/\/www.coimbatorewebhosting.com\/blog\/setup-firewall-on-linux-server\/\">click here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux Common Firewall Rules and Commands in iptables What is firewall? A firewall is a network security device that monitors incoming and outgoing network traffic and decides whether to allow or block specific traffic based on a defined set of security rules. Here we are going to show you some [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1560,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[17,3],"tags":[162,163],"class_list":["post-1555","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general","category-linux","tag-firewall-rules","tag-iptables"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2019\/04\/iptables-1.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pa2YU7-p5","_links":{"self":[{"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/comments?post=1555"}],"version-history":[{"count":0,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1555\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/media\/1560"}],"wp:attachment":[{"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=1555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=1555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=1555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}