创建sftp账户

脚本自动配置sftp用户

#!/bin/bash
# Filename:    createSftpCentOS.sh
# Revision:    1.0
# Date:        2018/4/8
# Author:      111
# Email:       111@139.com
# Description: 此脚本适用于centos操作系统配置sftp用户

#1.创建sftp组
function groupAdd(){
    groupadd sftp
}
#2.创建用户sftp用户
function userAdd(){
    mkdir -p $sftpRoot
    useradd -d $sftpRoot -g sftp -s /bin/false $userName
    echo $userPassWord|passwd --stdin $userName
}
#3.更改sftp home目录权限
function changeDir(){
    chown root:sftp $sftpRoot
    chmod 755 $sftpRoot
}

#4.编辑vi /etc/ssh/sshd_config
function changeConfig(){
sed -i "s/Subsystem/#Subsystem/g" /etc/ssh/sshd_config
cat >>/etc/ssh/sshd_config << EOF
Subsystem sftp internal-sftp
#mvtechsftp config
Match User mvtechsftp
ChrootDirectory /data/sftp_root/mvtechsftp
ForceCommand internal-sftp
EOF
}
#5.重启ssh服务
function restart6(){
    service sshd restart
}
function restart7(){
    systemctl restart sshd
}
#启动程序
function init(){
    #引用环境变量
    source /etc/profile
    #定义变量
    #用户名
    userName='sftpuser'
    #密码
    userPassWord='1qaz2wsx3edc'
    #sftphome目录
    sftpRoot='/data/sftp_root/mvtechsftp'
    groupAdd;
    userAdd;
    changeDir;
    #changeConfig;
    #判断操作系统版本
    export version=$(cat /etc/redhat-release|sed -r 's/.* ([0-9]+)..*/1/')
    if [ $version == "6" ];then /bin/echo "true";restart6;
    elif [ $version == "7" ];then /bin/echo "7";restart7;
    else /bin/echo "This system is not centos"fi
}
init
原文地址:https://www.cnblogs.com/sdhzdtwhm/p/9283359.html