SFTP(SSH File Transfer Protocol)是一种安全的文件传输协议,它基于SSH(Secure Shell)进行数据传输。要限制SFTP访问的IP地址,可以通过以下几种方法实现:
sshd_config
)编辑SSH服务器的配置文件/etc/ssh/sshd_config
,添加或修改以下配置项:
Match Address 192.168.1.1,192.168.1.2
ForceCommand internal-sftp
PasswordAuthentication yes
ChrootDirectory /home/%u
在这个例子中,只有来自192.168.1.1
和192.168.1.2
的IP地址可以访问SFTP。ForceCommand internal-sftp
强制使用SFTP命令,PasswordAuthentication yes
允许密码认证,ChrootDirectory /home/%u
将用户的根目录限制在其主目录下。
你可以使用iptables或firewalld等防火墙工具来限制SFTP访问的IP地址。
# 允许来自特定IP的SFTP连接
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.1 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.2 -j ACCEPT
# 拒绝其他所有IP的SFTP连接
iptables -A INPUT -p tcp --dport 22 -j DROP
# 允许来自特定IP的SFTP连接
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.1" port protocol="tcp" port="22" accept'
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.2" port protocol="tcp" port="22" accept'
# 重新加载防火墙规则
firewall-cmd --reload
你可以使用PAM模块来限制SFTP访问的IP地址。编辑/etc/pam.d/sshd
文件,添加以下行:
auth required pam_listfile.so item=user sense=allow file=/etc/ssh/allowed_ips onerr=succeed
然后创建/etc/ssh/allowed_ips
文件,并添加允许访问的IP地址:
192.168.1.1
192.168.1.2
如果你使用的是SELinux,可以通过配置SELinux策略来限制SFTP访问的IP地址。编辑/etc/selinux/config
文件,设置SELINUX
为enforcing
模式,然后使用semanage
命令添加IP地址限制:
# 安装policycoreutils-python-tools包
yum install policycoreutils-python-tools
# 添加IP地址限制
semanage login -a -s sftp_user -r sftp_t -i 192.168.1.1
semanage login -a -s sftp_user -r sftp_t -i 192.168.1.2
在这个例子中,sftp_user
是SFTP用户的SELinux用户,sftp_t
是SFTP用户的类型。
通过以上方法,你可以有效地限制SFTP访问的IP地址,提高系统的安全性。