I create a shell script to add user, create password and force new user change password. But I'm facing trouble during changing password

My script …

#!/bin/bash

# Script should be exicute with root /sudo user

if [[ "${UID}" -ne 0 ]]
then
        echo "Plese run with sudo/root"
        exit 1
fi

# User should proide atlest one argument as Username else guide user

if [[ "${#}" -lt 1 ]]
then
        echo "Usage: $0 username commants"
        exit 1
fi

# Store 1st argument as Username

Username="${1}"

# In case user provide more than one argument, use that as comment

shift
Comment="${@}"

# Create a Password

Password=$(date +%S%N) #It will provide nanosecond as password


# User creat

useradd -c "${Comment}" -m $Username

# Check if user successfully created or not

if [[ $? -ne 0 ]]
then
        echo "Account could not be created"
        exit 1
fi

# Set the passwod for User

echo "${Username}:${Password}" | chpasswd


# Check password successfully created or not

if [[  $? -ne 0 ]]
then
        echo "Password could not be set"
        exit 1
fi


# Force password change on first login

passwd -e "${Username}"

# Display the username password and the hostname where it created

echo "Username: ${Username}"
echo "Password: ${Password}"
echo "$(hostname)"

this script successfully create user and password

sudo bash user_create.sh Raj testUse

output is:

passwd: password expiry information changed.

Username: Raj
Password: 07915539039

but when I do …

su - Raj

output is:

You are required to change your password immediately (administrator enforced).
Changing password for Raj.
Current password:
su: Authentication token manipulation error

How do I solve this ?

You script works for me on Ubuntu 23.04 in Bash. What are you using?

You could try replacing the above line with:

# Force password change on first login
chage -d 0 "${Username}"

I’m using Ubuntu 22.04.4; and this solution is not working

I just checked on Ubuntu 22.04 and it works for me there.

If you get the error su: Authentication token manipulation error you have used the wrong password.

my run of your script:

root@ubuntu-host ~ ➜  ./script.sh diane
passwd: password expiry information changed.
Username: diane
Password: 18160761968
ubuntu-host

root@ubuntu-host ~ ➜  su - diane
You are required to change your password immediately (administrator enforced).
Changing password for diane.
Current password: 
New password: 
Retype new password: