Last Updated: February 25, 2016
·
1.062K
· eosrei

Create an unlocked user for public key only login

Users are locked if they do not have a password. This generates a random password for a user so SSH public key logins work. It is written for Ansible, but the same process will work in a Bash script.

- name: Users | Generate password for example user
  # Use creates to only run makepasswd once.
  shell: makepasswd --chars=20 creates=/home/example/.ssh/id_rsa
  register: make_password

- name: Users | Create user
  user: name=example createhome=yes home=/home/example generate_ssh_key=yes shell=/bin/bash password=${make_password.stdout}