Script to change password on linux servers over ssh

后端 未结 17 2675
南方客
南方客 2020-12-08 11:22

We have a number of Red Hat linux servers in our IT environment. I am being asked by my team members to write a script (preferably shell script) to change a user\'s password

相关标签:
17条回答
  • 2020-12-08 11:52

    Thought I should put my solution in an answer field - not sure if this should be a part of the question..

    OK, I have put together a partially working solution using Dennis' suggestion.

    servers.txt looks like:

    server1
    server2
    server3
    .
    .
    .
    

    I am using:

    for server in `cat servers.txt`; do
    ssh $server -l user 'passwd <<EOF
    old_pass
    new_pass
    new_pass
    EOF';
    done
    

    This produces:

    user@server1's password: **<Type password manually>**
    (current) UNIX password: New UNIX password: Retype new UNIX password: Changing password for user user.
    Changing password for user
    passwd: all authentication tokens updated successfully.
    user@server2's password: **<Type password manually>**
    (current) UNIX password: New UNIX password: Retype new UNIX password: Changing password for user user.
    Changing password for user
    passwd: all authentication tokens updated successfully.
    

    So here, I still need to type my old password once for each server. Can this be avoided?

    0 讨论(0)
  • 2020-12-08 11:53

    Have you tried App::Unix::RPasswd

    0 讨论(0)
  • 2020-12-08 11:55

    If you have ssh, why have passwords in the first place? Push the user's public ssh key to all the servers they're authorized to use and be done with it. This also lets you easily grant and revoke access all you want.

    At a previous $dayjob, where we had literally tens of thousands of servers, they had a database of which engineers were allowed on which servers, and the installation of ssh keys was an automated process. Almost NOBODY had a password on ANY machine.

    0 讨论(0)
  • 2020-12-08 11:56

    echo -e "wakka2\nwakka2\n" | passwd root

    0 讨论(0)
  • 2020-12-08 11:57

    You do not need root access to use passwd.

    This shoud work just fine.

    passwd <<EOF
    old password
    new password
    new password
    EOF
    
    0 讨论(0)
提交回复
热议问题