简体   繁体   中英

How to create a new username and password in Bash script?

I am trying to create a script where it adds a new user and password and also checks to see if that user and password already exist, while running in Root.

So, my script is running and working just fine. It only runs in the root and it correctly checks to see if a username is already being used. But, I cannot seem to add a new user and password. The following is my whole script:

#!/bin/bash
#Creating a script that creates a new user

ROOT_UID=0      #Root has $UID 0
SUCCESS=0
E_USEREXISTS=70
E_NOTROOT=65        #Not root

#Run as root, and this checks to see if the creater is in root. If not, will not run
if [ "$UID" -ne "$ROOT_UID" ]; then
    echo "Sorry must be in root to run this script"
    exit $E_NOTROOT
fi

if [ $# -eq 2 ]; then
username=$1
pass=$2

grep -q "$username" /etc/passwd

if [ $? -eq $SUCCESS ]; then
    echo "User $username already exists"
    echo "Please choose another username"
    exit $E_USEREXISTS 
fi

echo $pass | passwd $username --stdin
echo "the account is setup"

else
    echo "this program needs 2 arguments and you have given $#"
    echo "you have to call the script $0 username and the pass"
fi

exit 0

And I am not getting a straight up error, but here is an example of what is happening: I try to add someone (username then password):

[root@localhost Documents]# ./Script dkong 123bif

And the response is:

passwd: Unknown user name 'dkong'
the account is setup

Could someone help direct me? I want it to create a new username and password and I am not understanding why it is not.

It has been a while since I have used scripts so I am sorry if the answer is obvious! Just need a little direction. Thank you in advance!

Nevermind! I got it working! This is my solution:

useradd $username -d /home/$username -m ;
echo $passwd | passwd $username --stdin;
echo "the account is setup"

The error is in that you need to add the username to /etc/passwd after checking that isn't already exist by that line 'grep -q "$username" /etc/passwd' So you can simply add that line 'useradd $username' after the second if condition Exactly before that line 'echo $pass | passwd $username --stdin' The error happens because you tell passwd to make a password for a user that isn't already exist

To start with, never use full uppercase identifiers for user variables as they are reserved for use with shell environment.

So

ROOT_UID=0

should be

root_uid=0 # and so on

Creating a username requires useradd command. It automatically does the background checking to see if the user already exists and if so aborts the process and you just need to check the exit status of the command to see if the user creation was successful.

You may pass the username as an argument to the script, but passing password as plain text is not good in a security perspective. The Linux passwords are stored as hashes. The hashing algorithm can be MD5, SHA-512 and so. You need to use the crypt function, mentioned here , to help useradd deal storing hashed password.

Below would be my script

#!/bin/bash
# Checking pre-requirements, the script should contain one argument
[ -z "$1" ] && echo "Usage : script user_name" && exit 1
username=$1
# Read password key and has silently using -s ie keystrokes are invisible
read -sp 'Enter password key - ' key
# Typically ask for a confirmation, but omitted for the sake of brevity
# I suppose you default password hash algorithm is MD5

# You need some characters for a salt. Read the content in link mentioned above
read -sp 'Read upto 8 characters for salt - ' salt
# You can sanitize the user input for salt to trim its length to 8 and use it like below
useradd -m -d /home/$username \
-p $(perl -e 'print crypt("$ARGV[0]", "\$1\$$ARGV[1]")' "$username" "${salt:0:8}")
# Check the exit status of the last command
[ $? -eq 0 ] && echo "User $username successfully created" || \
echo "Sorry ! User $username couldn't be created at the moment"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM