Last active
August 14, 2017 13:50
-
-
Save meabed/a0d1285a72b1a130ed1f1fb55b2a7189 to your computer and use it in GitHub Desktop.
OVH Ubuntu Kernel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment