在CentOS上启用SFTP(SSH File Transfer Protocol)通常涉及几个步骤,包括安装必要的软件包、配置SSH服务器以及设置SFTP用户权限。以下是在CentOS 7和CentOS 8上启用SFTP的基本步骤:
安装OpenSSH服务器: 打开终端并运行以下命令来安装OpenSSH服务器:
sudo yum install openssh-server
启动并启用SSH服务: 安装完成后,启动SSH服务并设置为开机自启:
sudo systemctl start sshd
sudo systemctl enable sshd
配置SFTP:
默认情况下,OpenSSH服务器已经配置了SFTP。如果你需要自定义SFTP配置,可以编辑/etc/ssh/sshd_config文件:
sudo vi /etc/ssh/sshd_config
在文件中找到或添加以下行来启用SFTP子系统:
Subsystem sftp /usr/libexec/openssh/sftp-server
确保没有注释掉这一行(即前面没有#号)。
重启SSH服务: 保存并关闭文件后,重启SSH服务以应用更改:
sudo systemctl restart sshd
设置SFTP用户权限:
为了安全起见,你可能想要限制SFTP用户的访问权限。可以通过编辑/etc/ssh/sshd_config文件来实现:
Match Group sftpusers
ChrootDirectory %h
ForceCommand internal-sftp
AllowTcpForwarding no
X11Forwarding no
然后创建一个名为sftpusers的用户组,并将需要限制的SFTP用户添加到这个组中:
sudo groupadd sftpusers
sudo usermod -aG sftpusers your_username
最后,确保用户的家目录权限设置正确:
sudo chown root:root /home/your_username
sudo chmod 755 /home/your_username
CentOS 8的步骤与CentOS 7非常相似,因为它们都使用systemd来管理服务。
安装OpenSSH服务器:
sudo dnf install openssh-server
启动并启用SSH服务:
sudo systemctl start sshd
sudo systemctl enable sshd
配置SFTP:
编辑/etc/ssh/sshd_config文件,确保SFTP子系统已启用:
sudo vi /etc/ssh/sshd_config
添加或确保以下行存在:
Subsystem sftp /usr/libexec/openssh/sftp-server
重启SSH服务:
sudo systemctl restart sshd
设置SFTP用户权限:
与CentOS 7相同,编辑/etc/ssh/sshd_config文件来限制SFTP用户的访问,并创建相应的用户组。
完成这些步骤后,你应该能够在CentOS上使用SFTP了。记得在尝试连接之前,防火墙设置允许SSH(默认端口22)通信。