Created
September 30, 2015 20:26
-
-
Save psalaberria002/fab4548720de282f2f1c to your computer and use it in GitHub Desktop.
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 user and remove existing default user. | |
# Functions: | |
# - Create a new user. REMEMBER TO CHANGE THE PASSWORD! | |
# - Copy existing .ssh folder to new user | |
# - Change owner to new user | |
# - Delete default user (ubuntu in ec2 plain images) | |
# | |
# Variables to change: | |
# - username | |
# - password | |
# - userToRemove | |
######################################################################## | |
if [ $(id -u) -eq 0 ]; then | |
username="paul" | |
password="changeme" | |
if [ -z "$(getent passwd $username)"]; then | |
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) | |
useradd -p $pass -m -s /bin/bash $username | |
if [ $? -eq 0 ]; then | |
echo "User has been added to system!" | |
echo "Giving sudo access to $username" | |
usermod -a -G admin $username | |
userToRemove="ubuntu" | |
if [ -z "$(getent passwd $userToRemove)"]; then | |
echo "$userToRemove user doesn't exist. Not copying .ssh folder" | |
else | |
echo "Copying .ssh folder from $userToRemove user to $username" | |
cp -R /home/$userToRemove/.ssh /home/$username/ | |
echo "Changing owner $username:$username /home/$username/.ssh" | |
chown -R $username:$username /home/$username/.ssh | |
echo "Deleting user $userToRemove" | |
userdel $userToRemove | |
if [ $? -eq 0 ]; then | |
echo "User removed successfuly" | |
echo "Removing home folder for $userToRemove user" | |
rm -rf /home/$userToRemove | |
else | |
echo "Couldn't remove user" | |
fi | |
fi | |
else | |
echo "Failed to add a user!" | |
fi | |
else | |
echo "$username exists!" | |
#exit 1 | |
fi | |
else | |
echo "Only root may add a user to the system" | |
#exit 2 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment