Adhz
Adhz

Reputation: 55

how to pass a user password ,stored in config file to passwd command

I am trying to create linux users using a script . The username and corresponding password are stored in a config file.

config file is given below

username="user1"
password="passxxx"

using a shell script i am creating linux users with username and password provided in config file.

if [ -n "$username" ] then

  password="$line" 
  sudo adduser $username 
  sudo passwd $username

Here i have to give the password in the config file. usually we have to type the required password twice to update the password information . Is there any way to update the password without typing ( use the password provided in config file) .

Upvotes: 1

Views: 2722

Answers (1)

Sasha
Sasha

Reputation: 4014

Use chpasswd instead of passwd to set passwords in batch mode.

Example:

sudo chpasswd <<<"$username:$password"

Or, it you're using other shell (not bash of version >=3.0):

echo "$username:$password" | sudo chpasswd

Upvotes: 3

Related Questions