Transfer SSH public key to another machine in one step

ssh-keygen; ssh-copy-id user@host; ssh user@host
This command sequence allows simple setup of (gasp!) password-less SSH logins. Be careful, as if you already have an SSH keypair in your ~/.ssh directory on the local machine, there is a possibility ssh-keygen may overwrite them. ssh-copy-id copies the public key to the remote host and appends it to the remote account's ~/.ssh/authorized_keys file. When trying ssh, if you used no passphrase for your key, the remote shell appears soon after invoking ssh user@host.

20
By: bwoodacre
2009-03-18 07:59:33

These Might Interest You

  • If you use Mac OS X or some other *nix variant that doesn't come with ssh-copy-id, this one-liner will allow you to add your public key to a remote machine so you can subsequently ssh to that machine without a password.


    64
    cat ~/.ssh/id_rsa.pub | ssh user@machine "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"
    DEinspanjer · 2009-02-05 19:37:56 18
  • This uses ssh to transfer the contents of one Mac's clipboard to another's. This only works with plain text, sadly. Trying to transfer images will just clear out the remote machine's clipboard, and rich text will be converted to plain text. Using the "Remote Login" must be enabled on the remote machine (via System Preferences' Sharing panel) for this to work.


    1
    pbpaste | ssh user@hostname pbcopy
    DavidLudwig · 2011-04-24 16:30:48 1
  • This one is a bit more robust -- the remote machine may not have an .ssh directory, and it may not have an authorized_keys file, but if it does already, and you want to replace your ssh public key for some reason, this will work in that case as well, without duplicating the entry.


    4
    cat ~/.ssh/id_rsa.pub | ssh <REMOTE> "(cat > tmp.pubkey ; mkdir -p .ssh ; touch .ssh/authorized_keys ; sed -i.bak -e '/$(awk '{print $NF}' ~/.ssh/id_rsa.pub)/d' .ssh/authorized_keys; cat tmp.pubkey >> .ssh/authorized_keys; rm tmp.pubkey)"
    tamouse · 2011-09-30 07:39:24 2
  • Requires: curl xsel access to the internet(http://transfer.sh) This is an alias utilizing the transfer.sh service to make sharing files easier from the command line. I have modified the alias provided by transfer.sh to use xsel to copy the resulting URL to the clipboard. The full modified alias is as follows since commandlinefu only allows 255 characters: transfer() { if [ $# -eq 0 ]; then echo "No arguments specified. Usage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"; return 1; fi if tty -s; then basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9._-]/-/g'); curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile" |xsel --clipboard; else curl --progress-bar --upload-file "-" "https://transfer.sh/$1" |xsel --clipboard ; fi; xsel --clipboard; } Show Sample Output


    2
    transfer() { basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9._-]/-/g');curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile"|xsel --clipboard;xsel --clipboard ; }
    leftyfb · 2016-03-20 19:38:48 0

What Others Think

yes, but you missed some params in ssh-keygen for passwordless prompt nothing. ssh-keygen -t rsa -f ~/.ssh/id_rsa -b 2048 -P
starchox · 479 weeks and 1 day ago
@starchox you have specified the default values for the -t -f and -b options so they aren't necessary assuming a recent ssh-keygen. To truly get everything to run unattended (which probably isn't a "best practice") without a passphrase, try ssh-keygen -P ''; ssh-copy-id -i ~/.ssh/id_rsa user@host; ssh user@host that's two single quotes after the -P setting the passphrase to an empty string which I believe should be the same as no passphrase at all.
bwoodacre · 473 weeks and 3 days ago
I think what starchox was trying to say it that in order to be not prompted at all you have to specify everything his command did; and more, actually... ssh-keygen -P '' -f ~/.ssh/id_rsa -b 2048 ...will not ask you for a thing. @bwoodacre: your second command doesn't need the -i in it, that is the default (and without prompt) So, for most of you out there, if you are doing this on a new machine or machine that otherwise needs a new key to be written, your command for doing everything you need (without a passphrase) on one line is: ssh-keygen -P '' -f ~/.ssh/id_rsa -b 2048; ssh-copy-id user@host; ssh user@host ...ssh user@host is obviously only for logging in when done ...and most systems don't prompt for the -b (bits) and default to 2048 :)
sudopeople · 469 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: