通过root用户& useradd shell脚本中的密码

时间:2018-04-12 10:46:41

标签: shell ssh

我已经创建了一个shell脚本来添加ubuntu sftp用户,并且从root终端我能够成功创建用户但我想传递root用户&此脚本中的密码,以便从任何地方我都可以执行此命令。我在我的服务器上设置了root密码。 所以任何人都可以帮助我,如何在我的脚本中添加$ USER root和$ PASSWORD选项?

以下是我的剧本:

#!/bin/bash
SFTPONLY_GROUP="sftponly"
SFTP_BASE_FOLDER="/srv/sftp"
UPLOAD_DIR="htdocs"

USERID="$1"

# Check parameters
if [ "$1" = "" ]
then
echo "Usage: $0 <new username>" 1>&2
exit 1
fi

# Check if root
#if [ "$(id -u)" != "0" ]; then
#   echo "This script must be run as root" 1>&2
#   exit 1
#fi

# Make sure the group exists
/bin/egrep  -i "^${SFTPONLY_GROUP}" /etc/group
if [ $? -eq 0 ]; then
    echo "Nice, group $SFTPONLY_GROUP already exists in /etc/group"
else
    echo "Group $SFTPONLY_GROUP does not exist, creating..."
    groupadd $SFTPONLY_GROUP
fi

# Make sure the user does not exist
/bin/egrep  -i "^${USERID}" /etc/passwd
if [ $? -eq 0 ]; then
    echo "User $USERID exists in /etc/passwd, aborting..."
    exit 1
else
    echo "Good, $USERID is a new user."

    if [ -d "$SFTP_BASE_FOLDER/$USERID" ]; then
            echo "Folder $SFTP_BASE_FOLDER/$USERID already exists, 
aborting..."
            exit 1
    else
            echo "Adding user..."
            useradd $USERID

            echo "Adding password..."
            passwd $USERID

            echo "Creating folder $SFTP_BASE_FOLDER/$USERID..."
            mkdir $SFTP_BASE_FOLDER/$USERID

            echo "Setting home directory of the new user..."
            usermod -d / $USERID

            echo "Assigning $USERID to $SFTPONLY_GROUP..."
            usermod -G $SFTPONLY_GROUP $USERID

            echo "Setting necessary permissions for chroot folder..."
            chmod -R 755 $SFTP_BASE_FOLDER/$USERID


            echo "Creating directory structure..."
            mkdir $SFTP_BASE_FOLDER/$USERID/$UPLOAD_DIR
            chown $USERID:$SFTPONLY_GROUP 
$SFTP_BASE_FOLDER/$USERID/$UPLOAD_DIR

            echo "Done."
    fi
fi

0 个答案:

没有答案