温馨提示×

centos怎样开启sftp服务

小樊
36
2026-10-03 00:56:18
栏目: 智能运维

在 CentOS 上开启 SFTP 服务,实际上 SFTP 是依赖于 OpenSSH 的,不需要单独安装“SFTP 服务”,只要 SSH 服务正常即可。下面是完整步骤(以 CentOS 7 / 8 / Stream 为例):


一、确认并安装 OpenSSH

1. 检查是否安装

rpm -qa | grep openssh

如果没有,安装:

yum install -y openssh-server

(CentOS 8 / Stream 可用 dnf)


二、启动并开启 SSH 服务

2. 启动 sshd

systemctl start sshd

3. 设置开机自启

systemctl enable sshd

4. 查看状态

systemctl status sshd

看到 active (running) 就说明正常。


三、确认 SFTP 子服务已启用(默认就有)

SFTP 由 /etc/ssh/sshd_config 中的这一行控制:

Subsystem sftp /usr/libexec/openssh/sftp-server

检查:

grep sftp /etc/ssh/sshd_config

如果该行被注释(前面有 #),取消注释并重启 sshd:

systemctl restart sshd

四、防火墙放行 SSH(SFTP 走 22 端口)

firewall-cmd --permanent --add-service=ssh
firewall-cmd --reload

(如果是云服务器,还需在安全组放行 22 端口)


五、测试 SFTP

在客户端执行:

sftp user@服务器IP

或本地测试:

sftp localhost

六、限制用户只能使用 SFTP(可选)

如果你希望 某些用户只能 SFTP,不能 SSH 登录:

1. 修改 sshd_config

Match User sftpuser
    ChrootDirectory /home/sftpuser
    ForceCommand internal-sftp
    X11Forwarding no
    AllowTcpForwarding no

2. 设置目录权限(非常重要)

chown root:root /home/sftpuser
chmod 755 /home/sftpuser
mkdir /home/sftpuser/upload
chown sftpuser:sftpuser /home/sftpuser/upload

3. 重启 sshd

systemctl restart sshd

七、常见问题

  • 连不上:检查 22 端口、防火墙、SELinux
  • 权限拒绝:Chroot 目录必须属主为 root
  • SFTP 慢:关闭 DNS 反向解析
    UseDNS no
    

如果你告诉我:

  • CentOS 版本
  • 是否要 限制用户只能 SFTP
  • 是否使用 非 22 端口

我可以给你更精确的配置示例。

0 踩