Skip to content

Instantly share code, notes, and snippets.

@shichao-an
Last active August 29, 2015 14:11
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 shichao-an/91b64d83363e93bc21f4 to your computer and use it in GitHub Desktop.
Save shichao-an/91b64d83363e93bc21f4 to your computer and use it in GitHub Desktop.
Add SSH public key
#!/bin/bash
# Usage:
# First, source this script:
# $ source add_public_key.sh
# Then, use the function add user:
# $ add_public_key john 'ssh-rsa very-long-hash'
add_public_key()
{
local username="$1"
local ssh_key="$2"
local ssh_dir=$(eval echo ~$username/.ssh)
local auth_key_file="$ssh_dir/authorized_keys"
mkdir "$ssh_dir"
echo "$ssh_key" > "$auth_key_file"
chown -R "$username:$username" "$ssh_dir"
chmod 700 "$ssh_dir"
chmod 600 "$auth_key_file"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment