在 Linux 中配置 SFTP 环境

    做运维的朋友,应该经常会碰到这样的问题,研发需要新上一个web项目,需要上传文件到服务器上,并且仅仅是上传项目,解决方法一边有两种,sftp和ftp,今天讲如何使用sftp让系统用户用户上传项目的权限,并且实现chroot和无法使用ssh登录到系统:

    SFTP是指SSH文件传输协议(SSH File Transfer protocol)或安全文件传输协议(Secure File Transfer Protocol),它提供了可信数据流下的文件访问、文件传输以及文件管理功能。当我们为SFTP配置chroot环境后,只有被许可的用户可以访问,并被限制到他们的家目录中,换言之:被许可的用户将处于牢笼环境中,在此环境中它们甚至不能切换它们的目录。

   

 我的环境:

[root@test ~]# cat /etc/issue
CentOS release 6.6 (Final)
Kernel \r on an \m
[root@test ~]# rpm -qa | grep openssh-server
openssh-server-5.3p1-104.el6.i686

   


  1. 增加一个sftp用户组

[root@test ~]# groupadd sftpusers

2.创建一个用户分配给sftp用户

[root@test ~]# useradd -g sftpusers user01

3.修改用户家目录及指定不能登录shell

[root@test ~]# mkdir /sftp/
[root@test ~]# usermod -s /sbin/nologin -d /sftp/user01 -m user01

4.给用户创建密码(注意密码不明文显示)

[root@test ~]# passwd user01 
Changing password for user user01.
New password: 
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@test ~]#

5.修改ssh的配置文件

[root@test ~]# ll /etc/ssh/sshd_config 
-rw-------. 1 root root 3879 Oct 15  2014 /etc/ssh/sshd_config
[root@test ~]# vim /etc/ssh/sshd_config

# line 132
#Subsystem      sftp    /usr/libexec/openssh/sftp-server    #注释
Subsystem       sftp    internal-sftp        #修改为internal-sftp

# add this lines at the end of file
Match Group sftpusers        #指定一下参数仅适用的用户组sftpusers
    X11Forwarding no
    AllowTcpForwarding no
    ChrootDirectory %h       #设置chroot将用户锁在家目录,%h=家目录               
    ForceCommand internal-sftp    #该参数强制执行内部sftp

6.重启ssh服务

[root@test ~]# /etc/init.d/sshd restart 
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]

7.设置用户家目录权限

[root@test ~]# chmod 0755 /sftp/user01/    #注意权限不能大于0755
[root@test ~]# chown root /sftp/user01/    
[root@test ~]# chgrp -R sftpusers /sftp/user01/

8.如果要上传的话...(bug我没什么解决的好办法)

#因为用户家目录属主是root,并且权限最大0755,所以没法写,我的解决方法是在在家目录建立一个#文件夹,作为上传目录,并把属主给user01即可。
[root@test ~]# mkdir /sftp/user01/upload
[root@test ~]# chown user01:sftpusers /sftp/user01/upload/

9.linux登录测试

[root@test ~]# su - user01 
This account is currently not available.    #su - 切换失败
[root@test ~]# cat /etc/passwd | tail -1
user01:x:500:500::/sftp/user01:/sbin/nologin
[root@test ~]# ssh user01@localhost
The authenticity of host 'localhost (::1)' can't be established.
RSA key fingerprint is f3:fc:31:dc:7d:16:d5:ad:8c:bc:eb:69:8f:b2:0b:c9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
user01@localhost's password: 
This service allows sftp connections only.    #ssh登录也失败,ssh设置成功
Connection to localhost closed.

10.sftp登录测试(我这里windows7用xshell,工具大同小异)

wKiom1WdK1eAsUt4AAGnZQ2cAnk259.jpg


成功登录:

wKiom1WdK8Lw3LtDAAChiU5-uKE644.jpg


根目录下无法创建:

wKioL1WdLZKwn25xAAEo1dVXlWo558.jpg


upload下可以创建:

wKiom1WdK8KxbUUCAADIQG4JQ3g621.jpg


好了,这篇博文就到这里了,完成今天的任务,对于根目录无法创建的原因,是因为linux的安全机制限制,另外如果有童鞋在操作过程中发现有不对的地方欢迎来和我讨论,我的邮箱:luchuanjia@msn.com

本文出自 “一杯白开水” 博客,请务必保留此出处http://ultraera.blog.51cto.com/6640392/1672193

转载于:https://my.oschina.net/samzong/blog/885640

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值