Question

Create a script (bash preferably) that automates the setup of a user in ubuntu

Create a script (bash preferably) that automates the setup of a user in ubuntu

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The script is written below, I have created and tested it on my Ubuntu system only, but it will work on any modern Linux Distros including Redhat and its flavors.

#!/bin/bash
  

# The script should run by the root user
# Because the useradd command to add a user
# requires root permission.

read -p "Please enter username you wish to add to the system:- " user

# Need to see if the user is already present in the system
# For this, we can easily see with the id command to find out
# if the user exists or not; exit if yes.
# The command below discards the output on the console.

if id -u $user > /dev/null 2>&1; then
echo "The user is already present in the system, please try again with a different username"
exit 1
else
# User is new to the system, prompt for the password
read -s -p "Please enter password:- " pass

# Now we are good to add the user in the system.
# But, before that we need to encrypt the password
# As useradd manual says, the password has to be
# provided in encrypted form, as returned by crypt() method.
# We are using a small perl script to facilitate this.
# We can use Python and normal openssl as well, whatever
# we are comfortable with, the only thing to make sure is
# that we should use crypt() method as that's mandatory.
encryptedPass=$(perl -e 'print crypt($ARGV[0], "password")' $pass)

# We now have both username and the password for the new user
# will use useradd command to add it in the system with below options:
# -m => the user will be having a home directory /home/$user
# -s => the shell that we want the user to have, we are using bash for example
# -p => for providing the encrypted password we just created in the above step
useradd -m -s /bin/bash -p $encryptedPass $user

echo "User has been successfully added. Try logging in with su - <newUsername>..."
fi

# script finishes here ===============================================

I am including the actual IDE screenshot as well to highlight the commands used in the above script. Please take a look.

Running the script:

We are running the script as root user hence using "sudo ./a.sh". a.sh is our script.

once the testuser is successfully added, we are testing that user by logging in with it using "su - testuser". You will notice the id shows current testuser, pwd shows its home directory and the terminal shows "testuser@" which means the user is logged in now.

Now, we return back to the previous user's session and try to add the existing user to test out negative testcase. We give the same username "testuser" and it rejects it.

From the /etc/shadow file, we can see the testuser is listed in that file and its encrypted password is also there.

From the /etc/passwd file, we can also see that the testuser is now listed which confirms the user setup.

I hope the script works for you all fine, if you have any questions, please let me know and I will glad to clarify/assist.

Thanks for asking :)

Add a comment
Know the answer?
Add Answer to:
Create a script (bash preferably) that automates the setup of a user in ubuntu
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Putty 1. Create Bash shell script that would let a user make a copy of...

    In Putty 1. Create Bash shell script that would let a user make a copy of all the files in a directory. (3 points) 2. Create two Bash shell scripts that will perform some work (of your choice). (2*3 = 6 points) 3. Create a Bash shell script that would serve as a menu for the three scripts you have created. (1 point)

  • In the Ubuntu Terminal Create a script to copy DHCP logs to a separate file Create...

    In the Ubuntu Terminal Create a script to copy DHCP logs to a separate file Create a directory ~/scripts Create a directory ~/logs Create a script in ~/scripts, name it dhcp-activity.sh Include a comment that describes the script Print to the terminal “Copying DHCP Log Activity” Copy activity from dhclient to a file called dhclient.log in the ~/logs directory When you are finished, show the instructor: Cat dhcp-activity.sh Bash dhcp-activity.sh Cat dhclient.log

  • Problem 1 Write a BASH script to create a user account from the Linux system on...

    Problem 1 Write a BASH script to create a user account from the Linux system on which your script is run. The script should process two positional parameters. First positional parameter is supposed to be a string with a user name (e.g., user_name) Second positional parameter is supposed to be a string with a user password (e.g., user_password) In your script: Check if two positional parameters were passed to your script when it was invoked If NOT, print an appropriate...

  • Write a bash script to protect a directory. Your script should notify a user of the...

    Write a bash script to protect a directory. Your script should notify a user of the following: -New file was created -Permission changes -Ownership changes -File size changes -Modification date changes Your script should have two operation modes, protect and check. Use command line flags, -p <directory> and -c <directory> to decide which mode to operate in. In protect mode, your script should create a file called .protect inside the directory you want to watch. You will use this file...

  • I need a bash script that prompts the user for a filename within a directory, and...

    I need a bash script that prompts the user for a filename within a directory, and if the file/subdirectory exists to gzip them. Directories should be tarred and zipped, files should just be gzipped. This should poll inside of a loop to keep prompting the user until the user ends the script.

  • Write a script using bash that accepts three arguments from the user, then prints out the...

    Write a script using bash that accepts three arguments from the user, then prints out the following: The name of the script The value of each argument

  • Create a bash script that permanently redirects the scripts output to a file. Once the output...

    Create a bash script that permanently redirects the scripts output to a file. Once the output is permanently redirected execute 3 commands of your choosing within the script. Be sure that the script finishes by restoring output back to STDOUT

  • Hello I need help creating a bash script in linux for the following question: Create a...

    Hello I need help creating a bash script in linux for the following question: Create a script to expand the jpeg creator to create other types of files such as pdf, png, gif, etc. The jpeg creator is the following: #!/bin/bash FILE=$1 echo -e -n "\xFF\xD8\xFF\xE0"> $FILE dd if=/dev/random bs=512 count=4 >> $FILE

  • Write a Bash script called hello that uses command line arguments to allow the user to...

    Write a Bash script called hello that uses command line arguments to allow the user to put two strings after the command name, when the script is being executed. These strings should represent a first and last name. The script should then write out a greeting to the user that includes the first and last name. Here is an example of how the script might work (the first line represents what the user types to launch the script): [user@HAL] hello...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT