当你在Linux中使用SFTP无法获取目录时,可能是由多种原因导致的。以下是详细的排查和解决方案:
检查目录权限:确保目标目录对SFTP用户有读取权限
ls -ld /path/to/directory
需要至少r-x
权限(例如755)
解决方案:
chmod 755 /path/to/directory
chown user:group /path/to/directory
检查chroot配置:
/etc/ssh/sshd_config
中是否有类似配置:Match Group sftponly
ChrootDirectory /var/sftp
ForceCommand internal-sftp
解决方案:
检查SELinux状态:
sestatus
getenforce
解决方案:
# 临时关闭SELinux
setenforce 0
# 永久关闭(需编辑/etc/selinux/config)
# 或添加正确的SELinux上下文
chcon -R -t public_content_t /path/to/directory
检查sshd_config:
grep -i sftp /etc/ssh/sshd_config
确保有以下配置:
Subsystem sftp internal-sftp
解决方案:
systemctl restart sshd
检查目录是否挂载:
mount | grep /path/to/directory
解决方案:
使用-v参数查看详细输出:
sftp -v user@hostname
查看系统日志:
journalctl -u sshd -f
# 或
tail -f /var/log/auth.log
测试SSH连接:
ssh user@hostname
/bin/false
或/sbin/nologin
(应使用/bin/bash
或/usr/sbin/nologin
)通过以上步骤逐一排查,应该能够解决大多数SFTP无法获取目录的问题。