#!/bin/sh
In these lines, #!/bin/sh is used as the first line of a script to invoke the named shell. On the first line of the script, the "#!", is the name of a program which should be used to interpret the contents of the file. In this case, the first line contains "#! /bin/sh", then the contents of the file are executed as a shell script. # is used in shell scripts as the comment character. The script typically ignores all text that follows on the same line.
# adduser - Adds a new user to the system, including building their
# home directory, copying in default config data, etc.
# For a standard Unix/Linux system
These are comment lines. # is used in shell scripts as the comment character. The script typically ignores all text that follows on the same line.
pwfile="/etc/passwd" shadowfile="/etc/shadow"
gfile="/etc/group"
hdir="/home"
These are assignment statements that assign values for the directory locations. On a Unix system, an account is created by adding a unique entry to the /etc/passwd file, an entry consisting of a one-to eight-character account name, a unique user ID, a group ID, a home directory (/home), and a login shell for that user. The system stores the encrypted password value in /etc/shadow, an entry must be added to that file too, and finally the account needs to be listed in the /etc/group file, with the user either as his or her own group or as part of an existing group.
if [ "$(whoami)" != "root" ] ; then
This line checks to see if you are logged in as the root. The semicolon separates multiple commands entered on a single line.
echo "Error: You must be root to run this command." >&2
If you are not signed in as the root, then the script will stop with the error message, “Error: You must be root to run this command."
exit 1
If user is not the root, then the script will exit. The 1 represents a failure error code.
fi
fi is an end statement. If the person is not a root account holder then the script will stop. It will not go any further than step above.
echo "Add new user account to $(hostname)"
This statement displays the string between the quotes $(hostname). $(hostname) runs the 'hostname' command, which identifies the computer or server. This information is defined in the .profile file in /etc/profile or ~/.bash_profile
echo -n "login: " ; read login
This line prompts for login. Usually, the echo command prints something, and moves to the next line. With '-n', it stays on the same line. So the output of this line will read
(login: kiverson)
# Adjust '5000' to match the top end of your user account namespace
# because some system accounts have uid's like 65535 and similar.
These are comment lines. # is used in shell scripts as the comment character. The script typically ignores all text that follows on the same line.
uid="$(awk -F: '{ if (big < $3 && $3 < 5000) big=$3 } END { print big + 1 }' $pwfile)"
This line scans through the /etc/passwd file, ascertaining the largest user ID currently in use that's less than the highest allowable user account value and then adding 1 to it for the new account user ID. This saves the admin from having to remember what the next available ID is, and it also offers a high degree of consistency in account information as the user community evolves and changes. ()
homedir=$hdir/$login
This line states that the $hdir is the base for home directories (for instance, /home), $login is the username you're creating (kiverson). So homedir is going to be "/home/$login".
For example, "/home/kiverson"
# We are giving each user their own group, so gid=uid
gid=$uid
This section is a comment line and defines that the group id to match the user id. This means that the users would belong to a group and that they will share security settings.
echo -n "full name: " ; read fullname
This line prompts for full name. Usually, the echo command prints something, and moves to the next line. With '-n', it stays on the same line. So the output of this line will read
(full name: Khalid Iverson)
echo -n "shell: " ; read shell
This line prompts for shell input, Usually, the echo command prints something, and moves to the next line. With '-n', it stays on the same line. So the output of this line will read
(shell: Bash)
echo "Setting up account $login for $fullname..."When full name and shell output is added the prompt confirms that an account has been set up. The script will read
(Setting up account kiverson for Khalid Iverson)
echo ${login}:x:${uid}:${gid}:${fullname}:${homedir}:$shell >> $pwfile
This line states the user (ex. kiverson:x:11647:0:99999:/home:bash) information printed on the command line and will be written or copied to the users $pwfile in /etc/passwd.
echo ${login}:*:11647:0:99999:7::: >> $shadowfile
This line states the user (ex. kiverson:x:11647:0:99999:7:::) information will be printed on the command line and written or copied to the users $shadowfile in /etc/shadow.
echo "${login}:x:${gid}:$login" >> $gfile
This line states the user (ex. kiverson:x99999:kiverson) information will be printed on the command line and written or copied to the users $gfile in /etc/group.
mkdir $homedir
This line then creates a new directory for the user as defined above.
cp -R /etc/skel/.[a-zA-Z]* $homedir
This line copies the each source file into the directory (retaining the same name).
chmod 755 $homedir
This line states the permissions of the users on the directory. 755 in this case would mean that: 7 - Owner can read, write and execute 5 - The group can read and execute, but not write, 5 - Everyone else can read and execute, but not write.
find $homedir -print | xargs chown ${login}:${login}
In this line, 'find' looks in a directory, and locates every file and directory within it and every subdirectory. It then has the option of doing something with each thing that it finds. In this case, it simply prints the name of whatever it finds '-print'. Each name is piped into xargs, which runs a command on each name it receives.
# Setting an initial password
This is a comment lines. # is used in shell scripts as the comment character. The script typically ignores all text that follows on the same line.
passwd $login
These line sets the initial value (password - created by administrator), then allows the user to change the created password at their first login.
exit 0
This line exits the script. The “0” represents a success (or no-error) code to return to the calling program.