Using the passwd command from within a shell script

前端 未结 13 1005
遥遥无期
遥遥无期 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:33

    Here-document works if your passwd doesn't support --stdin and you don't want to (or can't) use chpasswd for some reason.

    Example:

    #!/usr/bin/env bash
    
    username="user"
    password="pass"
    
    passwd ${username} << EOD
    ${password}
    ${password}
    EOD
    

    Tested under Arch Linux. This passwd is an element of shadow-utils and installed from the core/filesystem package, which you usually have by default since the package is required by core/base.

提交回复
热议问题