网站搜索

如何在 Linux 中阻止对特定 IP 和网络范围的 SSH 和 FTP 访问


通常我们都经常使用SSHFTP服务来访问远程服务器和虚拟专用服务器。作为 Linux 管理员,您必须了解如何阻止对 Linux 中特定 IP 或网络范围的 SSH 和 FTP 访问,以进一步加强安全性。

  1. 25 个强化 Linux 服务器安全的技巧
  2. 保护 SSH 服务器安全的 5 个有用技巧

本教程将向您展示如何在 CentOS 6 和 7 服务器中阻止对特定 IP 地址和/或网络范围的 SSH 和 FTP 访问。本指南在 CentOS 6.x7.x 版本上进行了测试,但它可能也适用于其他 Linux 发行版,例如 Debian、Ubuntu 和 SUSE/openSUSE 等。

我们将通过两种方法来完成。第一种方法是使用 IPTables/firewallD,第二种方法是在 hosts.allow 的帮助下使用 TCP 包装器> 和 hosts.deny 文件。

请参阅以下指南以了解有关 IPTables 和 Firewalld 的更多信息。

  1. IPTables(Linux 防火墙)提示/命令基本指南
  2. 如何设置 Iptables 防火墙以启用对 Linux 中服务的远程访问
  3. 如何在 RHEL/CentOS 7 和 Fedora 21 中配置“FirewallD”
  4. 在 Linux 中配置和管理防火墙的有用“FirewallD”规则

现在您知道什么是 IPTablesFirewallD 及其基础知识。

方法 1:使用 IPTables/FirewallD 阻止 SSH 和 FTP 访问

现在让我们看看如何阻止对特定 IP(例如 192.168.1.100)和/或网络范围(例如 192.168.1.0/24)的 SSH 和 FTP 访问在 RHEL/CentOS/Scientific Linux 6.x 版本上使用 IPtables,在 CentOS 7.x 上使用 FirewallD

阻止或禁用 SSH 访问

--------------------- On IPtables Firewall ---------------------
iptables -I INPUT -s 192.168.1.100 -p tcp --dport ssh -j REJECT
iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport ssh -j REJECT
--------------------- On FirewallD ---------------------
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -m tcp --source 192.168.1.100 -p tcp --dport 22 -j REJECT
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -m tcp --source 192.168.1.100/24 -p tcp --dport 22 -j REJECT

要使新规则生效,您需要使用以下命令。

service iptables save         [On IPtables Firewall]
firewall-cmd --reload         [On FirewallD]

现在,尝试从被阻止的主机SSH服务器。请注意,这里的192.168.1.150是被阻止的主机。

ssh 192.168.1.150

您应该看到以下消息。

ssh: connect to host 192.168.1.150 port 22: Connection refused

取消阻止或启用 SSH 访问

要取消阻止或启用 SSH 访问,请转到远程服务器并运行以下命令:

--------------------- On IPtables Firewall ---------------------
iptables -I INPUT -s 192.168.1.100 -p tcp --dport ssh -j ACCEPT
iptables -I INPUT -s 192.168.1.100/24 -p tcp --dport ssh -j ACCEPT
--------------------- On FirewallD ---------------------
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -m tcp --source 192.168.1.100 -p tcp --dport 22 -j ACCEPT
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -m tcp --source 192.168.1.100/24 -p tcp --dport 22 -j ACCEPT

使用以下命令保存更改,通过 SSH 访问您的服务器。

service iptables save         [On IPtables Firewall]
firewall-cmd --reload         [On FirewallD]

阻止或禁用 FTP 访问

通常,FTP 的默认端口为 2021。因此,要使用 IPTables 阻止所有 FTP 流量,请运行以下命令:

--------------------- On IPtables Firewall ---------------------
iptables -I INPUT -s 192.168.1.100 -p tcp --dport 20,21 -j REJECT
iptables -I INPUT -s 192.168.1.100/24 -p tcp --dport 20,21 -j REJECT
--------------------- On FirewallD ---------------------
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -m tcp --source 192.168.1.100 -p tcp --dport 20,21 -j REJECT
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -m tcp --source 192.168.1.100/24 -p tcp --dport 20,21 -j REJECT

要使新规则生效,您需要使用以下命令。

service iptables save         [On IPtables Firewall]
firewall-cmd --reload         [On FirewallD]

现在,尝试使用以下命令从被阻止的主机(192.168.1.100)访问服务器:

ftp 192.168.1.150

您将收到如下所示的错误消息。

ftp: connect: Connection refused

取消阻止或启用 FTP 访问

要取消阻止并重新启用 FTP 访问,请运行:

--------------------- On IPtables Firewall ---------------------
iptables -I INPUT -s 192.168.1.100 -p tcp --dport 20,21 -j ACCEPT
iptables -I INPUT -s 192.168.1.100/24 -p tcp --dport 20,21 -j ACCEPT
--------------------- On FirewallD ---------------------
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -m tcp --source 192.168.1.100 -p tcp --dport 20,21 -j ACCEPT
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -m tcp --source 192.168.1.100/24 -p tcp --dport 20,21 -j ACCEPT

使用命令保存更改:

service iptables save         [On IPtables Firewall]
firewall-cmd --reload         [On FirewallD]

现在,尝试通过 FTP 访问服务器:

ftp 192.168.1.150

输入您的 ftp 用户名和密码。

Connected to 192.168.1.150.
220 Welcome to TecMint FTP service.
Name (192.168.1.150:sk): tecmint
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> 

方法 2:使用 TCP 包装器阻止 SSH 和 FTP 访问

如果您不想干扰 IPTablesFirewallD,那么 TCP 包装器 是阻止 SSH 和 FTP 访问特定对象的更好方法。 IP 和/或网络范围。

OpenSSH 和 FTP 编译时支持 TCP 包装器,这意味着您可以在以下两个重要文件中指定允许连接哪些主机而不触及防火墙,它们是:

  1. /etc/hosts.allow
  2. /etc/hosts.deny

顾名思义,第一个文件包含允许的主机的条目,第二个文件包含阻止的主机的地址。

例如,让我们阻止对 IP 地址为 192.168.1.100 和网络范围为 192.168.1.0 的主机的 SSH 和 FTP 访问。此方法对于 CentOS 6.x 和 7.x 系列相同。当然,它也适用于其他发行版,例如 Debian、Ubuntu、SUSE、openSUSE 等。

打开 /etc/hosts.deny 文件并添加您想要阻止的以下 IP 地址或网络范围,如下所示。

##### To block SSH Access #####
sshd: 192.168.1.100
sshd: 192.168.1.0/255.255.255.0

##### To block FTP Access #####
vsftpd: 192.168.1.100
vsftpd: 192.168.1.0/255.255.255.0

保存并退出文件。

现在,重新启动 sshd 和 vsftpd 服务以使新更改生效。

--------------- For SSH Service ---------------
service sshd restart        [On SysVinit]
systemctl restart sshd      [On SystemD]
--------------- For FTP Service ---------------
service vsftpd restart        [On SysVinit]
systemctl restart vsftpd      [On SystemD]

现在,尝试通过 SSH 连接服务器或从被阻止的主机。

ssh 192.168.1.150

您将看到以下输出:

ssh_exchange_identification: read: Connection reset by peer

现在,尝试通过 FTP 连接服务器或从被阻止的主机。


ftp 192.168.1.150

您将看到以下输出:

Connected to 192.168.1.150.
421 Service not available.

要再次取消阻止或启用 SSH 和 FTP 服务,请编辑 hosts.deny 文件并注释掉所有行,最后重新启动 vsftpd 和 sshd 服务。

结论

目前为止就这样了。总而言之,今天我们学习了如何使用 IPTables、FirewallD 和 TCP 包装器来阻止特定的 IP 地址和网络范围。这些方法非常简单明了。

即使是新手 Linux 管理员也可以在几分钟内完成此操作。如果您知道其他阻止 SSH 和 FTP 访问的方法,请随时在评论部分分享。并且不要忘记在您的所有社交网络中分享我们的文章。