在CentOS系统中,您可以通过配置SSH服务器来限制SFTP访问的IP地址。以下是具体步骤:
sshd_config
文件编辑SSH配置文件
打开SSH配置文件 /etc/ssh/sshd_config
:
sudo vi /etc/ssh/sshd_config
添加或修改Match
块
在文件中找到或添加一个Match
块来指定允许访问的IP地址。例如:
Match Address 192.168.1.100,192.168.1.101
ForceCommand internal-sftp
PasswordAuthentication yes
ChrootDirectory /home/%u
AllowTcpForwarding no
X11Forwarding no
这个配置表示只有IP地址为192.168.1.100
和192.168.1.101
的用户可以访问SFTP,并且强制使用内部SFTP命令,禁止TCP和X11转发。
重启SSH服务 保存并退出编辑器后,重启SSH服务以应用更改:
sudo systemctl restart sshd
您也可以通过配置防火墙(如firewalld
)来限制SFTP访问的IP地址。
安装firewalld
(如果尚未安装)
sudo yum install firewalld
启动并启用firewalld
sudo systemctl start firewalld
sudo systemctl enable firewalld
添加允许访问的IP地址
使用firewall-cmd
命令添加允许访问SFTP的IP地址:
sudo firewall-cmd --permanent --zone=trusted --add-source=192.168.1.100
sudo firewall-cmd --permanent --zone=trusted --add-source=192.168.1.101
重新加载防火墙规则
sudo firewall-cmd --reload
如果您的系统启用了SELinux,您还可以通过SELinux策略来进一步限制SFTP访问。
安装SELinux管理工具
sudo yum install policycoreutils-python
创建自定义SELinux策略模块
编辑或创建一个自定义SELinux策略模块文件,例如/etc/selinux/targeted/contexts/files/file_contexts.local
:
sudo vi /etc/selinux/targeted/contexts/files/file_contexts.local
添加以下内容:
/home/.ssh(/.*)? --> ssh_home_t:s0
重新加载SELinux策略
sudo restorecon -Rv /home/.ssh
通过以上方法,您可以有效地限制CentOS系统中SFTP访问的IP地址。选择适合您需求的方法进行配置即可。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:如何限制CentOS SFTP访问IP