#!/bin/bash # # ############################################################################# # Create new SSH user (Ubuntu) # 1) Download the "raw" with - wget -O createNewSSHUser.sh # 2) Make it executable with - chmod a+x createNewSSHUser.sh # 5) Immediately set a new password by logging in once with - # su newUsrName # ############################################################################# # function createSSHUser { # touch /root/trash 2> /dev/null if [ $? -ne 0 ] then echo "Must execute as root user . . . " echo "sudo ./createNewSSHUser.sh" exit 0; fi export A_NEW_USER=$1 export NEW_USER_PWD=$2 # echo New User is $A_NEW_USER identified by $NEW_USER_PWD # echo "Get ${A_NEW_USER} home directory .. . . . . . . . " export A_NEW_USER_HOME=$( grep "${A_NEW_USER}" /etc/passwd | awk -F: '{print $6}' ) echo "New user's home directory is ${A_NEW_USER_HOME}" # if [ "XX${A_NEW_USER_HOME}" == "XX" ]; then # echo "Create admin group ............................................" addgroup admin # echo "Create a full privileges admin user ..........................." export PASS_HASH=$(perl -e 'print crypt($ARGV[0], "password")' "$NEW_USER_PWD") echo ${PASS_HASH} # addgroup sudo useradd -Ds /bin/bash useradd -m -G admin,sudo -p ${PASS_HASH} ${A_NEW_USER} # A_NEW_USER_HOME=/home/${A_NEW_USER} else echo "The ${A_NEW_USER} user account is already configured in ${A_NEW_USER_HOME} . . . " fi } NEW_UUID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1) createSSHUser ubuntu $NEW_UUID echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers mkdir -p /home/ubuntu/.ssh/ chown -R ubuntu:ubuntu /home/ubuntu/.ssh/ cp -r /root/.ssh/* /home/ubuntu/.ssh/ chown -R ubuntu:ubuntu /home/ubuntu/.ssh/ # reboot exit 100