FreeBSD Change User Password Shell Script

https‮l.www//:‬autturi.com
FreeBSD Change User Password Shell Script

Here is a shell script that you can use to change the password for a user on a FreeBSD system:

#!/bin/sh

# Check that the script is being run as root
if [ "$(id -u)" -ne 0 ]; then
  echo "This script must be run as root"
  exit 1
fi

# Check that the user exists
if ! id "$1" >/dev/null 2>&1; then
  echo "User $1 does not exist"
  exit 1
fi

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

# Set the new password
echo "$1:$password" | chpasswd

# Check the result of the chpasswd command
if [ $? -ne 0 ]; then
  echo "Failed to change password for $1"
  exit 1
fi

echo "Password for $1 has been successfully changed"

To use this script, save it to a file (for example, change_password.sh) and make it executable using the chmod command:

chmod +x change_password.sh

Then, you can run the script as root, passing the username as the first argument:

./change_password.sh username

The script will prompt you to enter the new password for the user, and then set the new password using the chpasswd command.

Created Time:2017-10-28 20:40:33  Author:lautturi