How should I set password for a user I created using ssh?

101 views Asked by At

I have 20 machines where I need to create a user and set his password. I can create the accounts and set the passwords using a for loop. The inside of the for loop is given as follows

ssh -t user1@$node_name 'sudo useradd user2'

ssh -t user1@$node_name 'sudo passwd user2'

However, this requires me to input the password for user1 first and then input the new password for user2. I tried it for 2 machines and it works. I however do not like the wasteful effort involved and am guessing there would me a more efficient way of doing so. Any ideas?

1

There are 1 answers

1
Ross Presser On BEST ANSWER

To remove the need to enter user1's password, you can mess with the sudo -A or -a options on $node_name to get authentication to happen automatically in some other way.

To remove the need to type user2's password, you can try something like this:

ssh -t user1@$node_name "sudo echo $newpass | useradd user2 --stdin"