Score:0

Creating a Linux server user with specific public SSH key

iq flag

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.

David avatar
cn flag
What version of Ubuntu are you using?
Mikko Ohtamaa avatar
iq flag
I am using 20.04 and 22.04.
Score:1
iq flag

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

(   export NEW=example && \
    export KEY="ssh-rsa AAAAB3Nz... example@example.com && \
    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.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.