├── README.md ├── install.sh └── ssh-copy-id.sh /README.md: -------------------------------------------------------------------------------- 1 | # ssh-copy-id for OSX 2 | 3 | > Quick port of the useful unix utility `ssh-copy-id` 4 | 5 | ## Note: MacOS Sierra now includes an onboard `ssh-copy-id` in `/usr/bin` 6 | 7 | ## Installation 8 | 9 | Git clone & run install script 10 | 11 | OR 12 | 13 | `curl -L https://raw.githubusercontent.com/beautifulcode/ssh-copy-id-for-OSX/master/install.sh | sh 14 | ` 15 | 16 | 17 | ## SSH-COPY-ID [man][man] 18 | 19 | ### NAME 20 | `ssh-copy-id` - install your identity.pub in a remote machine's authorized_keys 21 | 22 | ### SYNOPSIS 23 | ssh-copy-id [-i [identity_file]] [user@]machine 24 | 25 | ### DESCRIPTION 26 | 27 | **ssh-copy-id** is a script that uses ssh to log into a remote machine (presumably using a login password, so password authentication should be enabled, unless 28 | you've done some clever use of multiple identities) 29 | 30 | It also changes the permissions of the remote user's home, `~/.ssh`, and `~/.ssh/authorized_keys` to remove group writability (which would otherwise prevent 31 | you from logging in, if the remote `sshd` has StrictModes set in its configuration). 32 | 33 | If the `-i` option is given then the identity file (defaults to `~/.ssh/identity.pub`) is used, regardless of whether there are any keys in your ssh-agent. 34 | Otherwise, if this: 35 | 36 | `ssh-add -L` 37 | 38 | provides any output, it uses that in preference to the identity file. 39 | 40 | If the `-i` option is used, or the ssh-add produced no output, then it uses the contents of the identity file. Once it has one or more fingerprints (by 41 | whatever means) it uses ssh to append them to `~/.ssh/authorized_keys` on the remote machine (creating the file, and directory, if necessary) 42 | 43 | ### SEE ALSO 44 | 45 | ssh(1), ssh-agent(1), sshd(8) 46 | 47 | [man]: http://linux.die.net/man/1/ssh-copy-id 48 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Installs ssh-copy-id into /usr/local/bin 3 | 4 | if [[ $(id -u) != 0 ]]; then 5 | if command -v sudo >/dev/null 2>&1; then 6 | SUDO="sudo" 7 | else 8 | echo >&2 "Requires sudo but it's not installed. Aborting." 9 | exit 1 10 | fi 11 | fi 12 | 13 | if git ls-files >& /dev/null && [[ -f ssh-copy-id.sh ]]; then 14 | $SUDO cp ssh-copy-id.sh /usr/local/bin/ssh-copy-id || { echo "Failed to install ssh-copy-id into /usr/local/bin."; exit 1; } 15 | else 16 | $SUDO curl -L https://raw.githubusercontent.com/beautifulcode/ssh-copy-id-for-OSX/master/ssh-copy-id.sh -o /usr/local/bin/ssh-copy-id || { echo "Failed to install ssh-copy-id into /usr/local/bin."; exit 1; } 17 | $SUDO chmod +x /usr/local/bin/ssh-copy-id || { echo "Failed to install ssh-copy-id into /usr/local/bin."; exit 1; } 18 | fi 19 | echo "Installed ssh-copy-id into /usr/local/bin."; exit 0; 20 | -------------------------------------------------------------------------------- /ssh-copy-id.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Shell script to install your public key on a remote machine 4 | # Takes the remote machine name as an argument. 5 | # Obviously, the remote machine must accept password authentication, 6 | # or one of the other keys in your ssh-agent, for this to work. 7 | 8 | ID_FILE="${HOME}/.ssh/id_rsa.pub" 9 | 10 | if [ "-i" = "$1" ]; then 11 | shift 12 | # check if we have 2 parameters left, if so the first is the new ID file 13 | if [ -n "$2" ]; then 14 | if expr "$1" : ".*\.pub" > /dev/null ; then 15 | ID_FILE="$1" 16 | else 17 | ID_FILE="$1.pub" 18 | fi 19 | shift # and this should leave $1 as the target name 20 | fi 21 | else 22 | if [ x$SSH_AUTH_SOCK != x ] ; then 23 | GET_ID="$GET_ID ssh-add -L | grep -vxF 'The agent has no identities.'" 24 | fi 25 | fi 26 | 27 | if [ -z "`eval $GET_ID`" ] && [ -r "${ID_FILE}" ] ; then 28 | GET_ID="cat ${ID_FILE}" 29 | fi 30 | 31 | if [ -z "`eval $GET_ID`" ]; then 32 | echo "$0: ERROR: No identities found" >&2 33 | exit 1 34 | fi 35 | 36 | if [ "$#" -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then 37 | echo "Usage: $0 [-i [identity_file]] [user@]machine" >&2 38 | exit 1 39 | fi 40 | 41 | { eval "$GET_ID" ; } | ssh $1 "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys; test -x /sbin/restorecon && /sbin/restorecon .ssh .ssh/authorized_keys" || exit 1 42 | 43 | cat <