Linux中SFTP配置与用户管理脚本

公司需要建立一个FTP来管理客户的文件上传,为了安全性我们打算采用SFTP,并要求每个客户通过SSHKEY登陆。
或许你会说:“SFTP不需要配置啊,有SSH直接就可以用了”。的确,但是我们不希望用户能够通过SSH登陆到我们的服务器上,我们希望每个客户只能通过SFTP来管理自己的文件,同时我们还想统一的来管理所有客户的目录。有了这样的需求,默认的SSH配置就不能满足了。

下面,是SFTP的配置步骤,以及我编写的一个用户管理脚本:

1.修改SFTP相关配置

$ sudo vim /etc/ssh/sshd_config

 代码如下 复制代码

# override default of no subsystems
#Subsystem sftp /usr/libexec/openssh/sftp-server
Subsystem sftp internal-sftp

Match Group sftpusers
        ChrootDirectory /home/sftp/%u
        ForceCommand internal-sftp

$ sudo /etc/init.d/sshd restart

2.创建用户管理脚本

 代码如下 复制代码

$ sudo vim sftpusers.sh

#!/bin/bash
#
# Manage sftp users for customers

# Author: Dong Guo
# Last Modified: 2013/09/06 by Dong Guo

userfile=/etc/passwd
groupfile=/etc/group
homedir=/home/sftp
loginshell=/sbin/nologin
groupname=sftpusers
username=$2

function check_root()
{
  if [ $EUID -ne 0 ]; then
    echo "This script must be run as root" 1>&2
    exit 1
  fi
}

function print_help(){
  #Print help messages then exit
  echo "Usage: $0 {create|disable|enable|passwd|sshkey|delete} {username}" >&2
  exit 1
}

function check_usergroup(){
  #Create usergroup if NOT exist
  cut -d : -f 1 $groupfile | grep -wq $groupname
  if [ $? -ne 0 ];then
    groupadd $groupname
  fi
}

function check_homedir(){
  #Create homedir if NOT exist
  if [ ! -d "$homedir" ];then
    mkdir $homedir
  fi
}

function check_username_exist(){
  #Check if user already exist
  cut -d : -f 1 $userfile | grep -wq $username
  if [ $? -eq 0 ];then
    echo "User $username ALREADY exist." && exit
  fi
}

function check_username_notexist() {
  #Check if user not exist
  cut -d : -f 1 $userfile | grep -wq $username
  if [ $? -ne 0 ];then
    echo "User $username NOT exist." && exit
  fi
}

function check_user_disabled(){
  #Check if user ALREADY disabled
  lockfile=$homedir/$username/sftpuser.locked
  if [ -a "$lockfile" ]; then
    echo "User $username ALREADY disabled." && exit
  fi
}

function update_sshkey(){
  #Get the sshkey
  echo -n "Input sshkey: "
  read sshkey
  #Check if sshkey is empty
  if [ -z "$sshkey" ];then
    echo "Empty sshkey." && exit
  fi
  #Check if sshkey not correct
  echo $sshkey | grep -Ewq '^ssh-rsa|^ssh-dss'
  if [ $? -ne 0 ];then
    echo "String "ssh-rsa" or "ssh-dss" NOT found." && exit
  fi
  mkdir $homedir/$username/.ssh
  chmod 700 $homedir/$username/.ssh
  echo "$sshkey" > $homedir/$username/.ssh/authorized_keys
  chmod 600 $homedir/$username/.ssh/authorized_keys
  chown -R $username:$groupname $homedir/$username/.ssh
}

if [ $# != 2 ];then
  print_help
fi

check_root
check_usergroup
check_homedir

case "$1" in
  'create')
    check_username_exist
    useradd -m -d "$homedir/$username" -g $groupname -s $loginshell -c "$username sftp" $username
    chmod 755 $homedir/$username
    chown root:root $homedir/$username
    if [ $? -eq 0 ]; then
      echo "User $username was created."
    fi
    ;;
  
  'disable')
    check_username_notexist
    passwd -l $username
    touch $homedir/$username/sftpuser.locked
    authfile=$homedir/$username/.ssh/authorized_keys
    if [ -a "$authfile" ]; then
      mv $authfile $authfile.disabled
    fi
    if [ $? -eq 0 ]; then
      echo "User $username was disabled."
    fi
    ;;
 
  'enable')
    check_username_notexist
    passwd -u $username
    rm -f $homedir/$username/sftpuser.locked
    authfile=$homedir/$username/.ssh/authorized_keys
    if [ -a "$authfile.disabled" ]; then
      mv $authfile.disabled $authfile
    fi
    if [ $? -eq 0 ]; then
      echo "User $username was enabled."
    fi
    ;;
  
  'delete')
    check_username_notexist
    echo -n "Delete all the data and account of user $username? [yes|no] "
    read yesorno
    if [ "$yesorno" == "yes" ];then
      userdel -rf $username
      if [ $? -eq 0 ]; then
        echo "User $username was deleted."
      fi
    fi
    ;;

  'passwd')
    check_username_notexist
    check_user_disabled
    passwd $username
    ;;
  
  'sshkey')
    check_username_notexist
    check_user_disabled
    update_sshkey
    if [ $? -eq 0 ]; then
      echo "The sshkey of user $username was updated."
    fi
    ;;
      
  *)
    print_help
    ;;
esac

$ sudo chmod +x sftpusers.sh

3.创建SFTP用户并测试

$ ./sftpusers.sh

 代码如下 复制代码

1 Usage: ./sftpusers.sh {create|disable|enable|passwd|sshkey|delete} {username}

创建用户

 代码如下 复制代码

$ ./sftpusers.sh create facebook

This script must be run as root

$ sudo ./sftpusers.sh create facebook

 User facebook was created.

$ id facebook

uid=504(facebook) gid=503(sftpusers) groups=503(sftpusers)

$ grep facebook /etc/passwd

facebook:x:504:503:facebook sftp:/home/sftp/facebook:/sbin/nologin 禁用用户

$ sudo ./sftpusers.sh disable facebook

Locking password for user facebook.
passwd: Success
User facebook was disabled.

修改用户密钥

 代码如下 复制代码

$ sudo ./sftpusers.sh sshkey facebook

User facebook ALREADY disabled.

$ sudo ./sftpusers.sh enable facebook

Unlocking password for user facebook.
User facebook was enabled.

$ sudo ./sftpusers.sh sshkey facebook

Input sshkey: ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA7g/9+vddcvZHpEBSLoNR3fCOuR0ZlyKBcmG7N facebook@us
The sshkey of user facebook was updated.

$ sudo ls -l /home/sftp/facebook/.ssh/authorized_keys

-rw------- 1 facebook sftpusers 397 Sep  5 19:40 /home/sftp/facebook/.ssh/authorized_keys

连接SFTP

 代码如下 复制代码

$ sftp facebook@sftpserver

Connecting to sftpserver...
sftp> ls -a
.  .. .bash_logout .bash_profile .bashrc .ssh           
sftp> cd .ssh
sftp> ls
authorized_keys   
sftp> exit

删除用户

 代码如下 复制代码

$ sudo ./sftpusers.sh delete facebook

Delete all the data and account of user facebook? [yes|no] yes
User facebook was deleted.

$ sudo ./sftpusers.sh delete facebook

User facebook NOT exist.

$ id facebook

id: facebook: No such user

时间: 2024-09-09 19:32:55

Linux中SFTP配置与用户管理脚本的相关文章

在 Linux 中为非 SSH 用户配置 SFTP 环境

在 Linux 中为非 SSH 用户配置 SFTP 环境 在某些环境中,系统管理员想要允许极少数用户在可以传输文件到Linux机器中,但是不允许使用 SSH.要实现这一目的,我们可以使用SFTP,并为其构建chroot环境. SFTP & chroot背景: SFTP是指SSH文件传输协议(SSH File Transfer protocol)或安全文件传输协议(Secure File Transfer Protocol),它提供了可信数据流下的文件访问.文件传输以及文件管理功能.当我们为SFT

在Linux中添加普通新用户

  在Linux中添加普通新用户 ,超级用户(也称为"root")是一个具有修改系统中任何文件权力的特别账号.在日常工作中,最好不要使用超级用户账号进入系统,因为任何错误操作都可能导致巨大的损失.由于超级用户账号是系统建立后提供的惟一一个账号,因此,您需要建立和使用一个一般用户账号进行日常工作. 超级用户可以创建新的用户账号,下面的命令将建立一个名为joe的新用户: # adduser joe # passwd joe (键入joe的口令) Linux采用了将系统管理员和一般用户分开的

Linux下VsFTP和ProFTP用户管理高级技巧 之一

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://chenguang.blog.51cto.com/350944/217853 Linux下VsFTP和ProFTP用户管理高级技巧       FTP服务时互联网上比较古老的一种应用,至今Interner应用面非常广泛,但令管理员头痛不已的是其用户管理,既多且杂,如何解决这一问题呢?使用MySQL与ProFTP或VsFTP软件结合可以搭建一个高效.稳定且集中管理的FTP服务器.本

linux中apache配置两个域名指向同一个ip地址不成功

问题描述 linux中apache配置两个域名指向同一个ip地址不成功 两个域名解析完毕,在linux服务器中配置apache文件后有一个域名不成功.测试过解析没问题,将有问题的域名指向其它有域名的服务器发现可用~望求高手出手帮忙~ 解决方案 看看这个 解决方案二: 看看这个

Linux中如何配置让哪些服务启动?

Linux中如何配置让哪些服务启动? 1 运行ntsysv或者setup命令,进入菜单进行配置: [root@localhost software]# ntsysv [root@localhost software]# setup

linux中vsftpd下虚拟用户+被动模式配置详解

下面说说在linux下部署vsftp的过程吧,小小笔记. 一.安装 这里我说下最简单的rpm包安装,因为光盘中有,何必编译呢? yum install db4* vsftpd -y 二.配置 1.新建系统用户 useradd vsftpd -s /sbin/nologin 限制vsftpd用户登录,此用户将最为vsftpd服务的宿主用户 useradd ftp -s /sbin/nologin 这里是Vsftpd虚拟宿主用户,默认情况下这个用户应该有. 当然这些用户你可以用另外的名字. 2.修改

Linux系统中sftp配置之密钥方式登录

由于vsftp采用明文传输,用户名密码可通过抓包得到,为了安全性,需使用sftp,锁定目录且不允许sftp用户登到服务器.由于sftp使用的是ssh协议,需保证用户只能使用sftp,不能ssh到机器进行操作,且使用密钥登陆.不是22端口. 1. 创建sftp服务用户组,创建sftp服务根目录 groupadd sftp #此目录及上级目录的所有者必须为root,权限不高于755,此目录的组最好设定为sftp mkdir /data/sftp chown -R root:sftp /data/sf

linux中shell批量添加用户和设置随机密码脚本

有一个朋友问我如何批量创建用户和设置密码 ,我就简单给他写了两个脚本,让他自己参考下并自己根据实际情况进行修改,毕竟他需要的用户名和密码都是实际生产环境的. 这里分两种情况,一种是测试for循环批量添加用户,一个是根据实际生产环境进行批量添加.分别写一下案例如下: 1,for添加指定类型用户以及设置随机密码. 脚本作用:批量添加user1-10用户并设置随机8位数随机密码 #!/bin/bash #test add user and set passwd for n in `seq 10` do

linux系统中的列出敏感用户的脚本代码_linux shell

此处的敏感用户是指这个用户属于多个组,或者这个用户属于的组名跟这个用户名不一样 #! /bin/bash #list user who belong to more than one group #and list user who belong to the group which isn't the same as the username #w is whitelist,we will not think user in this whitelist is special or dange