在SFTP(SSH File Transfer Protocol)配置中,设置文件传输模式通常涉及以下几个方面:
SFTP支持两种主要的传输模式:
在主动模式下,客户端需要配置防火墙以允许来自服务器的连接。配置示例如下:
# 在客户端的sshd_config文件中
ClientAliveInterval 60
ClientAliveCountMax 3
在被动模式下,服务器需要配置防火墙以允许来自客户端的连接。配置示例如下:
# 在服务器的sshd_config文件中
Port 22
ListenAddress 0.0.0.0
Subsystem sftp internal-sftp
# 配置被动模式的端口范围
Match Group sftpusers
ChrootDirectory %h
ForceCommand internal-sftp
AllowTcpForwarding no
X11Forwarding no
PasswordAuthentication yes
# 在服务器的防火墙配置中(例如iptables)
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m conntrack --ctstate NEW -j ACCEPT
iptables -A INPUT -p tcp --dport 1024:65535 -m state --state NEW -m conntrack --ctstate NEW -j ACCEPT
确保SFTP用户有足够的权限访问和传输文件。可以通过以下命令设置:
# 设置用户主目录的权限
chmod 755 /home/username
# 设置用户主目录下文件的权限
chmod 644 /home/username/*
编辑SFTP服务器的配置文件(通常是/etc/ssh/sshd_config),确保以下配置项正确:
# 启用SFTP子系统
Subsystem sftp internal-sftp
# 限制SFTP用户只能访问其主目录
Match Group sftpusers
ChrootDirectory %h
ForceCommand internal-sftp
AllowTcpForwarding no
X11Forwarding no
PasswordAuthentication yes
保存配置文件后,重启SFTP服务以应用更改:
# 在Debian/Ubuntu系统上
sudo systemctl restart sshd
# 在CentOS/RHEL系统上
sudo systemctl restart sshd
在客户端连接到SFTP服务器时,可以选择传输模式。例如,在使用scp命令时:
# 使用被动模式
scp -P 22 user@server:/path/to/remote/file /local/path
# 使用主动模式(不推荐,除非必要)
scp -P 22 -o "ProxyCommand nc -X connect -x localhost:2222 %h %p" user@server:/path/to/remote/file /local/path
通过以上步骤,你可以有效地配置SFTP以适应不同的网络环境和安全需求。