一、sftp概述
sftp(Secure File Transfer Protocol)是安全文件传输协议的缩写,是一种安全的文件传送协议,可以为传输文件提供一种安全的加密方法。是ssh内含协议,也就是说只要sshd服务器启动了,sftp就可使用,不需要额外安装,它的默认端口和SSH一样为22。
SFTP本身没有单独的守护进程,它必须使用SSHD守护进程(端口号默认是22)来完成相应的连接操作,所以从某种意义上来说,SFTP并不像一个服务器程序,而更像是一个客户端程序。
注:搭建SFTP文件服务器不需求安装什么额外的包,除了SSH,ssh需要OpenSSH4.8p1以后的版本。因为要使用chroot设置根目录(用户能看见和访问的根目录)
sftp通过使用加密/解密技术来保障传输文件的安全性,因此sftp的传输效率比普通的FTP要低,但sftp的安全性要比ftp高,因此sftp通常用于报表、对账单等对安全性要求较高的场景。
二、与ssh相关
ssh协议本身提供了两个服务器功能:
一个就是远程连接使用shell的服务器,即俗称的ssh。
一个就是类似FTP服务器的sftp-server,提供更安全的FTP服务。
SFTP可以从远程服务器上下载/上传文件,使用的是SSH的通道(port 22)。
三、sftp与ftp的区别
FTP是一种文件传输协议,一般是为了方便数据共享的。包括一个FTP服务器和多个FTP客户端.FTP客户端通过FTP协议在服务器上下载资源。
而SFTP协议是在FTP的基础上对数据进行加密,使得传输的数据相对来说更安全。
但是这种安全是以牺牲效率为代价的,也就是说SFTP的传输效率比FTP要低(不过现实使用当中,没有发现多大差别)
FTP要安装,SFTP不要安装;SFTP更安全,但更安全带来副作用就是的效率比FTP要低些。
四、linux下搭建sftp
4.1创建用户、用户组以及密码
4.1.1 创建sftp用户组
[root@catyuan ~]# groupadd sftp
4.1.2创建sftp用户test,不允许其登录操作系统
[root@catyuan ~]# useradd -g sftp -s /bin/flase test
注意:
/bin/false是最严格的禁止login选项,一切服务都不能用,
/sbin/nologin只是不允许系统login,可以使用其他ftp等服务
4.1.3 设置test用户的密码
[root@catyuan ~]# echo "test" || passwd test --stdin
4.2 指定sftp用户的家目录为同一个目录,然后我们通过用户名来区分
4.2.1 创建一个目录
[root@catyuan ~]# mkdir -p /data/sftp/test
4.2.2 修改test用户的家目录
[root@catyuan ~]# usermod -d /data/sftp/test test
4.3 配置文件
4.3.1 修改配置文件/etc/ssh/sshd_config
“#Subsystem sftp /usr/libexec/openssh/sftp-server” ##注释这行,文件尾部位置
##然后在后面添加下面这些:
Subsystem sftp internal-sftp
Match Group sftp
ChrootDirectory /data/sftp/test
ForceCommand internal-sftp
AllowTcpForwarding no
X11Forwarding no
4.3.2 配置的原因
如果这行文字存在且没有被注释掉,那么SFTP已经开启,所有可使用ssh的用户都可使用SFTP。
但是这种方式有一个缺陷,就是用户在SFTP软件里面可以cd / 从而看到系统所有文件。
所以说,我们要对其进行一些限定
4.3.3 相关配置详解
Subsystem sftp internal-sftp
#这行指定使用sftp服务使用系统自带的internal-sftp
Match Group sftp
# 匹配用户组,如果要匹配多个组,多个组之间用逗号分割
ChrootDirectory %h/%u
# 指定登陆用户到自己的用户目录 %h为相应的家目录 %u为相应的用户名
#举个例子:
#test是一个sftp组的用户,它通过sftp连接服务器上之后,只能看到/data/sftp/test目录下的内容
#test1也是一个sftp组的用户,它通过sftp连接服务器之后,只能看到/data/sftp/test1目录下的内容
ForceCommand internal-sftp
#指定sftp命令
X11Forwarding no
AllowTcpForwarding no
#是否允许用户能使用端口转发
Match user sftpadmin #匹配用户,多个用户名之间也是用逗号分割
ChrootDirectory /data/sftp
ForceCommand internal-sftp
AllowTcpForwarding no
X11Forwarding no
4.3.4 注意
1.chrootDirectory目录的父目录及其子目录的拥有者都只能是 root,用户组可以不是 root。
2.chrootDirectory目录的父目录及其子目录都不可以具有群组写入权限
3.chrootDirectory目录的父目录及其子目录属主只能是root,并且不能由任何其他用户写入,否则你
将在日志中看到错误报错"chroot目录的错误所有权或模式"。
4.sftp使用chrootDirectory指定根目录后,根应是无法写入的,所以要在chrootDirectory目录下新建一
个目录如upload,供test用户上传文件,这个目录所有者为test,所有组为sftp,所有者有写入
权限,而所有组无写入权限,权限为700、750或者755。
5.如果 chrootDirectory/upload 权限为700、750或者755,那么只能是 test用户上传文件,
如果其他用户属于sftp组,要使其也有上传权限,目录权限需设置为770。
4.4 设置chrootDirectory属主和目录权限
4.4.1 设置目录属主为root用户
[root@catyuan ~]# chown root:sftp /data/sftp/test/
4.4.2 设置目录权限为755,不允许群组及其他用户有写入权限
[root@catyuan ~]# chmod -R 755 /data/sftp/test/
4.5 创建test用户登录以后可以上传文件的目录
4.5.1 创建upload目录供test用户上传文件
[root@catyuan ~]# mkdir /data/sftp/test/upload
4.5.2 修改upload目录的属主为test用户,属组为sftp
[root@catyuan ~]# chown test:sftp /data/sftp/test/upload/
4.5.3 修改upload目录的权限为755,所有者有写入权限,所属组无写入权限
[root@catyuan ~]# chmod 755 /data/sftp/test/upload/
4.6 关闭selinux
4.6.1修改配置文件/etc/selinux/config
[root@catyuan ~]# vim /etc/selinux/config
将文件中的SELINUX=enforcing 修改为 SELINUX=disabled
4.6.2
[root@catyuan ~]# setenforce 0
4.7重启sshd服务
[root@catyuan ~]# systemctl restart sshd
4.8 登录验证
[c:\~]$ sftp test@192.168.191.128
输入密码后登录
sftp:/>
五、sftp的登录与退出
5.1 登录
Port是远程SFTP的端口,user是远程SFTP的登录用户名,ip是远程SFTP的地址或域名
5.1.1 sftp [remotehost IP ]
通过sftp连接[host],端口为默认的22,在弹出来的窗口输入要登录的用户名以及密码
[c:\~]$sftp 192.168.191.128
sftp:/>
5.1.2 sftp [user]@[remotehost IP]
通过sftp连接远程服务器,端口为默认的22,指定用户[user]。
[c:\~]$ sftp test@192.168.191.128
sftp:/>
5.1.3 sftp-oPort=[port][remotehost IP]
通过sftp连接远程服务器,指定端口[port],用户为Linux当前登录用户。
5.1.4 sftp -oPort=[port][user]@[ remotehost IP]
通过sftp连接[remotehost IP],端口为[port],用户为[user]。
5.2 退出
bye/exit/quit
六、sftp的命令
6.1 帮助
登录上sftp以后,输入help或?
[root@localhost ~]# sftp test@192.168.191.128
test@192.168.191.128's password: ##输入密码
Connected to 192.168.191.128.
sftp> help
bye Quit sftp
cd path Change remote directory to 'path'
chgrp grp path Change group of file 'path' to 'grp'
chmod mode path Change permissions of file 'path' to 'mode'
chown own path Change owner of file 'path' to 'own'
df [-hi] [path] Display statistics for current directory or
filesystem containing 'path'
exit Quit sftp
get [-Ppr] remote [local] Download file
reget remote [local] Resume download file
help Display this help text
lcd path Change local directory to 'path'
lls [ls-options [path]] Display local directory listing
lmkdir path Create local directory
ln [-s] oldpath newpath Link remote file (-s for symlink)
lpwd Print local working directory
ls [-1afhlnrSt] [path] Display remote directory listing
lumask umask Set local umask to 'umask'
mkdir path Create remote directory
progress Toggle display of progress meter
put [-Ppr] local [remote] Upload file
pwd Display remote working directory
quit Quit sftp
rename oldpath newpath Rename remote file
rm path Delete remote file
rmdir path Remove remote directory
symlink oldpath newpath Symlink remote file
version Show SFTP version
!command Execute 'command' in local shell
! Escape to local shell
? Synonym for help
6.2 操作远程服务器(server)的命令
cd: 切换目录
mkdir: 创建目录
ls/dir: 查看目录下的文件信息
rmdir: 删除目录
pwd:显示当前工作目录
chgrp:更改文件或目录的属组
chown:更改文件或目录的属主
chmod:更改文件或目录的权限
ln:建立文件链接
rm:删除文件或目录
rename:更改文件名或目录名
6.3 操作本机(client)的命令,命令前加l
lcd:切换目录
lls:列出当前主机所在目录的文件
lmkdir:创建目录
lpwd:显示当前目录
put:上传本机文件,格式为put [本机文件]
get:下载远程文件,get [远程文件]
七、案例
远程服务器IP:192.168.191.128,主机名为catyuan
客户端IP:192.168.191.131,主机名为localhost
7.1 从远程服务器下载文件
7.1.1 在远程服务器上创建文件
[root@catyuan ~]# cd /data/sftp/test/upload/
[root@catyuan upload]# touch test
7.1.2 连接远程服务器
[root@localhost ~]# sftp test@192.168.191.128
sftp>
7.1.3 查看远程服务器目录下的文件
sftp> ls upload/
upload/test
7.1.4 查看本机当前目录的文件
sftp> lls
anaconda-ks.cfg Documents etc.tar.gz Music Public Desktop
Downloads initial-setup-ks.cfg Pictures Templates Videos
7.1.5 从远程服务器下载test文件
sftp> get upload/test
Fetching /upload/test to test
7.1.6 查看本机当前目录下是否有了test文件
sftp> lls
anaconda-ks.cfg Documents etc.tar.gz Music Public test
Desktop Downloads initial-setup-ks.cfg Pictures Templates Videos
7.2 从本机上传文件
7.2.1 上传本机文件etc.tar.gz
sftp> put etc.tar.gz upload/
Uploading etc.tar.gz to /upload/etc.tar.gz
etc.tar.gz
7.2.2 查看远程服务器目录下的文件
sftp> ls upload/
upload/etc.tar.gz upload/test
7.3 在远程服务器上创建和删除目录
7.3.1 切换目录
sftp> cd upload/
7.3.2 查看目录下的文件
sftp> ls
etc.tar.gz test
7.3.3 创建目录
sftp> mkdir test_dir
sftp> ls
etc.tar.gz test test_dir
7.3.4 删除目录
sftp> rmdir test_dir/
sftp> ls
etc.tar.gz test
7.4 在远程服务器上删除文件
sftp> rmdir test
Couldn't remove directory: No such file or directory
sftp> rm test
Removing /upload/test
sftp> ls
etc.tar.gz