Skip to content

Instantly share code, notes, and snippets.

@valerio-bozzolan
Last active February 2, 2022 10:31
Show Gist options
  • Save valerio-bozzolan/70e0184478fa0ffa52a37745ac04f333 to your computer and use it in GitHub Desktop.
Save valerio-bozzolan/70e0184478fa0ffa52a37745ac04f333 to your computer and use it in GitHub Desktop.
Add Unix user with only SSH key (without password)
#!/bin/sh
#
# License: CC0
# Author: Valerio Bozzolan
# https://gist.github.com/valerio-bozzolan/70e0184478fa0ffa52a37745ac04f333
#
# Quickly create an user without password but with an SSH key
#
USERNAME="$1"
KEY="$2"
if [ -z "$KEY" ]; then
echo "Usage: "
echo " $0 'USERNAME' 'SSH-KEY... comment'"
exit 1
fi
adduser "$USERNAME" --disabled-password
mkdir --parents /home/"$USERNAME"/.ssh
echo "$KEY" >> /home/"$USERNAME"/.ssh/authorized_keys
chown "$USERNAME":"$USERNAME" -R /home/"$USERNAME"/.ssh
chmod 640 /home/"$USERNAME"/.ssh/authorized_keys
chmod 755 /home/"$USERNAME"/.ssh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment