Using the passwd command from within a shell script

前端 未结 13 991
遥遥无期
遥遥无期 2020-11-28 04:20

I\'m writing a shell script to automatically add a new user and update their password. I don\'t know how to get passwd to read from the shell script instead of interactively

13条回答
  •  离开以前
    2020-11-28 04:51

    Read the wise words from:

    • http://mywiki.wooledge.org/BashFAQ/078

    I quote:

    Nothing you can do in bash can possibly work. passwd(1) does not read from standard input. This is intentional. It is for your protection. Passwords were never intended to be put into programs, or generated by programs. They were intended to be entered only by the fingers of an actual human being, with a functional brain, and never, ever written down anywhere.

    Nonetheless, we get hordes of users asking how they can circumvent 35 years of Unix security.

    It goes on to explain how you can set your shadow(5) password properly, and shows you the GNU-I-only-care-about-security-if-it-doesn't-make-me-think-too-much-way of abusing passwd(1).

    Lastly, if you ARE going to use the silly GNU passwd(1) extension --stdin, do not pass the password putting it on the command line.

    echo $mypassword | passwd --stdin # Eternal Sin.
    echo "$mypassword" | passwd --stdin # Eternal Sin, but at least you remembered to quote your PE.
    passwd --stdin <<< "$mypassword" # A little less insecure, still pretty insecure, though.
    passwd --stdin < "passwordfile" # With a password file that was created with a secure `umask(1)`, a little bit secure.
    

    The last is the best you can do with GNU passwd. Though I still wouldn't recommend it.

    Putting the password on the command line means anyone with even the remotest hint of access to the box can be monitoring ps or such and steal the password. Even if you think your box is safe; it's something you should really get in the habit of avoiding at all cost (yes, even the cost of doing a bit more trouble getting the job done).

提交回复
热议问题