Last active
August 29, 2015 14:11
-
-
Save shichao-an/91b64d83363e93bc21f4 to your computer and use it in GitHub Desktop.
Add SSH public key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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