Skip to content

Instantly share code, notes, and snippets.

@ajbrown
Created August 17, 2016 20:40
Show Gist options
  • Save ajbrown/92d10bd83d468d33966971862963dc5c to your computer and use it in GitHub Desktop.
Save ajbrown/92d10bd83d468d33966971862963dc5c to your computer and use it in GitHub Desktop.
Create a new user, with a random password, allowing public key authentication
#!/bin/sh
# Usage: setup_user.sh <username> <path-to-pub-key>
#
# Credit to TechRepublic.com for inspiration:
USERNAME=$1
KEYPATH=$2
useradd -s /bin/bash -m $USERNAME
openssl rand -base64 8 | tee -a /home/$USERNAME/.password | passwd --stdin $USERNAME
chmod 600 /home/$USERNAME/.password
mkdir -p /home/$USERNAME/.ssh
chmod 700 /home/$USERNAME/.ssh
cp $KEYPATH /home/$USERNAME/.ssh/authorized_keys
chmod 600 /home/$USERNAME/.ssh/authorized_keys
chown -R $USERNAME:$USERNAME /home/$USERNAME/.ssh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment