在 CentOS 上实现 SFTP 文件共享,通常需要以下几个步骤:
-
安装 OpenSSH 服务器:
首先,确保你的 CentOS 系统上已经安装了 OpenSSH 服务器。如果没有安装,可以使用以下命令进行安装:
sudo yum install openssh-server
-
配置 OpenSSH 服务器:
编辑
/etc/ssh/sshd_config
文件,确保以下配置项已经启用(删除或注释掉前面的#
):PubkeyAuthentication yes PasswordAuthentication yes AllowUsers your_username
将
your_username
替换为你想要允许访问 SFTP 的用户名。保存文件并重启 OpenSSH 服务:
sudo systemctl restart sshd
-
创建一个用于共享的目录:
创建一个新的目录,用于存储共享文件。例如,创建一个名为
shared_files
的目录:sudo mkdir /home/your_username/shared_files sudo chown your_username:your_username /home/your_username/shared_files sudo chmod 755 /home/your_username/shared_files
将
your_username
替换为你想要允许访问 SFTP 的用户名。 -
配置 SFTP 用户:
为需要访问共享文件的 SFTP 用户创建一个新的系统用户(如果还没有的话):
sudo useradd -m your_username sudo passwd your_username
按照提示设置用户的密码。
-
设置用户权限:
将新创建的 SFTP 用户添加到
shared_files
目录的所有者组:sudo usermod -aG shared_files_group your_username
将
shared_files_group
替换为一个自定义的组名,例如sftp_users
。更改
shared_files
目录的组权限:sudo chmod 2770 /home/your_username/shared_files sudo chown :shared_files_group /home/your_username/shared_files
-
重启 OpenSSH 服务:
重新启动 OpenSSH 服务以应用更改:
sudo systemctl restart sshd
现在,SFTP 用户应该能够访问和共享 /home/your_username/shared_files
目录中的文件。要连接到 SFTP 服务器,可以使用支持 SFTP 的客户端(如 FileZilla、WinSCP 等)并输入以下信息:
- 主机名:你的 CentOS 服务器的 IP 地址或域名
- 端口:22(默认的 SSH 端口)
- 用户名:你的 SFTP 用户名
- 密码:你的 SFTP 用户密码
连接成功后,你就可以在 SFTP 客户端中浏览和共享文件了。