{"id":174,"date":"2025-03-27T09:45:33","date_gmt":"2025-03-27T07:45:33","guid":{"rendered":"https:\/\/www.24x7serverguard.com\/blog\/?p=174"},"modified":"2025-03-27T09:45:33","modified_gmt":"2025-03-27T07:45:33","slug":"how-to-block-ftp-access-using-iptables-and-csf-firewall","status":"publish","type":"post","link":"https:\/\/www.24x7serverguard.com\/blog\/ftp-issues\/how-to-block-ftp-access-using-iptables-and-csf-firewall\/","title":{"rendered":"How to Block FTP Access Using IPTables and CSF Firewall"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Securing your server by restricting FTP access is essential for preventing unauthorized access. Here\u2019s how you can block FTP (port 21) using <strong>IPTables (default system firewall)<\/strong> and <strong>CSF (ConfigServer Security &amp; Firewall)<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Block FTP Access Using IPTables<\/strong><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">1. Completely Disable FTP Access<br><strong>To block all FTP connections on the server, run:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">iptables -A INPUT -p tcp &#8211;dport 21 -j DROP<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. Block FTP Access for a Specific IP<br><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to deny FTP access for a specific IP (e.g., 10.10.10.10), use:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">iptables -A INPUT -p tcp -s 10.10.10.10 --dport 21 -j DROP<\/mark><br><br><strong>3. Block FTP Access for a Subnet<br><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><br><\/mark><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To block FTP access for an entire subnet (e.g., 10.10.10.0\/24), use:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">iptables -I INPUT -p tcp -s 10.10.10.0\/24 --dport 21 -j DROP<\/mark><br><br><strong>4. Save and Apply IPTables Rules<\/strong><br>After adding the rules, save them:<br><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\/etc\/init.d\/iptables save<br><\/mark><br>Then restart IPTables to apply changes:<br><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\/etc\/init.d\/iptables restart<br><\/mark><br><strong><br><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><br><\/mark><strong>Block FTP Access Using CSF Firewall<br><\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. Completely Disable FTP Access<\/strong><br>Edit the CSF configuration file:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">vi \/etc\/csf\/csf.conf<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Find the line:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"># Allow incoming TCP ports<br>TCP_IN =<\/mark><br><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Remove port <code>21<\/code> from the list, save the file, and restart CSF:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">csf -r<br><\/mark><br><strong>2. Block FTP Access for a Specific IP<\/strong><br>Edit the csf.deny file:<br><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">vi \/etc\/csf\/csf.deny<br><\/mark><br>Add the following line:<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">tcp:in:d=21:s=10.10.10.10<br><\/mark><br>Save the file and restart CSF:<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">csf -r<\/mark><br><br><strong>3. Allow FTP Access for a Specific IP and Deny All Others<br><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><br><\/mark>To allow FTP for a specific IP while blocking all others:<br><br>i) Edit the csf.conf file:<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">vi \/etc\/csf\/csf.conf<br><\/mark><br>Remove port 21 from:<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"># Allow incoming TCP ports<br><\/mark><br><br>Remove port 21 from:<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"># Allow outgoing TCP ports<br><\/mark><br>Save the file.<br><br>ii) Edit the csf.allow file:<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">vi \/etc\/csf\/csf.allow<br><\/mark><br>Add this entry:<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">tcp:in:d=21:s=10.10.10.10<\/mark><br><br>Save the file and restart CSF:<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">csf -r<\/mark><br><br><br><strong>Conclusion<\/strong><br>By following these steps, you can secure your server by blocking FTP access entirely, restricting it to specific IPs, or allowing FTP access only for trusted sources. Always test firewall rules to avoid accidental lockouts and ensure smooth server operation.<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><br><br><\/mark><br><br><strong><br><\/strong><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Securing your server by restricting FTP access is essential for preventing unauthorized access. Here\u2019s how you can block FTP (port 21) using IPTables (default system firewall) and CSF (ConfigServer Security &amp; Firewall). Block FTP Access Using IPTables 1. Completely Disable FTP AccessTo block all FTP connections on the server, run: iptables -A INPUT -p tcp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[114,115,60,117,116],"class_list":["post-174","post","type-post","status-publish","format-standard","hentry","category-ftp-issues","tag-block-ftp-access-using-firewall","tag-dubai","tag-ftp","tag-iptable","tag-server-management"],"_links":{"self":[{"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/posts\/174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/comments?post=174"}],"version-history":[{"count":1,"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/posts\/174\/revisions"}],"predecessor-version":[{"id":175,"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/posts\/174\/revisions\/175"}],"wp:attachment":[{"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/media?parent=174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/categories?post=174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.24x7serverguard.com\/blog\/wp-json\/wp\/v2\/tags?post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}