centos7搭建sftp服务器

centos7搭建sftp服务器

参考网址:

https://blog.csdn.net/michaelwoshi/article/details/94184288

一、实验背景

一、基于对线上服务器的保密和安全,不希望开发人员直接登录线上服务器,因为登录服务器的权限太多难以管控,如直接修改代码、系统配置等,希望能限制开发人员ssh登录机器,但是通过ftp/sftp上传代码文件。

二、项目中一些模块会调用sftp客户端程序,上传文件到服务器,实现相关功能,需要搭建好sftp服务端。

二、sftp服务简介

sftp是Secure FileTransferProtocol的缩写,安全文件传送协议,可以为传输文件提供一种安全的加密方法。

1)sftp 与 ftp 有着几乎一样的语法和功能

2)SFTP 为 SSH的一部分,是一种传输文件至服务器的安全方式

3)SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接操作

4)SFTP安全性非常高

5)SSH软件已经包含SFTP安全文件传输子系统

三、实验环境

使用 Oracle VM VirtualBox 搭建虚拟机

系统 : centos7

虚拟机 IP 地址 : 192.168.56.10

系统具体信息

[root@localhost ~]# cat /etc/os-release
NAME=“CentOS Linux”
VERSION=“7 (Core)”
ID=“centos”
ID_LIKE=“rhel fedora”
VERSION_ID=“7”
PRETTY_NAME=“CentOS Linux 7 (Core)”
ANSI_COLOR=“0;31”
CPE_NAME=“cpe:/o:centos:centos:7”
HOME_URL=“https://www.centos.org/”
BUG_REPORT_URL=“https://bugs.centos.org/”

CENTOS_MANTISBT_PROJECT=“CentOS-7”
CENTOS_MANTISBT_PROJECT_VERSION=“7”
REDHAT_SUPPORT_PRODUCT=“centos”
REDHAT_SUPPORT_PRODUCT_VERSION=“7”

四、sft软件安装

yum -y install openssh-server  openssh-clients
ssh  -V

相比传统的FTP服务,SFTP 显得更加方便、安全,一般系统安装了 ssh 后,默认就安装了这个服务,我们只要简单的配置一下就可以了。

注:openssh-server 版本至少得 4.8p1,因为配置权限需要版本添加的新配置项 ChrootDirectory 来完成。

五、配置sftp

SFTP 账号是基于 SSH 账号(也就是操作系统账户)的,默认情况下访问服务器的权限很大,怎样像FTP那样限制 SFTP 账号相关的访问权限呢?

  1. 我们需要创建一个用户组,专门用于sftp用户
groupadd  sftp
  1. 创建一个用户test,附加到sftp组,不允许登录操作系统
useradd  -G sftp  -s /bin/false  test
# 密码为 root
passwd --stdin  test <<<  "root"
  1. 编辑配SSH置文件

注释掉 Subsystem sftp /usr/libexec/openssh/sftp-server 行

添加一行 Subsystem sftp internal-sftp

说明:

如果这行文字存在且没有被注释掉,那么SFTP已经开启,所有可使用ssh的用户都可使用SFTP,但是这种方式有一个缺陷,就是用户在SFTP软件里面可以cd / 从而看到系统所有文件。

**为什么实用 internal-sftp 而不用默认的 sftp-server?**

因为这是一个进程内的 sftp 服务,当用户 ChrootDirectory 的时候,将不请求任何文件,且有更好的性能,不用为 sftp 再开一个进程。

  1. 配置 sshd_config 文件
vim  /etc/ssh/sshd_config

添加如下内容

Subsystem sftp internal-sftp

Match Group sftp

chrootDirectory %h

ForceCommand internal-sftp

X11Forwarding no

AllowTcpForwarding no

相关配置说明:

指定登陆用户到自己的用户目录 %为相应的家目录 %u为相应的用户名

ChrootDirectory %h

指定 sftp 命令

ForceCommand internal-sftp

是否允许用户能使用端口转发

X11Forwarding no

AllowTcpForwarding no

  1. 修改test用户home文件夹的权限,让其属于root用户
chown root:test  /home/test

chmod 750  /home/test

mkdir  /home/test/upload

chown test:sftp /home/test/upload

chmod 750  /home/test/upload
  1. 重启 sshd 服务 , 是 sshd_config 配置生效
systemctl restart sshd
  1. 测试文件上传
  • 测试 test 用户不可以使用 ssh 登录

$ ssh test@192.168.56.10
test@192.168.56.10’s password:
This service allows sftp connections only.
Connection to 192.168.56.10 closed.

说明: test只允许 sftp , 不允许 ssh

  • 测试文件上传到指定的目录

在本地 G 盘新建文件 hello.txt

内容如下

测试 sftp 文件上传

开启终端

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qoUUN4Wd-1652805015474)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20220429110415997.png)]

服务器查看文件

[root@localhost upload]# pwd
/home/test/upload
[root@localhost upload]# cat hello.txt
测试sftp文件上传
[root@localhost upload]#

  • 测试上传文件到非指定目录

s put /g/hello.txt /
Uploading /g/hello.txt to /hello.txt
remote open(“/hello.txt”): Permission denied

直接上传到 ChrootDirectory,权限不足

到此 sftp 服务器搭建完成

六、自定义配置 sftp 文件上传目录

根据上面例子,如果我们想要指定其他的 ChrootDirectory,操作就变得简单了!

例如上传路径为: /opt/sftp/chroot/upload

修改 sshd_config 配置

Subsystem sftp internal-sftp

Match Group sftp

chrootDirectory /opt/sftp/chroot

ForceCommand internal-sftp

X11Forwarding no

AllowTcpForwarding no

修改文件权限

mkdir -p /opt/sftp/chroot

mkdir -p /opt/sftp/chroot/upload

chown root:test /opt/sftp/chroot

chown test:sftp /opt/sftp/chroot/upload

chmod 750 /opt/sftp/chroot

chmod 700 /opt/sftp/chroot/upload

重启sshd服务

 systemctl  restart sshd 

 systemctl  status sshd 

测试登录和文件上传

ssh 登录失败

$ ssh test@192.168.56.10
test@192.168.56.10’s password:
This service allows sftp connections only.
Connection to 192.168.56.10 closed.

上传到根目录失败 , 显示权限不够

s put /g/hello.txt /
Uploading /g/hello.txt to /hello.txt
remote open(“/hello.txt”): Permission denied

上传到指定目录成功

$ sftp test@192.168.56.10
test@192.168.56.10’s password:
Connected to 192.168.56.10.
s put /g/hello.txt /upload/
Uploading /g/hello.txt to /upload/hello.txt
/g/hello.txt 100% 23 3.7KB/s 00:00
s

登录服务器查看是否上传成功

[root@localhost upload]# cd /opt/sftp/chroot/upload/
[root@localhost upload]# pwd
/opt/sftp/chroot/upload
[root@localhost upload]# cat hello.txt
测试sftp文件上传

sftp 命令补充

sftp命令 是一款交互式的文件传输程序,命令的运行和使用方式与ftp命令相似,但是,sftp命令对传输的所有信息使用ssh加密,它还支持公钥认证和压缩等功能。

语法

sftp(选项)(参数)

选项

-B:指定传输文件时缓冲区的大小;
-l:使用ssh协议版本1;
-b:指定批处理文件;
-C:使用压缩;
-o:指定ssh选项;
-F:指定ssh配置文件;
-R:指定一次可以容忍多少请求数;
-v:升高日志等级。

参数

目标主机:指定sftp服务器ip地址或者主机名。

实例

建立联接

$ sfpt username@1.1.1.1 # 回车输入密码

获取文件下载到指定路径

sftp> get /export/sftp/test.csv /Users/my/Downloads
Fetching /export/sftp/test.csv to /Users/my/Downloads/test.csv
/export/sftp/test.csv            100%  133     0.3KB/s   00:00

上传本地文件到服务器指定路径

sftp> put /Users/my/Downloads/re-produce.gif /export/sftp
Uploading /Users/my/Downloads/re-produce.gif to /export/sftp/re-produce.gif
/Users/my/Downloads/re-produce.gif            100%  257KB  86.6KB/s   
t/sftp/test.csv to /Users/my/Downloads/test.csv
/export/sftp/test.csv            100%  133     0.3KB/s   00:00

上传本地文件到服务器指定路径

sftp> put /Users/my/Downloads/re-produce.gif /export/sftp
Uploading /Users/my/Downloads/re-produce.gif to /export/sftp/re-produce.gif
/Users/my/Downloads/re-produce.gif            100%  257KB  86.6KB/s   
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值