A Simple Script for Adding Users to your AWS EC2 Instance

A Simple Script for Adding Users to your AWS EC2 Instance

As a systems administrator adding users is a multi-command process that can also be very time consuming. Fortunately, the right BASH script can save you time and keystrokes.

In this article, I am going to show you my useradd.sh script and how you can use it to seamlessly add users to your cloud system four simple, easy-to-follow steps:

Special Note: You must have root privileges on your system, in order to effectively run a script like this. If you don’t have root access, you script will not execute properly.

Prerequisites
Basic AWS services knowledge
An active AWS account with a running EC2 instance
Familiarity with the CLI and basic Linux commands
Basic BASH scripting knowledge
Basic understanding of Git and command knowledge
A GitHub.com account with a created personal access token (PAT)

Step 1: Creating Your Script
SSH into your your remote instance. Mine happens to be AWS EC2. Your’s might be different. Once logged in, create your script using the nano command:
nano useradd.sh
A Simple Script for Adding Users to your AWS EC2 Instance
I’ve provided you with a simple script below. When executed, this script will:

Direct the user to create a username
Direct the user to set a password
Checks username against existing usernames to ensure one doesn’t already exist
Uses a perl function that encrypts and writes the password securely to /etc/shadow
Sends a confirmation message or returns an error message
#!/bin/bash
# Automatically add a user to the server

if [ $(id -u) -eq 0 ]; then
        read -p "Select a Username : " username
        read -s -p "Set your Password : " password
        egrep "^$username" /etc/passwd > /dev/null
                if [ $? -eq 0 ]; then
                                echo "$username already exists. Please choose a different username."
                                exit 1
                        else
                                pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
                                useradd -m -p $pass $username
                                [ $? -eq 0 ] && echo "You have been successfully added to the system" || echo "Authentication Error. Failed to Create New User"
                        fi
                else
                        echo "Only a user with administrative privledges may add a user to the system."
                        exit 2
                fi
Hit [CTRL+X] to exit nano. Save the file prior to exiting. Confirm file name (useradd.sh)

Now that your script is written, you need to make it executable:

chmod u+x useradd.sh
Step 2: Running Your Script
Run your script:

sudo ./useradd.sh
When prompted, select a username hit [ENTER] and set a password and hit [ENTER].

If you were successful, your should see something similar in your terminal


You will receive a prompt telling you that the user was added successfully
Step 3: Change the EC2 Configuration File to Allow for Password Authentication
In AWS, it’s not enough to create a script that will add a new user. We must also edit the sshd_config file in the /etc directory to allow this. Let’s do this now in your CLI:

Switch to the root user:

sudo su
Open the sshd_config file in your preferred editor:

nano /etc/ssh/sshd_config

Change the PassWordAuthentication from ‘no’ to ‘yes’
You might need to scroll. Find the line “PasswordAuthentication no” change to “Password Authentication yes”. Type [CTRL+X] to exit. Hit “Y” to save the changes to the file, Hit [ENTER] to exit back to your working directory in the CLI.

Now, we need to restart sshd. Do this with the following command:

service sshd restart
If successful, you should recieve a similar message:


Step 4: Verify Results via SSH into our EC2 Instance Using the New User’s Credentials
Finally, to test to see if our script and updating the SSHD file worked. We log out of our instance and try logging in as our newly added user “cjones”:

ssh cjones@<EC2-xx-xxx-xxx-xx.compute-1.amazonaws.com
When prompted, we enter the password that we assigned to “cjones”. Hit [ENTER]. If everything was successful, you should now be logged in as the user you added using your BASH script:


BONUS STEP: Adding our Script to our GitHub Account
Now that we’ve tested our script for adding users. Let’s save it to our GitHub repository. In a previous article, I talked about how to Push Remote Scripts to GitHub for Later Use.

If you are interested in preserving your script for a GitHub repository, I highly encourage you read that as I will not go over specifics here with how to clone, stage, commit, and push a repo.

Now onward to our steps to add our script to our GitHub account:

Login to your EC2 instance with the user with which you constructed your script:


We’re going to move our script into our git repository:

mv useradd.sh scripts/
Move into our git repository:

cd scripts/
Instruct git to stage our script:

git add useradd.sh
Instruct git to stage our commit with a memo:

git commit -m "Automatically adds a user to the system and prompts for a password"
Push our git cloud-ward to our GitHub repository:

git push origin main
Finally, we can double check out GitHub.com repo to verify our push:


No comments:

Post a Comment

Featured post

A23 Rummy - Safe Secure Gaming Platform

A23 Rummy is a popular online rummy platform in India. It is owned and operated by Head Digital Works Private Limited. The platform offers...