FreeBSD Change User Password Shell Script

How do I change user password using a shell script under FreeBSD operating systems?

ADVERTISEMENTS

You need to use the -h fd option for pw command. It provides a special interface by which interactive scripts can set an account password using pw. Because the command line and environment are fundamentally insecure mechanisms by which programs can accept information, pw will only allow setting of account and group passwords via a file descriptor (usually a pipe between an interactive script and the program). sh, bash, ksh and perl all possess mechanisms by which this can be done. Linux / UNIX should able to use passwd command or perl script to do the same.

Task: Add User And Set A Password

Add a new user called vivek with password called topSecrete:
# echo PASSWORD | pw add user USERNAME -h 0
# echo topSecrete | pw add user vivek -h 0

Task: Modify Existing User Password

Change the password for a user called chitra
# echo newPassword | pw mod user chitra -h 0

A Sample Shell Script To Change User Password

#!/bin/sh
PW=/usr/sbin/pw
SHELL=/bin/csh
echo "*** ADD NEW USER SHELL SCRIPT ***"
echo -n "Username : " 
read user
echo "Password : " 
read -n passwd
echo ${passwd} | ${PW} add user ${user} -m -s ${SHELL} -h 0 >/dev/null 2>&1
if [ $? -eq 0 ]
then
	echo "User added to the system."
else
	echo "Failed to add user to the system."
fi
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
6 comments… add one
  • Aneesh Kumar. E.P Jun 1, 2009 @ 9:02

    Hi all,

    How can i achieve the same thing in centos system. When I tried i couldn’t find the pw in my system. I have to download the rpm but i failed to find a rpm for pw command. Please help me

  • Shoaibi Jun 1, 2009 @ 10:19

    hmmm…. spell mistake at line 13:
    “Add a bew user” => “Add a new user”

  • 🐧 nixCraft Jun 1, 2009 @ 10:40

    @Aneesh See this post

    @Shoaibi: thanks for the heads-up!

  • chr15 Jun 1, 2009 @ 13:05

    Aneesh Kumar. E.P,

    you should try something like this:

    useradd -m -d /home/$USER -s $SHELL $USER >/dev/null 2>&1
    echo -n $PASSWORD | passwd –stdin $USER

  • Aneesh Kumar. E.P Jun 2, 2009 @ 7:06

    Thank you for valuable help. i have solved my problem.

    Thank you all

  • syed Jul 25, 2009 @ 2:19

    solved you problem.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.