Block SSH and FTP Access to Specific IP and Network Range
In this article
Yes, you can block SSH and FTP access to specific IP addresses and network ranges using firewall rules, TCP wrappers, or application-level configuration. The most common and effective method is using a firewall like iptables on Linux or Windows Firewall on Windows, where you define rules that deny incoming connections to ports 22 (SSH) and 21 (FTP) from the specified source IPs or CIDR ranges.
Why Block SSH and FTP Access?
SSH and FTP are remote access protocols that, if left open to the entire internet, are constantly probed by automated bots attempting brute-force logins. Restricting access to only trusted IP addresses or internal network ranges dramatically reduces the attack surface. This is a core principle of network security: least privilege. For example, a company might allow SSH access only from its office IP range (e.g., 203.0.113.0/24) and block all other sources. Similarly, FTP (especially plain FTP, which sends credentials in cleartext) should be restricted to specific management hosts or internal networks to prevent credential interception and unauthorized file transfers.
How Firewall Rules Work
Firewalls filter traffic based on a set of ordered rules. Each rule has a match condition (e.g., source IP, destination port, protocol) and an action (ACCEPT, DROP, or REJECT). When a packet arrives, the firewall checks it against the rules in order; the first matching rule determines the packet's fate. For blocking SSH and FTP, you typically create rules that match the source IP or network and the destination port (22 for SSH, 21 for FTP control, and 20 for FTP data). The action is usually DROP (silently discard the packet) or REJECT (send back a connection refused message). DROP is often preferred because it makes the host less visible to port scanners.
Using iptables on Linux
On Linux, iptables is the classic firewall tool. To block a single IP address from accessing SSH, you would run:
iptables -A INPUT -s 192.168.1.100 -p tcp --dport 22 -j DROP
To block an entire network range (e.g., 10.0.0.0/8) from FTP, you would run:
iptables -A INPUT -s 10.0.0.0/8 -p tcp --dport 21 -j DROP
You can also block both protocols at once by specifying multiple ports with the --match multiport option:
iptables -A INPUT -s 203.0.113.0/24 -p tcp --match multiport --dports 21,22 -j DROP
Remember that iptables rules are not persistent by default; you need to save them using a tool like iptables-save or a distribution-specific utility (e.g., netfilter-persistent on Debian/Ubuntu).
Using FirewallD and UFW
Many modern Linux distributions use higher-level firewall tools. With UFW (Uncomplicated Firewall), you can block an IP range from SSH with:
ufw deny from 192.168.1.0/24 to any port 22 proto tcp
With FirewallD, the command is:
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="22" drop'
Then reload the firewall with firewall-cmd --reload. These tools simplify rule management and are often the default on distributions like Ubuntu (UFW) and CentOS/RHEL (FirewallD).
Windows Firewall and Other Platforms
On Windows, you can use Windows Defender Firewall with Advanced Security. Create a new inbound rule, select "Custom," choose TCP and specific local ports (22 and 21), then in the "Scope" section specify the remote IP addresses to block (e.g., a single IP or a range like 192.168.1.0/24). Set the action to "Block the connection." On macOS, the built-in application firewall is less flexible; you would typically use the PF (Packet Filter) firewall via the command line, or use a third-party tool. For network-level blocking, you can also configure access control lists (ACLs) on your router or managed switch.
Application-Level Blocking
Beyond firewalls, you can restrict access at the application level. For SSH, the sshd_config file supports AllowUsers, AllowGroups, DenyUsers, and DenyGroups directives. For example, to only allow SSH from a specific user and IP:
AllowUsers admin@192.168.1.100
For FTP servers like vsftpd or ProFTPD, you can use TCP wrappers (if compiled with support) or configure per-IP access in the server's config file. For example, in vsftpd, you can use tcp_wrappers=YES and then edit /etc/hosts.allow and /etc/hosts.deny to allow or deny specific hosts. This adds a second layer of defense, but it is not a substitute for a firewall because it only applies to the specific service, not to other services on the same port.
Common Mistakes and Pitfalls
- Blocking the wrong port: FTP uses port 21 for control and port 20 for data. If you only block 21, active mode data transfers on port 20 may still pass. Also, SSH can run on a non-standard port; check your config.
- Forgetting IPv6: Many firewalls have separate rules for IPv6. If you only block IPv4, an attacker using an IPv6 address can still reach your service.
- Rule order: In iptables, if you have an ACCEPT rule before your DROP rule, the ACCEPT will match first. Ensure your block rules are placed before any general ACCEPT rules.
- Not testing: After applying rules, test from an allowed and a blocked IP to confirm the behavior. A misconfigured rule can lock you out of your own server.
- Persistence: As mentioned, iptables rules are lost on reboot unless saved. Always make your changes persistent.
Frequently Asked Questions
Q: Can I block SSH and FTP access to a specific IP using a single command?
A: Yes, with iptables you can use the multiport module as shown above, or you can write two separate rules. With UFW, you can add two rules or use a comma-separated list of ports in some versions, but the syntax varies.
Q: What is the difference between DROP and REJECT?
A: DROP silently discards the packet, making the port appear closed or filtered. REJECT sends back an ICMP error or TCP RST, telling the client the connection was refused. DROP is generally more secure because it reveals less information, but it can cause timeouts for legitimate clients that are accidentally blocked.
Q: How do I block a whole range like 192.168.1.0/24?
A: Use CIDR notation in the source field. For example, in iptables: -s 192.168.1.0/24. The same notation works in UFW, FirewallD, and Windows Firewall's remote IP address field.
Q: Will these rules affect existing SSH or FTP sessions?
A: Firewall rules typically only affect new connections. Existing established sessions are usually allowed to continue because of stateful tracking (e.g., ESTABLISHED,RELATED rules). If you want to terminate existing sessions, you need to kill the processes or use a tool like tcpkill.
Q: Can I block SSH and FTP access from a specific country?
A: Blocking by country is not directly possible with standard firewall tools. You would need to use GeoIP databases and a tool like xtables-addons on Linux, or use a third-party service. This is less precise than IP-based blocking and is not recommended as a primary security measure.
Q: What if I only want to allow SSH and FTP from a specific IP and block all others?
A: This is a common "whitelist" approach. You would set a default DROP policy for the ports, then add ACCEPT rules for the specific IPs. For example, in iptables: iptables -A INPUT -p tcp --dport 22 -j DROP followed by iptables -A INPUT -s 192.168.1.100 -p tcp --dport 22 -j ACCEPT. Note the order matters: the ACCEPT rule must come first.