Bash – Ubuntu: how to create a user with —-disabled-password and still have them sudo (which fails as there is no password)

bashlinode

I am trying to write a linode stackscript, which is a bash file which is run as root when you build a new image. It can prompt for parameters such as USER, KEY and possibly, but not ideally, PASSWORD.

In the script I create a user "bob", then

adduser --disabled-password --gecos "" --shell /bin/bash $USER
adduser $USER sudo 

We are creating the user with certificate and with no password, the user, once logged in via SSH, cant sudo as it prompts for a password that doesnt exist.

I assume there are 2 options here:

  1. give it a password. Unfortunately, there is no option to do this via parameter, so is there a way to do it without prompts (not interactive)?
  2. fix it so bob doesn't need to enter a password.

Best Answer

Instead disabling password, create a user without password.

workaround

adduser --disabled-password --gecos "" --shell /bin/bash $USER usermod -g sudo $USER passwd -d $USER

Related Topic