LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   change password of other user using ssh (https://www.linuxquestions.org/questions/linux-newbie-8/change-password-of-other-user-using-ssh-4175425557/)

bloodstreetboy 09-04-2012 05:58 AM

change password of other user using ssh
 
There are two servers in LAN. There are number of users in both servers.
I am user of one server. If I want to change password of another user of my server (I have these privileges by root), I can do it easily.
But If I want to change password of other server's user, I use ssh.

I log into other server using ssh and I use sudo passwd username.
It asks me new password for the username and I can change it easily.
But i have to do this thing multiple times. i want to use shell script for this.
I have tried some combinations but it is not working.

I want when I will run the shell script, it should ask me "new password" that I will enter and "retype password" that I will enter again and password will be changed.

I don't want to login manually through ssh and manually enter the command
"sudo passwd username"

My shell scripts which are not working:-

Code:

#!/bin/bash
ssh server-IP sudo passwd user
OR
#!/bin/bash
ssh server-IP 'sudo passwd user;'

It asks me server's password when I enter this, terminal disappears and nothing happens

Code:

#!/bin/bash
ssh server-IP ; sudo passwd user
OR
#!/bin/bash
ssh server-IP; sudo passwd user

It ends on server's command prompt and does not execute the change passwd command.

Code:

#!/bin/bash
ssh server-IP | sudo passwd user;

It is not working too. Please help

Snark1994 09-04-2012 06:13 AM

The problem is that passwd needs you to type it in to the programme itself. Try using the 'chpasswd' command.

(also, it's probably best not to use
Code:

echo user:pass | chpasswd
, if you were tempted to do that - create a file with user:pass in it and run
Code:

chpasswd < pwd_file
for security reasons)

fortran 09-04-2012 06:23 AM

You said
you are using `ssh server-IP` and it asks for password. It means you are logged in as root or may be your user has root privileges in /etc/passwd file because if you would be user, you would use `ssh username@server-IP`

I am not giving you lecture here but I want to say if you do not know essential server administration you could harm your server.

For your solution,
Do not use sudo in shell script. May be your server will be RPM that's why you are facing this problem.
My possible solutions are

#!/bin/bash
ssh server-IP passwd username

OR

#!/bin/bash
ssh server-IP "passwd username"

If you remove sudo into your shell script, they will work.

Now it will ask, server's password and new & retype password for requested user.
If you do not want that it should ask server's password again and again.
Make authorized_keys file in server's ssh directory and put the content of your public file(.ssh/id_dsa.pub or .ssh/id_rsa.pub) into authorized_keys file.

Snark1994 09-05-2012 04:34 AM

Oh, good reading, pavi_kanetkar... I completely misunderstood the question.

http://linuxproblem.org/art_9.html describes what pavi_kanetkar was talking about in his last paragraph.

bloodstreetboy 09-11-2012 01:18 AM

@pavi
Thanks for your solution, Now it is working. I removed word 'sudo' into my shell scripts and most of them are working in the way I want.
Now when I run my shell script in terminal using "./shell-script-name.sh" or "sh shell-script-name.sh"
First it asks for server's password, then I enter it.
Now it asks for user's new password, I entered it too.
Now retype password and after this the username defined in shell script whose password is changed.
That's what I wanted.

Snark1994 09-12-2012 03:55 AM

Brilliant, in that case please mark the thread as 'SOLVED' and give pavi some reputation (click on 'Yes' next to 'Did you find this post helpful?' on his post)


All times are GMT -5. The time now is 07:55 AM.