FreeBSD Change User Password Shell Script

To change the password of a user in FreeBSD using a shell script, you can use the passwd command. Here’s an example shell script that prompts for a username and new password:

#!/bin/sh

# Prompt for the username
echo "Enter the username: "
read username

# Prompt for the new password
echo "Enter the new password: "
read -s password

# Use the passwd command to change the user's password
echo "$password" | passwd --stdin "$username"

# Check if the password change was successful
if [ $? -eq 0 ]; then
echo "Password changed successfully for user $username"
else
echo "Failed to change password for user $username"
fi

Here’s how the script works:

  1. The script prompts the user to enter the username and new password using the read command.
  2. The passwd command is used to change the user’s password. The --stdin option is used to pass the new password to the passwd command via standard input, and the username is specified as an argument.
  3. The script checks the exit status of the passwd command to determine if the password change was successful or not.

Save the above script to a file, for example “chpasswd.sh”, and make it executable using the command chmod +x chpasswd.sh. You can then run the script by typing ./chpasswd.sh in the terminal and following the prompts.

Leave a Comment