linux搭建sftp服务器

时间:2021-12-13 12:52:04

转自:http://blog.csdn.net/superswordsman/article/details/49331539

最近工作需要用到sftp服务器,被网上各种方法尤其是权限设置问题搞得晕头转向,现在将自己搭建过程总结了一下,整理出来一种最简单的方法可供大家参考。

第1歩,添加sftp用户并制定根目录:useradd -d  /home/sftp  -s /sbin/nologin sftpuser

第2歩,修改密码:执行passwd sftpuser,然后输入密码2次即可

第3歩,修改配置文件,执行vi /etc/ssh/sshd_config ,在里面添加如下信息:

Subsystem       sftp    internal-sftp

Match User sftpuser
               #X11Forwarding no
              #AllowTcpForwarding no
              #ForceCommand internal-sftp

第4歩,设置根目录的访问权限:chmod 777 /home/sftp(可选)

第5歩,重启sshd进程,使配置生效:service sshd restart

至此,sftp服务器就搭建好了,使用命令sftp sftpuser@10.1.1.100登陆一下,登陆成功说明搭建成功。

注:如果在配置文件里面增加了ChrootDirectory /home/sftp ,那么根目录的拥有者和属组必须是root,此时目录权限应这样设置:

chown root:root /home/sftp

chmod 755 /home/sftp

此时只有用户root对/home/sftp目录有写入权限,而使用sftpuser这个用户登陆后是没有写入权限的,故需要在/home/sftp/目录下再新建一个文件夹,并赋予sftpuser读写权限,这样才能完成sftp操作。

由此可见,在配置文件中增加了ChrootDirectory后,需要特别注意权限问题。

常见问题:

1、sftp登录时报错:

Connecting to 10.66.2.111...
/etc/ssh/ssh_config: line 60: Bad configuration option: UseDNS
/etc/ssh/ssh_config: terminating, 1 bad configuration options
Couldn't read packet: Connection reset by peer

该问题是由dns解析导致,在客户端的配置文件/etc/ssh/ssh_config中查看是否有UseDNS no,如果有,注释掉该行。

2、sftp连接时特别慢,而且警告:reverse mapping checking getaddrinfo for bogon [10.91.0.227] failed - POSSIBLE BREAK-IN ATTEMPT!

可能原因:在sftp服务器上找到配置文件/etc/ssh/sshd_config,将GSSAPIAuthentication yes 改为GSSAPIAuthentication no。