Skip to content

Instantly share code, notes, and snippets.

@macrotis
Last active December 16, 2015 07:39
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 macrotis/5400665 to your computer and use it in GitHub Desktop.
Save macrotis/5400665 to your computer and use it in GitHub Desktop.
Someone I know needed an easy way to SSH into a Linux host from a Mac without having to remember the password. The "correct" answer is to set up an RSA public/private key pair and configure the remote host to accept it, but for someone whose primary responsibilities involve wrestling with Adobe software all day, that's just another hurdle to jum…
#!/bin/bash
# The above line tells the OS to run this file with the executable stored
# in /bin/bash; in this case GNU BASH.
keydir=$HOME/.ssh/
keyfile=id_rsa
keyloc=$keydir/$keyfile
ruser= # The username of the remote user
rhost= # The IP Address or DNS name of the remote host
rport=22 # Remote port to connect to; this will nearly always be TCP 22.
# Many operating systems (notably OS X) don't come with a .ssh/ directory by
# default, so we attempt to create it here. If mkdir fails due to an already
# existing directory, that's fine, we can continue on anyway.
mkdir $keydir
chmod 0700 $keydir
# Generate an RSA 2048-bit private/public keypair with no passphrase protecting
# it and store it in the location specified by $keyloc
ssh-keygen -t rsa -b 2048 -f $keyloc -N ''
# Install the public key in the ~/.ssh/authorized_keys file on the remote host.
# This allows SSH to authenticate based on the public key you've presented.
# Additionally, it sets all the file permissions in the obnoxious way SSH
# likes it (if we didn't do that, SSH would simply refuse to use the key).
ssh -p $rport $ruser@$rhost \
"cat > /tmp/id_rsa_$ruser; mkdir ~/.ssh/; chmod 0700 ~/.ssh/;
cat /tmp/id_rsa_$ruser >> ~/.ssh/authorized_keys; rm /tmp/id_rsa_$ruser
chmod 0600 ~/.ssh/authorized_keys" < $keyloc.pub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment