I'd like to provision some new personal accounts on the Ubuntu Linux server. I know how to create user accounts with root, useradd and then how to manually set their authorized_keys file.

However is there a simple way to just create a specific user by giving its username and SSH public key? Passwords are not needed.

I know how to write a shell script for this myself, but it feels a bit overkill and I feel I am missing something in the user management, as this should be quite common task.

  • 1
    What version of Ubuntu are you using?
    – David
    Oct 20, 2022 at 13:02
  • I am using 20.04 and 22.04. Oct 20, 2022 at 13:02

1 Answer 1


Never mind, I wrote this one-liner that does the job:

(   export NEW=example && \
    export KEY="ssh-rsa AAAAB3Nz... [email protected] && \
    adduser --gecos "" --disabled-password $NEW && \
    mkdir /home/$NEW/.ssh && \
    echo $KEY > /home/$NEW/.ssh/authorized_keys && \
    chown -R $NEW:$NEW /home/$NEW/.ssh && \
    chmod 644 /home/$NEW/.ssh/authorized_keys && \
    chmod 0700 /home/$NEW/.ssh \

Edit the NEW username and its ssh KEY and copy-paste to terminal as root.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .