Skip to content

Instantly share code, notes, and snippets.

@sudocurse
Created June 26, 2018 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudocurse/8f3bf3a21304e33e0b5ebf60b1980286 to your computer and use it in GitHub Desktop.
Save sudocurse/8f3bf3a21304e33e0b5ebf60b1980286 to your computer and use it in GitHub Desktop.
make a user, give them key access
#!/bin/bash
# consume the first argument so we can deal with the key as one variable
username=$1
shift
sudo useradd $username
sudo -u $username mkdir /home/$username/.ssh/
sudo -u $username touch /home/$username/.ssh/authorized_keys
sudo chmod 640 /home/$username/.ssh/authorized_keys
# $@ means 'all of the arguments'. since we consumed the first one, all that's left is this.
# this approach lets us avoid worrying about whether or not the pubkey was quoted
sudo -u $username sh -c "echo $@ >> /home/$username/.ssh/authorized_keys"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment