It's possible with usermod -p PASSWD USERNAME
and while loop. Problem is that PASSWD cannot be in plain text, it has to be encrypted password. For that purpose you can use mkpasswd
. That way "newPassString" is encrypted , and remains the same for all users. Here's example of what can be done in just one line of code.
$ cat userlist | while read USERNAME; do echo "$USERNAME";sudo usermod -p "$(mkpasswd "newPassString")" "$USERNAME" ;done
tester
[sudo] password for xieerqi:
testuser
[sudo] password for xieerqi:
Notice that i have to enter sudo
for each sudo
call - that's my personal setting. In regular, unaltered /etc/sudoers
file, the timeout is 15 minutes by default, so you'd need to enter it only once.
A better approach is to put the code into a script, and call with parameters $1 as filename and $2 as new password string :
xieerqi:$ cat passScript.sh
#!/bin/bash
while read USERNAME; do
echo "$USERNAME";
usermod -p "$(mkpasswd "$2")" "$USERNAME"
done < "$1"
xieerqi:$ chmod +x passScript.sh
xieerqi:$ sudo ./passScript.sh userlist myNewPass
[sudo] password for xieerqi:
tester
testuser
xieerqi:$ su testuser
Password:
testuser@eagle:/home/xieerqi$