Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4666ebb

Browse files
committed
Script for user account creation
1 parent cf80f8e commit 4666ebb

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Scenario:
2+
Imagine that you're working as a Linux System Administrator for a fast growing company. The latest company initiative requires you to build and deploy dozens of servers. You're falling behind schedule and are going to miss your deadline for these new server deployments because you are constantly being interrupted by the help desk calling you to create new Linux accounts for all the people in the company who have been recruited to test out the company's newest Linux-based application.
3+
In order to meet your deadline and keep your sanity, you decide to write a shell script that will create new user accounts. Once you're done with the shell script you can put the help desk in charge of creating new accounts which will finally allow you to work uninterrupted and complete your server deployments on time.
4+
5+
Requirements:
6+
The script:
7+
8+
- Is named "add-local-user.sh".
9+
10+
1. Enforces that it be executed with superuser (root) privileges. If the script is not executed with superuser privileges it will not attempt to create a user and returns an exit status of 1.
11+
12+
2. Prompts the person who executed the script to enter the username (login), the name for person who will be using the account, and the initial password for the account.
13+
14+
3. Creates a new user on the local system with the input provided by the user.
15+
16+
4. Informs the user if the account was not able to be created for some reason. If the account is not created, the script is to return an exit status of 1.
17+
18+
5. Displays the username, password, and host where the account was created. This way the help desk staff can copy the output of the script in order to easily deliver the information to the new account holder.
19+
20+
After coming up with your list, you realize that you're not sure where to get the hostname information. You decide to wait until you start writing your shell script to check the bash man page to see if there are any builtin commands or variables that could provide this information
21+
22+
23+
Psuedo Code:
24+
25+
# Make sure the script is being executed with superuser privileges.
26+
27+
# Get the username (login).
28+
29+
# Get the real name (contents for the description field).
30+
31+
# Get the password.
32+
33+
# Create the user with the password.
34+
35+
# Check to see if the useradd command succeeded.
36+
37+
# Set the password.
38+
39+
# Check to see if the passwd command succeeded.
40+
41+
# Force password change on first login.
42+
43+
# Display the username, password, and the host where the user was created.
44+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/bash
2+
3+
# This script is used for creating a user in linux/unix environments
4+
# Validation is also being done of the user account to check whether the command succeeded
5+
# Finally, displaying the user created along with password and the host on which it was created
6+
7+
# Make sure the script is being executed by superuser privileges
8+
if [[ "${UID}" -ne 0 ]]
9+
then
10+
echo "The script is not run as root"
11+
exit 1
12+
fi
13+
14+
15+
# Get the username (login)
16+
read -p "Enter the username: " USER_NAME
17+
18+
19+
# Get the real name and contents for the description
20+
read -p "Enter the real name: " COMMENT
21+
22+
23+
# Get the password
24+
read -p "Enter the password: " PASSWORD
25+
26+
27+
# Create the user with password
28+
useradd -c "${COMMENT}" -m ${USER_NAME}
29+
30+
31+
# Check to see if the useradd command succeeded
32+
if [[ "${?}" -ne 0 ]]
33+
then
34+
echo "User add command failed"
35+
exit 1
36+
fi
37+
38+
39+
# Set the password
40+
echo ${PASSWORD} | passwd --stdin ${USER_NAME}
41+
42+
# Check to see if password command succeeded
43+
if [[ "${?}" -ne 0 ]]
44+
then
45+
echo "Password command failed"
46+
exit 1
47+
fi
48+
49+
50+
# Force password to change on its first login
51+
passwd -e ${USER_NAME}
52+
53+
54+
# Display username, password and host where the user was created
55+
echo
56+
echo 'Username:'
57+
echo "${USER_NAME}"
58+
echo
59+
echo 'Password:'
60+
echo "${PASSWORD}"
61+
echo
62+
echo 'Hostname'
63+
echo "${HOSTNAME}"
64+
exit 0

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /