-2

The following script is supposed to create a user and generate a random password on an Ubuntu machine:

#!/bin/bash

#Script should execute with sudo/root access
if [[ "${UID}" -ne 0 ]]
then
    echo "Please run with sudo or root"
    exit 1
fi

#User should provide atleast one argument as username else guide him
if [[ "${#}" -lt 1 ]]
then
    echo "Usage: ${0} USER_NAME [COMMENT]..."
    echo "Create a user with name USER_NAME and comments field of COMMENT"
    exit 1
fi

#Store 1st argument as user name
USER_NAME="${1}"

#In case of more than one argument, store it as account comments
shift
COMMENT="${@}"

#Create a password
 PASSWORD=$(date +%s%N)

#Create the user
useradd -c "$COMMENT" -m $USER_NAME

#Check if user is successfully created or not
if [[ $? -ne 0 ]]
then
    echo "The Account could not be created"
    exit 1
fi

#Set the password for the user
echo $PASSWORD | passwd --stdin $USER_NAME

#Check if password is successfully set or not
if [[ $? -ne 0 ]]
then
    echo "The password could not be set"
    exit 1
fi

#Force password change on first login
passwd -e $USER_NAME

#Display username, password and the host where the user was created
echo
echo "Username: $USER_NAME"
echo
echo "Password: $PASSWORD"
echo
echo "Hostname: $(hostname)"

Output:

root@Sengh:/home/sengh/scripts# bash user_create.sh singh this is a
passwd: unrecognized option '--stdin'
Usage: passwd [options] [LOGIN]

Options:
  -a, --all                     report password status on all accounts
  -d, --delete                  delete the password for the named account
  -e, --expire                  force expire the password for the named account
  -h, --help                    display this help message and exit
  -k, --keep-tokens             change password only if expired
  -i, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -l, --lock                    lock the password of the named account
  -n, --mindays MIN_DAYS        set minimum number of days before password
                                change to MIN_DAYS
  -q, --quiet                   quiet mode
  -r, --repository REPOSITORY   change password in REPOSITORY repository
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       directory prefix
  -S, --status                  report password status on the named account
  -u, --unlock                  unlock the password of the named account
  -w, --warndays WARN_DAYS      set expiration warning days to WARN_DAYS
  -x, --maxdays MAX_DAYS        set maximum number of days before password
                                change to MAX_DAYS

The password could not be set
8
  • 2
    OK, what is the question? Did you read the error message? It is quite clear and tells you that --stdin isn't a valid option for passwd. Commented Jan 29, 2024 at 10:12
  • some versions of passwd might have that option. But if yours doesn't, then it doesn't. Commented Jan 29, 2024 at 11:15
  • @terdon I am referring to this video: youtu.be/TtGM9GfBuok?si=VdsHe-tpW73Imkgu&t=18409 where he used --stdin to rectify the "Password could not be set" error. Commented Jan 29, 2024 at 13:27
  • 1
    I'm sorry, but following random youtube videos is rarely a good way to learn. In this case, presumably, the video's creator is using a different system. More importantly, always make sure to read the error message carefully. They tell you what is wrong, and you have a very nice passwd: unrecognized option '--stdin' message. Commented Jan 29, 2024 at 13:51
  • 1
    I saw in youtube someone ran blah and a chicken salad magically appeared on his desk. But if I write in the shell blah, I get the message blah: Command not found. How to resolve it? Commented Jan 30, 2024 at 9:40

2 Answers 2

2

The reason it doesn't work is apparent in the output of your command, as multiple comments already pointed out:

passwd: unrecognized option '--stdin'

You say you it worked in youtube. Well, many things might work in youtube (or any other tutorial) but not for you, due to different distros, tools versions etc. In your specific case, you can see the answer in the Using passwd Command in Shell Script tutorial in LinuxOPsys:

Note: If you're seeing the error message passwd: unrecognized option '--stdin', it means that the passwd command on your system doesn't support the --stdin option. This option is typically available on some distributions like CentOS Stream or RHEL, but may not be present on others, like Ubuntu.

So probably the person in the youtube video used one of those distributions that supported this option. On your distribution, however, this option is obviously not available.

Why don't most passwd implementations support this option? You can find the answer for this question in the following Bash FAQ page:

The traditional passwd(1) does not read from standard input. This is intentional. It is for your protection. Passwords were never intended to be put into programs, or generated by programs. They were intended to be entered only by the fingers of an actual human being, with a functional brain, and never, ever written down anywhere. So before you continue, consider the possibility that the authors of passwd(1) were on to something, and you probably shouldn't be trying to script passwd(1) input.

If you still insist in automating it using a script after reading the warning above, you can find the answer in the same LinuxOPsys tutorial in the Using chpasswd section, as also suggested by @NicolasFormichella's answer.

You commented to this answer:

I don't want to mention the user whose password needs to be set, instead I want it to read from the user using variable as to which user needs to be created.

Well, you could still use the variables. Instead of

echo $PASSWORD | passwd --stdin $USER_NAME

Use:

echo "$USER_NAME:$PASSWORD" | chpasswd

But generally speaking, you shouldn't trust any random youtube video you encounter.

1
  • Thank you so much. echo "$USER_NAME:$PASSWORD" | chpasswd worked like a charm. The reason this script is generating password using nanoseconds is because it is always gonna be a random number and secondly, it is always gonna ask the user to change the password immediately so I don't think there's anything wrong in using such scripts when creating bulk users. Commented Feb 1, 2024 at 11:12
0

What you want is chpasswd

man 8 chpasswd states :

chpasswd - update passwords in batch mode

Example usage

echo "example:example" | chpasswd

Would change the password of the example user

2
  • ...or use a different version of passwd, or wrap passwd in an expect script. Commented Jan 29, 2024 at 10:51
  • @Nicolas Actually, I am referring to this video: youtu.be/TtGM9GfBuok?si=VdsHe-tpW73Imkgu&t=18409 and I don't want to mention the user whose password needs to be set, instead I want it to read from the user using variable as to which user needs to be created. Refer to the video shared above to check the purpose of this script. Commented Jan 29, 2024 at 13:30

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.