Shell Scripting Project: Creating a User and auto generate password in Ubuntu 20.04

Shell Scripting Project: Creating a User and auto generate password in Ubuntu 20.04

Table of contents

No heading

No headings in the article.

Introduction:

Shell scripting is an important skill for anyone working with Linux operating systems. It allows users to automate repetitive tasks and improve their productivity. In this tutorial, we'll be creating a simple shell script that creates a user in Ubuntu 20.04 with a username provided as an argument and generates a random password for the user. The script should be executed with only the root user. We'll also display the username, password, and hostname at the end. Also, before I start I want to thank Shubham Londhe for guidance in this learning journey.

Prerequisites:

Before we begin, you'll need to have access to a Ubuntu 20.04 machine and administrative privileges to create new users.

Steps:

  1. Open a terminal on your Ubuntu machine and create a new file called "create_user.sh" by running the command:

  1. Open the file in your favorite text editor and add the following code:

    Below conditions, I have kept making it more interesting.

    # Script should execute with only the root user

    #User should provide at least one argument username else guide him

    #Store the first argument as a user_name

    #In case of >1 arguments keep other arguments as account comment

    #Generate a random password

    #Create the User

    #Check whether the account is created or not

    #Set a password for a user account

    #Check if the password is successfully set or not

    #Force user to change the password on the first login

    #Display the USER_NAME, PASSWORD and HOST where user was created

  1. Save and close the file.

  2. Make the file executable by running the command:

  3. Test the script by running the command:

    The script will generate a random password for the user, create the user with the password, and then display the username, password, and hostname. I am also attaching my GitHub profile for the shell script 90DaysOfDevOps/2023/day05 at ShubhamBMatere-Batch-3 · ShubhamBMatere/90DaysOfDevOps (github.com)

    Conclusion:

    In this tutorial, we've learned how to create a simple shell script that creates a user in Ubuntu 20.04 with a username provided as an argument and generates a random password for the user. We've also displayed the username, password, and hostname at the end. This is a basic example of shell scripting, but it demonstrates how powerful and useful it can be. With more advanced scripting skills, users can automate complex tasks and improve their efficiency.